< Summary - pva.SuperV

Information
Class: pva.SuperV.Api.Services.FieldFormatters.FieldFormatterService
Assembly: pva.SuperV.Api
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/FieldFormatters/FieldFormatterService.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
95%
Covered lines: 78
Uncovered lines: 4
Coverable lines: 82
Total lines: 123
Line coverage: 95.1%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetFieldFormatterTypesAsync()100%11100%
GetFieldFormattersAsync()100%11100%
SearchFieldFormattersAsync()100%11100%
GetFieldFormatterAsync()50%2287.5%
CreateFieldFormatterAsync()50%2290%
UpdateFieldFormatterAsync()50%2290%
DeleteFieldFormatterAsync()100%4492.3%
FilterFieldFormatters(...)100%22100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/FieldFormatters/FieldFormatterService.cs

#LineLine coverage
 1using pva.SuperV.Api.Exceptions;
 2using pva.SuperV.Api.Mappers;
 3using pva.SuperV.Engine;
 4using pva.SuperV.Engine.Exceptions;
 5using pva.SuperV.Engine.FieldFormatters;
 6using pva.SuperV.Model;
 7using pva.SuperV.Model.FieldFormatters;
 8using pva.SuperV.Model.Services;
 9
 10namespace pva.SuperV.Api.Services.FieldFormatters
 11{
 12    public class FieldFormatterService : BaseService, IFieldFormatterService
 13    {
 14        private readonly ILogger logger;
 1515        private readonly Dictionary<string, Comparison<FieldFormatterModel>> sortOptions = new()
 1516            {
 3617                { "name", new Comparison<FieldFormatterModel>((a, b) => a.Name.CompareTo(b.Name)) }
 1518            };
 19
 1520        public FieldFormatterService(ILoggerFactory loggerFactory)
 1521        {
 1522            this.logger = loggerFactory.CreateLogger(this.GetType());
 1523        }
 24
 25        public async Task<List<string>> GetFieldFormatterTypesAsync()
 126        {
 127            logger.LogDebug("Getting field formatters types");
 128            Type fieldFormatterType = typeof(FieldFormatter);
 29#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access othe
 130            return await Task.FromResult(AppDomain.CurrentDomain.GetAssemblies()
 16431                .SelectMany(domainAssembly => domainAssembly.GetExportedTypes())
 872332                .Where(type => type.IsSubclassOf(fieldFormatterType) &&
 872333                        type != fieldFormatterType && !type.IsAbstract)
 134                .Select(type => type.ToString())
 135                .ToList());
 36#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access othe
 137        }
 38
 39        public async Task<List<FieldFormatterModel>> GetFieldFormattersAsync(string projectId)
 940        {
 941            logger.LogDebug("Getting field formatters for project {ProjectId}",
 942                projectId);
 943            return await Task.FromResult(GetProjectEntity(projectId).FieldFormatters.Values
 7944                .Select(fieldFormatter => FieldFormatterMapper.ToDto(fieldFormatter))
 945                .ToList());
 946        }
 47        public async Task<PagedSearchResult<FieldFormatterModel>> SearchFieldFormattersAsync(string projectId, FieldForm
 848        {
 849            logger.LogDebug("Searching field formatters for project {ProjectId} with filter {NameFilter} page number {Pa
 850               projectId, search.NameFilter, search.PageNumber, search.PageSize);
 851            List<FieldFormatterModel> allFieldFormatters = await GetFieldFormattersAsync(projectId);
 852            List<FieldFormatterModel> fieldFormatters = FilterFieldFormatters(allFieldFormatters, search);
 853            fieldFormatters = SortResult(fieldFormatters, search.SortOption, sortOptions);
 754            return CreateResult(search, allFieldFormatters, fieldFormatters);
 55
 756        }
 57
 58        public async Task<FieldFormatterModel> GetFieldFormatterAsync(string projectId, string fieldFormatterName)
 159        {
 160            logger.LogDebug("Getting field formatter {FieldFormatterName} for project {ProjectId}",
 161                fieldFormatterName, projectId);
 162            if (GetProjectEntity(projectId).FieldFormatters.TryGetValue(fieldFormatterName, out FieldFormatter? fieldFor
 163            {
 164                return await Task.FromResult(FieldFormatterMapper.ToDto(fieldFormatter));
 65            }
 066            return await Task.FromException<FieldFormatterModel>(new UnknownEntityException("Field formatter", fieldForm
 167        }
 68
 69
 70        public async Task<FieldFormatterModel> CreateFieldFormatterAsync(string projectId, CreateFieldFormatterRequest c
 371        {
 372            logger.LogDebug("Creating field formatter {FieldFormatterName} of type {FieldFormatterType} for project {Pro
 373                createRequest.FieldFormatter.Name, createRequest.FieldFormatter.FormatterType, projectId);
 374            if (GetProjectEntity(projectId) is WipProject wipProject)
 375            {
 376                FieldFormatter fieldFormatter = FieldFormatterMapper.FromDto(createRequest.FieldFormatter);
 377                wipProject.AddFieldFormatter(fieldFormatter);
 378                return await Task.FromResult(FieldFormatterMapper.ToDto(fieldFormatter));
 79            }
 080            return await Task.FromException<FieldFormatterModel>(new NonWipProjectException(projectId));
 381        }
 82
 83        public async Task<FieldFormatterModel> UpdateFieldFormatterAsync(string projectId, string fieldFormatterName, Fi
 184        {
 185            logger.LogDebug("Uodating field formatter {FieldFormatterName} of type {FieldFormatterType} for project {Pro
 186                fieldFormatterName, fieldFormatterModel.FormatterType, projectId);
 187            if (GetProjectEntity(projectId) is WipProject wipProject)
 188            {
 189                FieldFormatter fieldFormatter = FieldFormatterMapper.FromDto(fieldFormatterModel);
 190                wipProject.UpdateFieldFormatter(fieldFormatterName, fieldFormatter);
 191                return await Task.FromResult(FieldFormatterMapper.ToDto(fieldFormatter));
 92            }
 093            return await Task.FromException<FieldFormatterModel>(new NonWipProjectException(projectId));
 194        }
 95
 96        public async ValueTask DeleteFieldFormatterAsync(string projectId, string fieldFormatterName)
 497        {
 498            logger.LogDebug("Deleting field formatter {FieldFormatterName} for project {ProjectId}",
 499                fieldFormatterName, projectId);
 4100            if (GetProjectEntity(projectId) is WipProject wipProject)
 3101            {
 3102                if (wipProject.RemoveFieldFormatter(fieldFormatterName))
 1103                {
 1104                    await ValueTask.CompletedTask;
 1105                    return;
 106                }
 1107                await ValueTask.FromException(new UnknownEntityException("Field formatter", fieldFormatterName));
 0108            }
 1109            await ValueTask.FromException(new NonWipProjectException(projectId));
 1110        }
 111
 112        private static List<FieldFormatterModel> FilterFieldFormatters(List<FieldFormatterModel> allFieldFormatters, Fie
 8113        {
 8114            List<FieldFormatterModel> filteredFieldDefinitions = allFieldFormatters;
 8115            if (!String.IsNullOrEmpty(search.NameFilter))
 1116            {
 12117                filteredFieldDefinitions = [.. filteredFieldDefinitions.Where(clazz => clazz.Name.Contains(search.NameFi
 1118            }
 8119            return filteredFieldDefinitions;
 8120        }
 121    }
 122}
 123