public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { // Validate incoming JSON Form Data for Creating Hotel // ModelState IsValid checks if all the required data for CreateHotelDTO is in the JSON request if (!ModelState.IsValid) { _logger.LogError($"Invalid POST attempt in {nameof(CreateHotel)}"); return(BadRequest(ModelState)); } try { var hotel = _imapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); // FOR POST or any API Methods that changes the database we need to call Save or commit the changes to the database. await _unitOfWork.Save(); // note that after Save() the "hotel" which is of type DTO will have // the property ".id" which we can then send back to the client return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotelDTO)); // With "GetHote" in the CreatedAtRoute function we need to put a Name on the GET => GetHotel Data Annotation // As you see above we declared and added this Name as a nickname or identifier for the other Route here in HotelController // [HttpGet("{id:int}", Name = "GetHotel")] } catch (Exception ex) { _logger.LogError(ex, $"Something went wrong in the {nameof(CreateHotel)}"); return(StatusCode(500, "Internal server Error. Please try again Later.")); } }
// int id will be passed from the FOrm JSON body of the client to the CreateHotel DTO public async Task <IActionResult> UpdateHotel(int id, [FromBody] CreateHotelDTO hotelDTO) { if (!ModelState.IsValid || id < 1) { _logger.LogError($"Invalid UPDATE attempt in {nameof(UpdateHotel)}"); return(BadRequest(ModelState)); } try { var hotel = await _unitOfWork.Hotels.Get(q => q.Id == id); if (hotel == null) { _logger.LogError($"Invalid UPDATE attempt in {nameof(UpdateHotel)}"); return(BadRequest("Submitted data is invalid")); } // if not null then proceed with the update // so whatever is in hotelDTO put this into the "hotel" variable _imapper.Map(hotelDTO, hotel); _unitOfWork.Hotels.Update(hotel); await _unitOfWork.Save(); return(NoContent()); } catch (Exception ex) { _logger.LogError(ex, $"Something went wrong in the {nameof(UpdateHotel)}"); return(StatusCode(500, "Internal server Error. Please try again Later.")); } }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { if (!ModelState.IsValid) { _logger.LogError($"Invalid POST attempt in {nameof(CreateHotel)}"); return(BadRequest(ModelState)); } var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { if (!ModelState.IsValid) { _logger.LogError($"Invalid Post Request In {nameof(CreateHotel)}"); return(BadRequest(ModelState)); } var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); //hotel will be given an aoutomatic Id and get saved return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { _logger.LogInformation("Creating new Hotel!"); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); var result = _mapper.Map <HotelDTO>(hotel); return(CreatedAtRoute(nameof(GetHotel), new { id = hotel.Id }, result)); }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { if (!ModelState.IsValid) { _logger.LogError($"Invalid POST attempt in {nameof(CreateHotel)}"); return(BadRequest(ModelState)); } var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); // it returns the result of GetHotel method by calling it by its name so that we can see the recently added hotel. return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); }
public ActionResult Post([FromBody] CreateHotelDTO createHotelDTO) { try { _service.Save(createHotelDTO); return(Ok()); } catch (Exception e) { string errors = e.Message; return(ValidationProblem(new ValidationProblemDetails() { Type = "Model Validation Error", Detail = errors })); } }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { try { var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); } catch (Exception ex) { _logger.LogError(ex, $"Something went wrong in the {nameof(CreateHotel)}"); return(StatusCode(500, "Internal server error. Please try again later")); } }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { if (!ModelState.IsValid) { _logger.LogError($"Invalid POST attempt in {nameof(CreateHotel)}"); return(BadRequest(ModelState)); } try { var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); } catch (Exception ex) { _logger.LogError(ex, $"Something Went Wrong in the {nameof(CreateHotel)}"); return(StatusCode(500, "Internal Server Error.Please TRy Again later")); } }
public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDTO hotelDTO) { var location = GetControllerActionNames(); if (!ModelState.IsValid) { _logger.LogError($"{location}: Invalid POST attempt"); return(BadRequest(ModelState)); } try { var hotel = _mapper.Map <Hotel>(hotelDTO); await _unitOfWork.Hotels.Insert(hotel); await _unitOfWork.Save(); return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel)); } catch (Exception ex) { return(InternalError($"{location}: Error", ex)); } }
public void Save(CreateHotelDTO model) { var room = _mapper.Map <CreateHotelCommand>(model); _bus.SendCommand(room); }