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