< Summary - pva.SuperV

Information
Class: pva.SuperV.Api.Services.FieldDefinitions.FieldDefinitionService
Assembly: pva.SuperV.Api
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/FieldDefinitions/FieldDefinitionService.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
84%
Covered lines: 78
Uncovered lines: 14
Coverable lines: 92
Total lines: 134
Line coverage: 84.7%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetFieldsAsync()100%11100%
GetFieldAsync()50%2287.5%
SearchFieldsAsync()100%11100%
CreateFieldsAsync()50%2252.38%
UpdateFieldAsync()66.66%6692.3%
DeleteFieldAsync()50%2290%
FilterFieldDefinitions(...)100%22100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/FieldDefinitions/FieldDefinitionService.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.FieldDefinitions;
 8using pva.SuperV.Model.Services;
 9
 10namespace pva.SuperV.Api.Services.FieldDefinitions
 11{
 12    public class FieldDefinitionService : BaseService, IFieldDefinitionService
 13    {
 14        private readonly ILogger logger;
 1315        private readonly Dictionary<string, Comparison<FieldDefinitionModel>> sortOptions = new()
 1316            {
 3417                { "name", new Comparison<FieldDefinitionModel>((a, b) => a.Name.CompareTo(b.Name)) }
 1318            };
 19
 1320        public FieldDefinitionService(ILoggerFactory loggerFactory)
 1321        {
 1322            this.logger = loggerFactory.CreateLogger(this.GetType());
 1323        }
 24
 25        public async Task<List<FieldDefinitionModel>> GetFieldsAsync(string projectId, string className)
 1126        {
 1127            logger.LogDebug("Getting fields for {ClassName} for {ProjectId}",
 1128                className, projectId);
 21629            return await Task.FromResult(GetClassEntity(projectId, className).FieldDefinitions.Values.Select(field => Fi
 1130        }
 31
 32        public async Task<FieldDefinitionModel> GetFieldAsync(string projectId, string className, string fieldName)
 133        {
 134            logger.LogDebug("Getting field {FieldName} for {ClassName} for {ProjectId}",
 135                fieldName, className, projectId);
 136            if (GetClassEntity(projectId, className).FieldDefinitions.TryGetValue(fieldName, out IFieldDefinition? field
 137            {
 138                return await Task.FromResult(FieldDefinitionMapper.ToDto(fieldDefinition));
 39            }
 040            return await Task.FromException<FieldDefinitionModel>(new UnknownEntityException("Field", fieldName));
 141        }
 42
 43        public async Task<PagedSearchResult<FieldDefinitionModel>> SearchFieldsAsync(string projectId, string className,
 944        {
 945            logger.LogDebug("Searching for fields with filter {NameFilter} for {ClassName} for {ProjectId} page number {
 946                search.NameFilter, className, projectId, search.PageNumber, search.PageSize);
 947            List<FieldDefinitionModel> allFieldDefinitions = await GetFieldsAsync(projectId, className);
 948            List<FieldDefinitionModel> fieldDefinitions = FilterFieldDefinitions(allFieldDefinitions, search);
 949            fieldDefinitions = SortResult(fieldDefinitions, search.SortOption, sortOptions);
 850            return CreateResult(search, allFieldDefinitions, fieldDefinitions);
 851        }
 52
 53
 54        public async Task<List<FieldDefinitionModel>> CreateFieldsAsync(string projectId, string className, List<FieldDe
 455        {
 456            logger.LogDebug("Creating fields for {ClassName} for {ProjectId}",
 457                className, projectId);
 458            if (GetProjectEntity(projectId) is WipProject wipProject)
 459            {
 460                List<FieldDefinitionModel> createdFieldDefinitions = [];
 461                Class clazz = GetClassEntity(wipProject, className);
 62                try
 463                {
 464                    createRequests.ForEach(fieldDefinition =>
 8265                    {
 8266                        logger.LogDebug("Creating field {FieldName} of type {FieldType} for {ClassName} for {ProjectId}"
 8267                            fieldDefinition.Name, fieldDefinition.FieldType, className, projectId);
 8268                        FieldFormatter? fieldFormatter = null;
 8269                        if (fieldDefinition.ValueFormatter is not null)
 1770                        {
 1771                            fieldFormatter = wipProject.GetFormatter(fieldDefinition.ValueFormatter);
 1772                        }
 8273                        createdFieldDefinitions.Add(FieldDefinitionMapper.ToDto(clazz.AddField(FieldDefinitionMapper.Fro
 8674                    });
 475                    return await Task.FromResult(createdFieldDefinitions);
 76                }
 077                catch (SuperVException e)
 078                {
 79                    // If exception while creatig one of the fields, remove all the already created fields.
 80                    try
 081                    {
 082                        createdFieldDefinitions.ForEach(fieldDefinition => clazz.RemoveField(fieldDefinition.Name));
 083                    }
 084                    catch (SuperVException)
 085                    {
 86                        // Ignore execption while deleting
 087                    }
 088                    return await Task.FromException<List<FieldDefinitionModel>>(e);
 89                }
 090            }
 091            return await Task.FromException<List<FieldDefinitionModel>>(new NonWipProjectException(projectId));
 492        }
 93
 94        public async Task<FieldDefinitionModel> UpdateFieldAsync(string projectId, string className, string fieldName, F
 395        {
 396            logger.LogDebug("Updating field {FieldName} of type {FieldType} for {ClassName} for {ProjectId}",
 397                updateRequest.Name, updateRequest.FieldType, className, projectId);
 398            if (GetProjectEntity(projectId) is WipProject wipProject)
 399            {
 3100                _ = GetClassEntity(wipProject, className);
 3101                if (updateRequest.Name?.Equals(fieldName) != false)
 2102                {
 2103                    IFieldDefinition fieldDefinitionUpdate = FieldDefinitionMapper.FromDto(updateRequest);
 2104                    return await Task.FromResult(FieldDefinitionMapper.ToDto(wipProject.UpdateField(className, fieldName
 105                }
 1106                return await Task.FromException<FieldDefinitionModel>(new EntityPropertyNotChangeableException("field", 
 107            }
 0108            return await Task.FromException<FieldDefinitionModel>(new NonWipProjectException(projectId));
 1109        }
 110
 111        public async ValueTask DeleteFieldAsync(string projectId, string className, string fieldName)
 1112        {
 1113            logger.LogDebug("Deleting field {FieldName} for {ClassName} for {ProjectId}",
 1114                fieldName, className, projectId);
 1115            if (GetProjectEntity(projectId) is WipProject wipProject)
 1116            {
 1117                GetClassEntity(wipProject, className).RemoveField(fieldName);
 1118                await ValueTask.CompletedTask;
 1119                return;
 120            }
 0121            await ValueTask.FromException(new NonWipProjectException(projectId));
 1122        }
 123        private static List<FieldDefinitionModel> FilterFieldDefinitions(List<FieldDefinitionModel> allFieldDefinitions,
 9124        {
 9125            List<FieldDefinitionModel> filteredClasses = allFieldDefinitions;
 9126            if (!String.IsNullOrEmpty(search.NameFilter))
 2127            {
 68128                filteredClasses = [.. filteredClasses.Where(fieldDefinition => fieldDefinition.Name.Contains(search.Name
 2129            }
 9130            return filteredClasses;
 9131        }
 132    }
 133}
 134