public async Task <ActionResult <UserDTO> > EditUser(Guid userId, UserDTO userDTO) { try { //check object if (userDTO == null || userId == null) { return(BadRequest()); } var newUser = mapper.Map <User>(userDTO); newUser.Id = userId; if (!await repo.Exists(newUser, userId)) { return(NotFound("User not found")); } ////get old object //User olduser = await repo.GetAsyncByGuid(userId); //if (olduser == null) return NotFound("User not found"); //update await repo.Update(newUser, userId); return(NoContent()); } catch (Exception ex) { logger.LogError(ex.Message); return(BadRequest("User could not be updated.")); } }
public async Task <IActionResult> PutCategory(int id, CategoryDTO categoryDTO) { //1. altijd null check naast supplementaire Id check if (categoryDTO == null || id != categoryDTO.Id) { return(BadRequest()); } //2. mapping var category = mapper.Map <Category>(categoryDTO); if (category == null) { return(BadRequest(new { Message = "Onvoldoende data bij de categorie" })); } //3. validatie if (!ModelState.IsValid) { return(BadRequest(new { Message = $"Geen geldige input. {ModelState}" })); } // _context.Entry(category).State = EntityState.Modified; try { Category categorySearch = (await genericRepo.GetByExpressionAsync(c => c.Id == categoryDTO.Id)).FirstOrDefault(); await genericRepo.Update(mapper.Map <Category>(categoryDTO), id); } catch (DbUpdateConcurrencyException) { if (!genericRepo.Exists(category, id).Result) { return(NotFound()); } else { return(RedirectToAction("HandleErrorCode", "Error", new { statusCode = 400, errorMessage = $"De categorie '{category.CategoryName}' werd niet aangepast." })); } } return(NoContent()); }