public ActionResult Put(int id, [FromBody] ReservationEditDTO value) { try { _editReservationCommand.Execute(value); return(NoContent()); }catch (AlredyExistException) { return(StatusCode(422, "Any of parameters alredy exit value")); } catch (NotFoundException) { return(NotFound()); }catch (Exception) { return(StatusCode(500, "Server error, try later")); } }
public async Task <ReservationDTO> EditReservation(ReservationEditDTO reservationEditDTO, int userId) { ValidationResults result = ModelValidator.IsValid(reservationEditDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var reservation = context.Reservations.Get(reservationEditDTO.Id); if (reservation == null) { throw new NotFoundException("No such reservation"); } if (reservation.GuestId != userId) { throw new PermissionException(); } if (reservationEditDTO.StartDate != null && reservationEditDTO.EndDate != null) { bool canBeReserved = context.Reservations.GetAll() .Where(r => r.RoomId == reservation.RoomId && r.Id != reservation.Id && (r.StartDate <reservationEditDTO.StartDate && r.EndDate> reservationEditDTO.StartDate) || (r.StartDate <reservationEditDTO.EndDate && r.EndDate> reservationEditDTO.EndDate)) .Count() == 0; if (!canBeReserved) { throw new ValidationException("Room is not available is this time"); } reservation.StartDate = reservationEditDTO.StartDate; reservation.EndDate = reservationEditDTO.EndDate; } if (!string.IsNullOrEmpty(reservationEditDTO.AdditionalInfo)) { reservation.AdditionalInfo = reservationEditDTO.AdditionalInfo; } context.Reservations.Update(reservation); await context.SaveAsync(); return(reservation.ToReservationDTO()); }
public async Task <ReservationDTO> EditReservation([FromBody] ReservationEditDTO reservationEditDTO) { return(await this.reservationService.EditReservation(reservationEditDTO, User.ConvertToUserData().Id)); }