using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.Role; using GxPress.Result.Role; using Microsoft.Extensions.Options; using Datory; namespace GxPress.Repository.Implement { public class RoleRepository : IRoleRepository { private readonly Repository _repository; public RoleRepository(IOptionsMonitor dbOptionsAccessor) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(Role role) { return await _repository.InsertAsync(role); } public async Task IsNameExistsAsync(string name) { return await _repository.ExistsAsync(Q.Where(nameof(Role.Name), name)); } public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task GetNameAsync(int id) { if (id <= 0) return string.Empty; return await _repository.GetAsync(Q .Select(nameof(Role.Name)) .Where(nameof(Role.Id), id) ); } public async Task> GetListAsync(int groupId) { return await _repository.GetAllAsync(Q.Where(nameof(Role.GroupId), groupId).OrderBy(nameof(Role.Sort))); } /// /// 添加 /// /// /// public async Task AddAsync(RoleAddRequest request) { var role = new Role { GroupId = request.GroupId, Name = request.Name, Sort = request.Sort }; role.Id = await InsertAsync(role); return new RoleDetailResult { Id = role.Id, Name = role.Name, GroupId = role.GroupId, Sort = role.Sort }; } /// /// 修改 /// /// /// /// public async Task UpdateAsync(int id, RoleAddRequest request) { var role = await GetAsync(id); if (role == null) throw new BusinessException("该角色不存在"); role.Name = request.Name; role.GroupId = request.GroupId; role.Sort = request.Sort; return await _repository.UpdateAsync(role); } /// /// 删除 /// /// /// public async Task DeleteAsync(int id) { var role = await GetAsync(id); if (role == null) throw new BusinessException("该角色不存在"); return await _repository.DeleteAsync(id); } /// /// 详情 /// /// /// public async Task GetDetailAsync(int id) { var role = await GetAsync(id); if (role == null) throw new BusinessException("该角色不存在"); return new RoleDetailResult { Id = role.Id, Name = role.Name, GroupId = role.GroupId }; } /// /// 列表 /// /// /// public async Task> GetDetailListAsync(int groupId) { var list = await GetListAsync(groupId); var result = list.Select(item => new RoleDetailResult { Id = item.Id, Name = item.Name, GroupId = item.GroupId, Sort = item.Sort }).ToList(); return result; } } }