From 317dc30b1726807bf4789e33dccc7d77f1718658 Mon Sep 17 00:00:00 2001 From: Antinew <2357729423@qq.com> Date: Sat, 14 Jun 2025 15:33:44 +0800 Subject: [PATCH] 1 --- .gitignore | 1 + .../Properties/launchSettings.json | 2 +- .../Services/App/DictionaryService.cs | 59 +++++++++++++++-- .../Interface/InterfaceCategoryService.cs | 65 +++++++++++++++++++ .../Entitys/InterfaceCategoryEntity.cs | 21 ++++++ 5 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs create mode 100644 src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs diff --git a/.gitignore b/.gitignore index 6163587..4f3edf7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ riderModule.iml .idea/ /.vs /Fake.sln.DotSettings.user +/InterfaceForward.sln.DotSettings.user diff --git a/src/InterfaceForward.Api/Properties/launchSettings.json b/src/InterfaceForward.Api/Properties/launchSettings.json index cb556c1..8c84019 100644 --- a/src/InterfaceForward.Api/Properties/launchSettings.json +++ b/src/InterfaceForward.Api/Properties/launchSettings.json @@ -14,7 +14,7 @@ "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger/index.html", - "applicationUrl": "http://localhost:5145", + "applicationUrl": "http://localhost:8888", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/src/InterfaceForward.Application/Services/App/DictionaryService.cs b/src/InterfaceForward.Application/Services/App/DictionaryService.cs index cb3330a..2714e60 100644 --- a/src/InterfaceForward.Application/Services/App/DictionaryService.cs +++ b/src/InterfaceForward.Application/Services/App/DictionaryService.cs @@ -1,4 +1,6 @@ -using InterfaceForward.Repositories.Dictionary.Services; +using System.ComponentModel; +using System.Reflection; +using InterfaceForward.Repositories.Dictionary.Services; using Microsoft.AspNetCore.Mvc; namespace InterfaceForward.Application.Services.App @@ -6,11 +8,11 @@ namespace InterfaceForward.Application.Services.App /// /// 字典服务 /// - [Route("Dictionary")] [ApiExplorerSettings(GroupName = "应用服务")] public class DictionaryService : ApplicationService { private readonly IDictionaryRepository _dictionaryRepository; + private static readonly Dictionary> EnumCache = new(); /// /// @@ -20,18 +22,63 @@ namespace InterfaceForward.Application.Services.App { _dictionaryRepository = dictionaryRepository; } - - #region Get + + /// /// 列表 /// /// 类型 /// - [HttpGet("GetList")] + [HttpGet("Dictionary/GetList")] public async Task GetList(string type) { return await _dictionaryRepository.AsQueryable().Where(x => x.Type == type).Select(x => new { Key = x.Id, Value = x.Name }).ToListAsync(); } - #endregion + + /// + /// 获取枚举下拉列表 + /// + /// 枚举类型名称 + /// + [HttpGet("Dictionary/GetKvsByEnumTypeName")] + public Dictionary GetKvsByEnumTypeName(string enumTypeName) + { + // Check if the type is already cached + if (EnumCache.TryGetValue(enumTypeName, out var res)) + { + return res; + } + + // Load the assembly + var assemblyName = "InterfaceForward.Domain.Shared"; + var assembly = Assembly.Load(assemblyName); + if (assembly == null) + { + throw new ArgumentException($"Assembly '{assemblyName}' could not be loaded."); + } + + // Get the enum type + var enumType = assembly.GetType("InterfaceForward.Domain.Shared.Enum." + enumTypeName); + if (enumType == null || !enumType.IsEnum) + { + throw new ArgumentException($"The type '{enumTypeName}' is not a valid enum."); + } + + // Retrieve the descriptions and values + var enumDict = new Dictionary(); + foreach (var value in enumType.GetEnumValues()) + { + var name = value.ToString(); + var fieldInfo = enumType.GetField(name); + var descriptionAttribute = fieldInfo.GetCustomAttribute(); + string description = descriptionAttribute != null ? descriptionAttribute.Description : name; + enumDict[description] = (int)value; + } + + // Cache the result + EnumCache[enumTypeName] = enumDict; + + return enumDict; + } } } diff --git a/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs b/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs new file mode 100644 index 0000000..fc006df --- /dev/null +++ b/src/InterfaceForward.Application/Services/Interface/InterfaceCategoryService.cs @@ -0,0 +1,65 @@ +using InterfaceForward.Repositories; +using InterfaceForward.Repositories.Interface.Entitys; +using Microsoft.AspNetCore.Mvc; + +namespace InterfaceForward.Application.Services.Interface; + +/// +/// 接口分类 +/// +[ApiExplorerSettings(GroupName = "接口服务")] +public class InterfaceCategoryService(IBasicRepository categoryRepository) : ApplicationService +{ + /// + /// 添加接口分类 + /// + /// 父级id,没有就传0,表示顶级 + /// + [HttpPost("Interface/AddInterfaceCategory")] + public async Task AddInterfaceCategoryInput(int pId, string name) + { + if (await categoryRepository.AnyAsync(x => x.PId == pId && x.Name == name)) + { + throw new BusinessException("重复添加"); + } + + await categoryRepository.InsertAsync(new InterfaceCategoryEntity + { + PId = pId, + Name = name + }); + } + + /// + /// 更新接口分类 + /// + /// id + /// + [HttpPut("Interface/UpdateInterfaceCategoryInput")] + public async Task UpdateInterfaceCategoryInput(int id, string name) + { + var category = await categoryRepository.FirstOrDefaultAsync(x => x.Id == id); + if (category == null) + { + throw new BusinessException("分类不存在"); + } + + category.Name = name; + await categoryRepository.UpdateAsync(category); + } + + /// + /// 删除接口分类 + /// + /// id + [HttpDelete("Interface/DeleteInterfaceCategoryInput")] + public async Task DeleteInterfaceCategoryInput(int id) + { + var category = await categoryRepository.FirstOrDefaultAsync(x => x.Id == id); + if (category == null) + { + throw new BusinessException("分类不存在"); + } + await categoryRepository.SoftDeleteAsync(x => x.Id == id); + } +} \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs b/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs new file mode 100644 index 0000000..55079ca --- /dev/null +++ b/src/InterfaceForward.Repositories/Interface/Entitys/InterfaceCategoryEntity.cs @@ -0,0 +1,21 @@ +namespace InterfaceForward.Repositories.Interface.Entitys +{ + /// + /// 接口分类表 + /// + [SugarTable("t_interface_category")] + public class InterfaceCategoryEntity : FullAuditedAggregateRoot + { + /// + /// 父分类id + /// + [SugarColumn(ColumnName = "PId")] + public int PId { get; set; } + + /// + /// 分类名称 + /// + [SugarColumn(ColumnName = "Name")] + public string Name { get; set; } = null!; + } +} \ No newline at end of file