using DbContextLib; using DTO; using Entities; using Microsoft.EntityFrameworkCore; using StubbedContextLib; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DTOToEntity { public class RoleService : IService { private readonly UnitOfWork _context = new UnitOfWork(); public RoleService() { } public RoleService(StubbedContext context) { _context = new UnitOfWork(context); } public RoleService(UnitOfWork context) { _context = context; } public async Task Add(RoleDTO role) { var roleEntity = role.ToEntity(); _context.RoleRepository.Insert(roleEntity); await _context.SaveChangesAsync(); return role; } public async Task Delete(object id) { RoleEntity role = _context.RoleRepository.GetById((long)id); if (role == null) { throw new Exception("Role not found"); } _context.RoleRepository.Delete((long)id); await _context.SaveChangesAsync(); return role.ToDTO(); } public async Task GetById(object id) { RoleEntity? role = _context.RoleRepository.GetById((long)id); if (role == null) { throw new Exception("Role not found"); } return role.ToDTO(); } public async Task> Gets(int index, int count) { IEnumerable roles = _context.RoleRepository.GetItems(index, count); return new PageResponse(roles.ToList().Select(r => r.ToDTO()), _context.RoleRepository.GetItems(0,1000000000).Count()); } public async Task Update(RoleDTO role) { if (role == null) { throw new ArgumentNullException(); } var roleEntity = _context.RoleRepository.GetById(role.Id); if (roleEntity != null) { throw new Exception("role not found"); } roleEntity.Name = role.Name; _context.RoleRepository.Update(roleEntity); await _context.SaveChangesAsync(); return roleEntity.ToDTO(); } } }