| | | 1 | | using pva.SuperV.Api.Exceptions; |
| | | 2 | | using pva.SuperV.Api.Mappers; |
| | | 3 | | using pva.SuperV.Engine; |
| | | 4 | | using pva.SuperV.Engine.Exceptions; |
| | | 5 | | using pva.SuperV.Model; |
| | | 6 | | using pva.SuperV.Model.Instances; |
| | | 7 | | using pva.SuperV.Model.Services; |
| | | 8 | | |
| | | 9 | | namespace pva.SuperV.Api.Services.Instances |
| | | 10 | | { |
| | | 11 | | public class InstanceService : BaseService, IInstanceService |
| | | 12 | | { |
| | | 13 | | private readonly ILogger logger; |
| | 13 | 14 | | private readonly Dictionary<string, Comparison<InstanceModel>> sortOptions = new() |
| | 13 | 15 | | { |
| | 0 | 16 | | { "name", new Comparison<InstanceModel>((a, b) => a.Name.CompareTo(b.Name)) } |
| | 13 | 17 | | }; |
| | | 18 | | |
| | 13 | 19 | | public InstanceService(ILoggerFactory loggerFactory) |
| | 13 | 20 | | { |
| | 13 | 21 | | this.logger = loggerFactory.CreateLogger(this.GetType()); |
| | 13 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<List<InstanceModel>> GetInstancesAsync(string projectId) |
| | 9 | 25 | | { |
| | 9 | 26 | | logger.LogDebug("Getting instances for project {ProjectId}", |
| | 9 | 27 | | projectId); |
| | 9 | 28 | | if (GetProjectEntity(projectId) is RunnableProject runnableProject) |
| | 9 | 29 | | { |
| | 9 | 30 | | return await Task.FromResult(runnableProject.Instances.Values.Select(InstanceMapper.ToDto).ToList()); |
| | | 31 | | } |
| | 0 | 32 | | return await Task.FromException<List<InstanceModel>>(new NonRunnableProjectException(projectId)); |
| | 9 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<PagedSearchResult<InstanceModel>> SearchInstancesAsync(string projectId, InstancePagedSearchRe |
| | 8 | 36 | | { |
| | 8 | 37 | | logger.LogDebug("Searching instances for project {ProjectId} with filter {NameFilter} class {ClassNameFilter |
| | 8 | 38 | | projectId, search.NameFilter, search.ClassName, search.PageNumber, search.PageSize); |
| | 8 | 39 | | List<InstanceModel> allInstances = await GetInstancesAsync(projectId); |
| | 8 | 40 | | List<InstanceModel> projects = FilterInstances(projectId, allInstances, search); |
| | 8 | 41 | | projects = SortResult(projects, search.SortOption, sortOptions); |
| | 7 | 42 | | return CreateResult(search, allInstances, projects); |
| | | 43 | | |
| | 7 | 44 | | } |
| | | 45 | | |
| | | 46 | | public async Task<InstanceModel> GetInstanceAsync(string projectId, string instanceName) |
| | 2 | 47 | | { |
| | 2 | 48 | | logger.LogDebug("Getting instance {InstanceName} for project {ProjectId}", |
| | 2 | 49 | | instanceName, projectId); |
| | 2 | 50 | | if (GetProjectEntity(projectId) is RunnableProject runnableProject) |
| | 2 | 51 | | { |
| | 2 | 52 | | if (runnableProject.Instances.TryGetValue(instanceName, out Instance? instance)) |
| | 1 | 53 | | { |
| | 1 | 54 | | return await Task.FromResult(InstanceMapper.ToDto(instance)); |
| | | 55 | | } |
| | 1 | 56 | | return await Task.FromException<InstanceModel>(new UnknownEntityException("Instance", instanceName)); |
| | | 57 | | } |
| | 0 | 58 | | return await Task.FromException<InstanceModel>(new NonRunnableProjectException(projectId)); |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task<InstanceModel> CreateInstanceAsync(string projectId, InstanceModel createRequest, bool addToRu |
| | 4 | 62 | | { |
| | 4 | 63 | | logger.LogDebug("Creating instance {InstanceName} of class {ClassName} with fields {Fields} for project {Pro |
| | 6 | 64 | | createRequest.Name, createRequest.ClassName, String.Join(",", createRequest.Fields.Select(field => strin |
| | 4 | 65 | | if (GetProjectEntity(projectId) is RunnableProject runnableProject) |
| | 4 | 66 | | { |
| | 4 | 67 | | Instance? createdInstance = runnableProject.CreateInstance(createRequest.ClassName, createRequest.Name, |
| | 4 | 68 | | createRequest.Fields.ForEach(fieldModel => |
| | 2 | 69 | | { |
| | 2 | 70 | | IField? field = createdInstance!.GetField(fieldModel.Name) ?? throw new UnknownEntityException("Fiel |
| | 2 | 71 | | FieldValueMapper.SetFieldValue(field, fieldModel.FieldValue); |
| | 6 | 72 | | }); |
| | 4 | 73 | | return await Task.FromResult(InstanceMapper.ToDto(createdInstance!)); |
| | | 74 | | } |
| | 0 | 75 | | return await Task.FromException<InstanceModel>(new NonRunnableProjectException(projectId)); |
| | 4 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async ValueTask DeleteInstanceAsync(string projectId, string instanceName) |
| | 0 | 79 | | { |
| | 0 | 80 | | logger.LogDebug("Deleting instance {InstanceName} for project {ProjectId}", |
| | 0 | 81 | | instanceName, projectId); |
| | 0 | 82 | | if (GetProjectEntity(projectId) is RunnableProject runnableProject) |
| | 0 | 83 | | { |
| | 0 | 84 | | runnableProject.RemoveInstance(instanceName); |
| | 0 | 85 | | await ValueTask.CompletedTask; |
| | 0 | 86 | | return; |
| | | 87 | | } |
| | 0 | 88 | | await ValueTask.FromException(new NonRunnableProjectException(projectId)); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | private static List<InstanceModel> FilterInstances(string projectId, List<InstanceModel> allInstances, InstanceP |
| | 8 | 92 | | { |
| | 8 | 93 | | List<InstanceModel> filteredInstances = allInstances; |
| | 8 | 94 | | if (!String.IsNullOrEmpty(search.ClassName)) |
| | 1 | 95 | | { |
| | 1 | 96 | | Dictionary<string, bool> classNameMatches = []; |
| | 4 | 97 | | filteredInstances = [.. filteredInstances.Where(instance => FilterInstanceClass(projectId, search.ClassN |
| | 1 | 98 | | } |
| | 8 | 99 | | if (!String.IsNullOrEmpty(search.NameFilter)) |
| | 1 | 100 | | { |
| | 2 | 101 | | filteredInstances = [.. filteredInstances.Where(instance => instance.Name.Contains(search.NameFilter))]; |
| | 1 | 102 | | } |
| | 8 | 103 | | return filteredInstances; |
| | 8 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static bool FilterInstanceClass(string projectId, string searchedClassName, InstanceModel instance, ref |
| | 3 | 107 | | { |
| | 3 | 108 | | if (classNameMatches.TryGetValue(instance.ClassName, out bool isClassNameMatching)) |
| | 1 | 109 | | { |
| | 1 | 110 | | return isClassNameMatching; |
| | | 111 | | } |
| | 2 | 112 | | Class? clazz = GetClassEntity(projectId, instance.ClassName); |
| | 2 | 113 | | List<string> classInheritance = []; |
| | 4 | 114 | | while (clazz != null) |
| | 3 | 115 | | { |
| | 3 | 116 | | isClassNameMatching = clazz.Name.Equals(searchedClassName); |
| | 3 | 117 | | classInheritance.Add(clazz.Name); |
| | 3 | 118 | | if (isClassNameMatching) |
| | 1 | 119 | | { |
| | 1 | 120 | | break; |
| | | 121 | | } |
| | 2 | 122 | | clazz = clazz.BaseClass; |
| | 2 | 123 | | } |
| | 12 | 124 | | foreach (string className in classInheritance) |
| | 3 | 125 | | { |
| | 3 | 126 | | classNameMatches[className] = isClassNameMatching; |
| | 3 | 127 | | } |
| | 2 | 128 | | return isClassNameMatching; |
| | 3 | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |