public void Upsert() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(Upsert)) .Options; using (var context = new ExpensesDbContext(options)) { var roleService = new UserRoleService(context, config); var toAdd = new UserRolePostDto { Name = "GOD" }; var toUpdate = new UserRolePostDto { Name = "Sclave Master" }; UserRole role = roleService.Create(toAdd); context.Entry(role).State = EntityState.Detached; UserRole updated = roleService.Upsert(role.Id, toUpdate); Assert.AreNotEqual(role.Name, updated.Name); } }
public UserRole Create(UserRolePostDto role) { UserRole toAdd = UserRolePostDto.ToRole(role); dbcontext.UserRoles.Add(toAdd); dbcontext.SaveChanges(); return(toAdd); }
public async Task <UserRole> AddAsync(UserRolePostDto entity) { UserRolePostDtoValidator validator = new UserRolePostDtoValidator(); ValidationResult results = validator.Validate(entity); if (!results.IsValid) { throw new ValidationException("UserRolePostDTO", string.Join(". ", results.Errors)); } return(await userRoleRepository.AddAsync(mapper.Map <UserRole>(entity))); }
public void GetById() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetById)) .Options; using (var context = new ExpensesDbContext(options)) { var roleService = new UserRoleService(context, config); var toAdd = new UserRolePostDto { Name = "GOD" }; UserRole expected = roleService.Create(toAdd); UserRole actual = roleService.GetById(expected.Id); Assert.AreEqual(expected, actual); } }
public void Delete() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(Delete)) .Options; using (var context = new ExpensesDbContext(options)) { var roleService = new UserRoleService(context, config); var toAdd = new UserRolePostDto { Name = "GOD" }; UserRole role = roleService.Create(toAdd); roleService.Delete(role.Id); Assert.IsNull(context.UserRoles.Find(role.Id)); } }
public void GetAll() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetAll)) .Options; using (var context = new ExpensesDbContext(options)) { var roleService = new UserRoleService(context, config); var first = new UserRolePostDto { Name = "God" }; var second = new UserRolePostDto { Name = "Slave Master" }; var third = new UserRolePostDto { Name = "Matroana" }; var fourth = new UserRolePostDto { Name = "Shobolan" }; var fifth = new UserRolePostDto { Name = "Tod Howard" }; roleService.Create(first); roleService.Create(second); roleService.Create(third); roleService.Create(fourth); roleService.Create(fifth); context.SaveChanges(); PaginatedList <UserRoleGetDto> populated = roleService.GetAll(1); PaginatedList <UserRoleGetDto> empty = roleService.GetAll(2); Assert.AreEqual(5, populated.Entries.Count); Assert.Zero(empty.Entries.Count); } }
public UserRole Upsert(int id, UserRolePostDto rolePostDto) { var existing = dbcontext.UserRoles.AsNoTracking().FirstOrDefault(u => u.Id == id); if (existing == null) { UserRole toAdd = UserRolePostDto.ToRole(rolePostDto); dbcontext.UserRoles.Add(toAdd); dbcontext.SaveChanges(); return(toAdd); } UserRole toUpdate = UserRolePostDto.ToRole(rolePostDto); toUpdate.Id = id; toUpdate.UserUserRoles = existing.UserUserRoles; dbcontext.UserRoles.Update(toUpdate); dbcontext.SaveChanges(); return(toUpdate); }
public void Create() { var options = new DbContextOptionsBuilder <ExpensesDbContext>() .UseInMemoryDatabase(databaseName: nameof(Create)) .Options; using (var context = new ExpensesDbContext(options)) { var roleService = new UserRoleService(context, config); var toAdd = new UserRolePostDto { Name = "GOD" }; UserRole role = roleService.Create(toAdd); Assert.AreEqual(toAdd.Name, role.Name); Assert.IsNotNull(roleService.GetById(role.Id)); } }
public async Task <IActionResult> AddNewUserRole([FromBody] UserRolePostDto userRolePostDto) { var userRoleResp = await userRoleService.AddAsync(userRolePostDto); return(CreatedAtAction("GetClient", new { id = userRoleResp.Id }, mapper.Map <UserRoleResponseDto>(userRoleResp))); }
public IActionResult Put(int id, [FromBody] UserRolePostDto role) { var result = roleService.Upsert(id, role); return(Ok(result)); }
public IActionResult Post([FromBody] UserRolePostDto role) { roleService.Create(role); return(Ok()); }