public async Task <IActionResult> Post([FromBody] AddUpdateHotelViewModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newHotel = new Hotel(); _mapper.Map(model, newHotel); _hotelRepository.AddEntity(newHotel); if (await _hotelRepository.SaveAsync()) { model.Id = newHotel.Id; var newUri = Url.Link("GetHotel", new { id = model.Id }); return(Created(newUri, model)); } else { _message = "Nie udało się dodać nowego hotelu"; return(BadRequest(_message)); } } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <IActionResult> Put(int id, [FromBody] AddUpdateHotelViewModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var hotelEntity = await _hotelRepository.GetEntityById(id); if (hotelEntity == null) { _message = $"Hotel o identyfikatorze {id} nie istnieje"; return(NotFound(_message)); } _mapper.Map(model, hotelEntity); _hotelRepository.UpdateEntity(hotelEntity); if (await _hotelRepository.SaveAsync()) { var updatedHotel = _mapper.Map <Hotel, AddUpdateHotelViewModel>(hotelEntity); return(Ok(updatedHotel)); } else { _message = "Nie udało się zaktualizować hotelu"; return(BadRequest(_message)); } } catch (Exception e) { return(BadRequest(e.Message)); } }