/// <inheritdoc /> public async Task <HouseDto> Update(HouseUpdateDto request) { //TODO need implement validator using (var db = new ApplicationDbContext()) { var houses = db .Houses.Include(h => h.Subnet) .AsQueryable(); var toUpdate = houses.FirstOrDefault(h => h.Id == request.Id); if (toUpdate == null) { throw new InternalExceptions.NotFoundException($"house with id {request.Id}"); } toUpdate.Address = request.Address; toUpdate.Comment = request.Comment; toUpdate.Number = request.Number; toUpdate.AdditionalNumber = request.AdditionalNumber; toUpdate.SubnetId = request.SubnetId; var result = db.Houses.Update(toUpdate); await db.SaveChangesAsync(); return(_mapper.ToDto(result.Entity)); } }
public IActionResult Update(int id, [FromBody] HouseUpdateDto houseDto) { if (houseDto == null) { return(BadRequest()); } var existingHouseEntity = _houseRepository.GetSingle(id); if (existingHouseEntity == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(houseDto, existingHouseEntity); _houseRepository.Update(existingHouseEntity); bool result = _houseRepository.Save(); if (!result) { throw new Exception($"something went wrong when updating the house with id: {id}"); } return(Ok(Mapper.Map <HouseDto>(existingHouseEntity))); }
public async Task <JsonResult> House([FromBody] HouseUpdateDto request) { var result = await _service.Update(request); return(Json(result)); }