| | | 1 | | using pva.SuperV.Api.Exceptions; |
| | | 2 | | using pva.SuperV.Engine; |
| | | 3 | | using pva.SuperV.Engine.Exceptions; |
| | | 4 | | using pva.SuperV.Model; |
| | | 5 | | |
| | | 6 | | namespace pva.SuperV.Api.Services |
| | | 7 | | { |
| | | 8 | | public abstract class BaseService |
| | | 9 | | { |
| | | 10 | | protected static Project GetProjectEntity(string projectId) |
| | 245 | 11 | | { |
| | 245 | 12 | | if (Project.Projects.TryGetValue(projectId, out Project? project)) |
| | 243 | 13 | | { |
| | 243 | 14 | | return project; |
| | | 15 | | } |
| | 2 | 16 | | throw new UnknownEntityException("Project", projectId); |
| | 243 | 17 | | } |
| | | 18 | | |
| | | 19 | | protected static Class GetClassEntity(string projectId, string className) |
| | 14 | 20 | | { |
| | 14 | 21 | | return GetClassEntity(GetProjectEntity(projectId), className); |
| | 13 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected static Class GetClassEntity(Project project, string className) |
| | 31 | 25 | | { |
| | 31 | 26 | | if (project.Classes.TryGetValue(className, out Class? clazz)) |
| | 30 | 27 | | { |
| | 30 | 28 | | return clazz; |
| | | 29 | | } |
| | 1 | 30 | | throw new UnknownEntityException("Class", className); |
| | 30 | 31 | | } |
| | | 32 | | |
| | | 33 | | protected static IFieldDefinition GetFieldDefinitionEntity(Project project, string className, string fieldName) |
| | 2 | 34 | | { |
| | 2 | 35 | | return GetFieldDefinitionEntity(GetClassEntity(project, className), fieldName); |
| | 2 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected static IFieldDefinition GetFieldDefinitionEntity(Class clazz, string fieldName) |
| | 9 | 39 | | { |
| | 9 | 40 | | if (clazz.FieldDefinitions.TryGetValue(fieldName, out IFieldDefinition? fieldDefinition)) |
| | 9 | 41 | | { |
| | 9 | 42 | | return fieldDefinition; |
| | | 43 | | } |
| | 0 | 44 | | throw new UnknownEntityException("Field", fieldName); |
| | 9 | 45 | | } |
| | | 46 | | |
| | | 47 | | protected static IField GetFieldEntity(string projectId, string instanceName, string fieldName) |
| | 135 | 48 | | { |
| | 135 | 49 | | if (GetProjectEntity(projectId) is RunnableProject runnableProject) |
| | 135 | 50 | | { |
| | 135 | 51 | | Instance instance = runnableProject.GetInstance(instanceName); |
| | 135 | 52 | | return instance.GetField(fieldName); |
| | | 53 | | } |
| | 0 | 54 | | throw new NonRunnableProjectException(projectId); |
| | 135 | 55 | | } |
| | | 56 | | |
| | | 57 | | protected static PagedSearchResult<T> CreateResult<T>(PagedSearchRequest search, List<T> allEntities, List<T> fi |
| | 35 | 58 | | => new(search.PageNumber, search.PageSize, allEntities.Count, |
| | 35 | 59 | | [.. filteredEntities |
| | 35 | 60 | | .Skip((search.PageNumber - 1) * search.PageSize) |
| | 35 | 61 | | .Take(search.PageSize)]); |
| | | 62 | | |
| | | 63 | | protected static List<T> SortResult<T>(List<T> entities, string? sortOption, Dictionary<string, Comparison<T>> s |
| | 40 | 64 | | { |
| | 40 | 65 | | if (String.IsNullOrEmpty(sortOption)) |
| | 25 | 66 | | { |
| | 25 | 67 | | return entities; |
| | | 68 | | } |
| | 15 | 69 | | string actualSortOption = sortOption.StartsWith('-') ? sortOption[1..] : sortOption; |
| | 15 | 70 | | if (sortOptions.TryGetValue(actualSortOption, out Comparison<T>? comparison)) |
| | 10 | 71 | | { |
| | 10 | 72 | | entities.Sort(comparison); |
| | 10 | 73 | | if (sortOption.StartsWith('-')) |
| | 5 | 74 | | { |
| | 5 | 75 | | entities.Reverse(); |
| | 5 | 76 | | } |
| | 10 | 77 | | } |
| | | 78 | | else |
| | 5 | 79 | | { |
| | 5 | 80 | | throw new InvalidSortOptionException(sortOption, [.. sortOptions!.Keys]); |
| | | 81 | | } |
| | 10 | 82 | | return entities; |
| | 35 | 83 | | } |
| | | 84 | | |
| | | 85 | | |
| | | 86 | | } |
| | | 87 | | } |