Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
        public async Task <string> EditUserAsync(EditUserRequestModel model)
        {
            try
            {
                var userId = _headerService.GetUserId();
                var user   = _userRepository.GetUserById(userId);

                if (user == null)
                {
                    throw new Exception("User was not found");
                }

                _mapper.Map(model, user);

                await _userRepository.EditUser(user);

                return("User was edited successfully");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        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;
            }
        }
Esempio n. 4
0
        public async Task <string> PayRoom(OrderRequestModel model)
        {
            try
            {
                if (model.ReservationId == null)
                {
                    throw new Exception("operation is impossible without Id");
                }
                var userId = _headerService.GetUserId();
                if (userId == null)
                {
                    throw new Exception("operation is impossible without Id");
                }
                var verify = new VerificationReservationId()
                {
                    ReservationId = model.ReservationId.ToString(),
                    UserId        = userId.ToString()
                };

                var reservation = await _publisher.VerifyReservationId(verify);

                var receiveReservation = JsonConvert.DeserializeObject <ReceiveReservation>(reservation);


                var amountPaid = receiveReservation.AmountPaid.ToString();


                var pay = new PaymentModel()
                {
                    AmountPaid = amountPaid, UserId = userId.ToString()
                };

                var message = await _publisher.PayRoom(pay);

                var m   = "spspspsps";
                var mmm = "rtrtrtrtrtrtr";


                if (message == m)
                {
                    throw new Exception(message);
                }

                if (message == mmm)
                {
                    throw new Exception(message);
                }

                var errorMessage = "I'm sorry, but it appears your account has insufficient funds";

                if (message == errorMessage)
                {
                    throw new Exception(message);
                }

                var dateOfPayment = DateTime.UtcNow;
                var newOrder      = new Order()
                {
                    Id               = Guid.NewGuid(),
                    UserId           = userId,
                    RoomId           = receiveReservation.RoomId,
                    ReservStartDate  = receiveReservation.ReservStartDate,
                    ReservFinishDate = receiveReservation.ReservFinishedDate,
                    DateOfPayment    = dateOfPayment,
                    AmountPaid       = receiveReservation.AmountPaid,
                };

                await _orderRepository.AddOrderAsync(newOrder);

                var payment = new Payment()
                {
                    ReservationId = model.ReservationId.ToString()
                };

                await _publisher.PublishPayment(payment);

                return(message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "faild orderService");
                throw;
            }
        }