MenusRepository.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Repository.Interface.Menus;
  8. using Microsoft.Extensions.Options;
  9. using System.Linq;
  10. namespace GxPress.Repository.Implement.Menus
  11. {
  12. public class MenusRepository : IMenusRepository
  13. {
  14. private readonly Repository<Entity.Menus.Menus> _repository;
  15. private readonly IMapper _mapper;
  16. public MenusRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  17. {
  18. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  19. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  20. _repository = new Repository<Entity.Menus.Menus>(database);
  21. _mapper = mapper;
  22. }
  23. public IDatabase Database => _repository.Database;
  24. public string TableName => _repository.TableName;
  25. public List<TableColumn> TableColumns => _repository.TableColumns;
  26. public async Task<int> InsertAsync(Entity.Menus.Menus model)
  27. {
  28. return await _repository.InsertAsync(model);
  29. }
  30. public async Task<bool> UpdateAsync(Entity.Menus.Menus model)
  31. {
  32. return await _repository.UpdateAsync(model);
  33. }
  34. public async Task<bool> DeleteAsync(int id)
  35. {
  36. return await _repository.DeleteAsync(id);
  37. }
  38. public async Task<IEnumerable<Entity.Menus.Menus>> GetAllAsync()
  39. {
  40. var all = await _repository.GetAllAsync();
  41. var list = new List<Entity.Menus.Menus>();
  42. list = all.Where(n => n.ParentId == 0).ToList();
  43. foreach (var item in list)
  44. {
  45. item.Children = all.Where(n => n.ParentId == item.Id).ToList();
  46. }
  47. return list;
  48. }
  49. public async Task<IEnumerable<Entity.Menus.Menus>> GetAllAsync(List<int> menusIds)
  50. {
  51. var all = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id), menusIds));
  52. var list = new List<Entity.Menus.Menus>();
  53. list = all.Where(n => n.ParentId == 0).ToList();
  54. foreach (var item in list)
  55. {
  56. item.Children = all.Where(n => n.ParentId == item.Id).ToList();
  57. }
  58. return list;
  59. }
  60. public async Task<IEnumerable<Entity.Menus.Menus>> GetAllMenusAsync(List<int> menusIds)
  61. {
  62. var all = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id), menusIds));
  63. return all;
  64. }
  65. }
  66. }