public async Task <string> CreateReservation(BookingRequestModel model) { try { var filter = Builders <Reservation> .Filter.Eq("RoomId", model.RoomId); var reservation = _reservation.Find(filter).FirstOrDefault(); if (reservation != null) { throw new Exception("Room can't be booked"); } var startDate = DateTime.UtcNow; var finishDate = startDate.AddMinutes(2); var userId = _headerService.GetUserId(); var newReservation = new Reservation() { Id = Guid.NewGuid(), RoomId = model.RoomId, UserId = userId, StartDateOfBooking = startDate, FinishDateOfBooking = finishDate, ReservStartDate = model.ReservStartDate, ReservFinishedDate = model.ReservFinishedDate }; _reservation.InsertOne(newReservation); var newTransferReservation = new TransferReservation() { RoomId = newReservation.RoomId }; await _publicher.Publish(newTransferReservation); return("Reservation was added successfully"); } catch (Exception ex) { throw ex; } }
public async Task <string> CreateReservation(BookingRequestModel model) { try { VerificationRoomId verify = new VerificationRoomId() { RoomId = model.RoomId.ToString() }; var price = await _publicher.VerifyRoomId(verify); var message = "Room was not found!!!!!"; if (price == message) { throw new Exception(message); } if (model.ReservFinishedDate <= model.ReservStartDate) { throw new Exception("Start date can not be biggest than finish date"); } var filter = Builders <Reservation> .Filter.Eq("RoomId", model.RoomId); var listReservations = _reservation.Find(filter).ToList(); //if (reservation != null) throw new Exception("Room can't be booked"); foreach (var reservation in listReservations) { if (model.ReservStartDate >= reservation.ReservStartDate && model.ReservStartDate <= reservation.ReservFinishedDate || model.ReservFinishedDate >= reservation.ReservStartDate && model.ReservFinishedDate <= reservation.ReservFinishedDate) { throw new Exception("Room can't be booked"); } else { if (model.ReservStartDate <= reservation.ReservStartDate && model.ReservFinishedDate >= reservation.ReservFinishedDate) { throw new Exception("Room can't be booked"); } } } var startDate = DateTime.UtcNow; var finishDate = startDate.AddMinutes(2); var userId = _headerService.GetUserId(); var numberOfNights = (model.ReservFinishedDate - model.ReservStartDate).Days; var priceForNight = Convert.ToDecimal(price); var amountPaid = numberOfNights * priceForNight; var newReservation = new Reservation() { Id = Guid.NewGuid(), RoomId = model.RoomId, UserId = userId, StartDateOfBooking = startDate, FinishDateOfBooking = finishDate, ReservStartDate = model.ReservStartDate, ReservFinishedDate = model.ReservFinishedDate, NumberOfNights = numberOfNights, AmountPaid = amountPaid, Status = Status.Booked }; _reservation.InsertOne(newReservation); var newTransferReservation = new TransferReservation() { RoomId = newReservation.RoomId }; return("Reservation was added successfully"); } catch (Exception ex) { throw ex; } }