public async Task <IActionResult> SaveChangedItem([FromRoute] int id, [FromBody] PersonForUpdateModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _peopleService.UpdateAsync(id, model, string.Empty); if (!result.Result.IsValid) { return(BadRequest(result.Result)); } return(NoContent()); }
public async Task <Result <PersonModel> > UpdateAsync(int id, PersonForUpdateModel model, string userId) { PersonEntity dbEntity = _mapper.Map <PersonForUpdateModel, PersonEntity>(model); Result <PersonModel> result = await ValidateUpdateAsync(dbEntity); var modelFromRepo = await _context.People.Where(b => b.Id == id).FirstOrDefaultAsync(); if (modelFromRepo == null) { result.IsValid = false; result.Errors.Add("W bazie nie istnieje osoba o podanym identyfikatorze"); } else { Mapper.Map(model, modelFromRepo); modelFromRepo.PostalCode = !string.IsNullOrEmpty(dbEntity.PostalCode) ? new string(dbEntity.PostalCode.Where(c => Char.IsNumber(c)).ToArray()) : modelFromRepo.PostalCode; if (_context.SaveChanges() < 0) { throw new Exception($"Nie powiodła się operacja zapisu zmodyfikowanej osoby o id = {id} podczas zatwierdzania zmian na bazie danych"); } } return(result); }