예제 #1
0
        public IHttpActionResult CustomerBooking(CustomerBookingDTO booking) // BOOKING WITHOUT USER ACCOUNT, ONLY AS CUSTOMER
        {
            if (!ModelState.IsValid)
            {
                var errors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
                return(BadRequest(ModelState));
            }
            TextResult      httpResponse     = new TextResult("", msg);
            SeatManager     smgr             = new SeatManager();
            CustomerManager cmgr             = new CustomerManager();
            BookingManager  bmgr             = new BookingManager();
            Customers       custEntity       = new Customers();
            Bookings        bookingEntity    = new Bookings();
            int?            customerId       = null;
            DateTime        currentDate      = DateTime.Now;
            var             convertedForDate = bmgr.DateTimeConverter(booking.BookingForDate);                 // Convert dates passed from fronted to DateTime objects
            int?            allSeatsId       = smgr.GetSeatPlacementId(booking.RowNumber, booking.SeatNumber); // Gets the allSeatsId from AllSeats from row and seatnumber

            try
            {
                custEntity = cmgr.AddCustomer(booking.Email); // try to create new customer entity
                db.Customers.Add(custEntity);
                db.SaveChanges();                             // if customer entity exists, trying to insert a new customer will cause exception due to duplicate keys
            } catch
            {
                customerId = cmgr.FindCustomerId(booking.Email);                                             // if customer entity already exists, get customerID from email input
            }
            if (customerId != null)                                                                          // if customer entity already exists, customerId is not null, use customerId instead of entity
            {
                bookingEntity = bmgr.CustomerBooking(convertedForDate, currentDate, allSeatsId, customerId); // creates booking entity with customerId
                try
                {
                    db.Bookings.Add(bookingEntity);     // creates booking enitity, with customerId
                    db.SaveChanges();
                }
                catch
                {
                    httpResponse.ChangeHTTPMessage("Could not make booking!", msg);
                    return(httpResponse);
                }
            }
            bookingEntity = bmgr.CustomerBooking(custEntity, convertedForDate, currentDate, allSeatsId); // creates booking entity, with customerEntity
            try
            {
                db.Bookings.Add(bookingEntity);
                db.SaveChanges();
            }   catch
            {
                httpResponse.ChangeHTTPMessage("Could not make booking!", msg);
                return(httpResponse);
            }
            httpResponse.ChangeHTTPMessage("Booking completed!", msg);
            return(httpResponse);
        }