public IActionResult ProcessReserve(ReserveDTO reserveDTO)
 {
     if (reserveDTO == null)
     {
         return(BadRequest());
     }
     return(Ok(_bookingService.ProcessBooking(reserveDTO)));
 }
Esempio n. 2
0
        public ReserveResultDTO ProcessBooking(ReserveDTO reserveDTO)
        {
            var result = new ReserveResultDTO();

            try
            {
                lock (_threadSafeBooking)
                {
                    var availableSeats = _context.CinemaBooking.First(x => x.CinemaFuctionId == reserveDTO.FunctionId).AvailableSeats;
                    if (availableSeats < reserveDTO.NumberOfTickets)
                    {
                        result.Message        = "NO SEATS";
                        result.AvailableSeats = availableSeats;
                        result.MessageCode    = -1;
                        return(result);
                    }

                    var paymentResult = _paymentService.Pay(reserveDTO.User, reserveDTO.NumberOfTickets);
                    if (paymentResult.Equals("OK"))
                    {
                        var cinemaReservation = new CinemaReservation();
                        cinemaReservation.CinemaFunctionId   = reserveDTO.FunctionId;
                        cinemaReservation.CreateDate         = DateTime.Now;
                        cinemaReservation.IsActive           = true;
                        cinemaReservation.TotalPaid          = reserveDTO.Total;
                        cinemaReservation.TotalTickets       = reserveDTO.NumberOfTickets;
                        cinemaReservation.User               = reserveDTO.User;
                        cinemaReservation.OriginalMovieTitle = reserveDTO.OriginalMovieTitle;
                        _context.CinemaReservations.Add(cinemaReservation);
                        var cinemaFunction = _context.CinemaBooking.First(x => x.CinemaFuctionId == reserveDTO.FunctionId);
                        cinemaFunction.AvailableSeats -= reserveDTO.NumberOfTickets;
                        cinemaReservation.TheaterId    = reserveDTO.TheaterId;
                        _context.SaveChanges();
                        result.MessageCode = 1;
                        result.Message     = "OK";
                        return(result);
                    }
                    result.MessageCode = -2;
                    result.Message     = "PAYMENT SERVICE ERROR";
                    return(result);
                }
            }
            catch (Exception ex)
            {
                //Here we can log the exception
                result.MessageCode = -3;
                result.Message     = "SYSTEM ERROR";
                return(result);
            }
        }
Esempio n. 3
0
        public void WhenProcessReserveThenReturnExpectedResult(string user, int functionId, int availableSeats, int numberOfTickets, int expectedResult)
        {
            _context.CinemaFunctions.Add(new CinemaFunction()
            {
                CinemaFuctionId = functionId, BasePrice = 1000, AvailableSeats = availableSeats, MovieId = 1
            });
            _context.SaveChanges();

            var reserveDTO = new ReserveDTO
            {
                FunctionId      = functionId,
                MovieId         = 1,
                NumberOfTickets = numberOfTickets,
                Total           = 3000,
                User            = user
            };
            var result = _bookingService.ProcessBooking(reserveDTO);

            Assert.True(result.MessageCode == expectedResult);
        }
        public List <ReserveDTO> GetReserves()
        {
            var reserveTable              = new UserData.TabReservedDataTable();
            var reserveAdapter            = new TabReservedTableAdapter();
            List <ReserveDTO> reserveList = new List <ReserveDTO>();
            int rowCount = reserveAdapter.getReserve(reserveTable);

            if (rowCount > 0)
            {
                foreach (DataRow r in reserveTable.Rows)
                {
                    ReserveDTO reserve = new ReserveDTO();
                    reserve.RID          = int.Parse(r["RID"].ToString());
                    reserve.UserName     = r["UserName"].ToString();
                    reserve.Title        = r["Title"].ToString();
                    reserve.ReservedDate = r["ReservedDate"].ToString();
                    reserveList.Add(reserve);
                }
            }
            return(reserveList);
        }