using AutoMapper; using Microsoft.EntityFrameworkCore; using OrdersManagementDataModel.Dtos; using OrdersManagementDataModel.Entities; namespace OrdersManagementDataModel.Services; public class RoleService(OrdersManagementDbContext context, IMapper mapper) : IRoleService { public async Task> GetRoles() { IList roles = await context.Roles.Select(x => mapper.Map(x)).ToListAsync(); return roles; } public async Task GetRoleById(Guid id) { RoleDto? role = await context.Roles.Where(x => x.RowPointer == id) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); return role; } public async Task GetRoleByName(string name) { RoleDto? role = await context.Roles.Where(x => x.Name == name) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); return role; } public async Task AddRole(RoleDto roleDto) { Role role = new Role { Name = roleDto.Name, RowPointer = Guid.NewGuid() }; context.Roles.Add(role); return await context.SaveChangesAsync(); } public async Task DeleteRole(Guid id) { Role? role = await context.Roles.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null; if (role == null) return 0; context.Roles.Remove(role); return await context.SaveChangesAsync(); } }