예제 #1
0
        public async Task <IActionResult> RequestBooking(BookingCreateDto bookingForm)
        {
            var user = await _context.Users.Where(x => x.Username == User.FindFirst(ClaimTypes.Name).Value).FirstOrDefaultAsync();

            if (user == null)
            {
                return(Unauthorized("User not found"));
            }

            var bookee = await _context.Users.Where(x => x.Id == bookingForm.BookedWithUserId).FirstOrDefaultAsync();

            if (bookee.BookingsEnabled == false)
            {
                return(BadRequest("User does not currently take bookings"));
            }

            DateTime currentDT  = DateTime.Now;
            Booking  newBooking = new Booking()
            {
                BookingOwnerId   = user.Id,
                BookedWithUserId = bookingForm.BookedWithUserId,
                BookingCreated   = currentDT,
                BookingUpdated   = currentDT,
                RequestedDT      = bookingForm.RequestedDT,
                StatusId         = 1, // pending
            };

            await _context.Booking.AddAsync(newBooking);

            await _context.SaveChangesAsync();

            return(StatusCode(201));
        }
예제 #2
0
        public async Task <ActionResult <Booking> > CreateBookingAsync(BookingCreateDto bookingCreateDto)
        {
            var bookingModel = _mapper.Map <Booking>(bookingCreateDto);
            await _repository.CreateAsync(bookingModel);

            await _repository.SaveChangesAsync();

            var bookingReadDto = _mapper.Map <BookingReadDto>(bookingModel);

            return(CreatedAtRoute(nameof(GetBookingAsync), new { bookingReadDto.Id }, bookingReadDto));
        }
예제 #3
0
        public HttpResponseMessage BookJourney(BookingCreateDto bookingDto)
        {
            if (bookingDto == null)
            {
                HttpError err = new HttpError("The bookingDto cannot be null");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
            }
            else if (bookingBusiness.IsAttributeNull(bookingDto))
            {
                HttpError err = new HttpError("There is at least one value missing");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
            }
            else
            {
                string routeID = bookingDto.routeID;
                string userID  = bookingDto.userID;

                if (!bookingBusiness.IsUserIDValid(userID))
                {
                    HttpError err = new HttpError("The userID you entered is not valid.");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                else if (bookingBusiness.SameDepartureTime(routeID, userID))
                {
                    HttpError err = new HttpError("Have Already Booked A Journey With the Same Departure Time");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                else if (bookingBusiness.UpperNearDepartureTime(routeID, userID))
                {
                    HttpError err = new HttpError("A Journey which has a departure time within 3 hours of new booking was already booked ");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                else if (bookingBusiness.BookingExists(routeID, userID))
                {
                    HttpError err = new HttpError("User Already Booked This Journey");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                else if (!bookingBusiness.HasAlreadyBookedThisJourney(routeID, userID))
                {
                    HttpError err = new HttpError("You have already have a booking in this journey.");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                else if (bookingBusiness.IsSeatsAvailable(routeID))
                {
                    HttpError err = new HttpError("No Seats Available");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, err));
                }
                bookingBusiness.BookJourney(routeID, userID);
                return(Request.CreateResponse(HttpStatusCode.OK, "Journey Booked"));
            }
        }
        public async Task <ActionResult <BookingReadDto> > PostBooking(BookingCreateDto bookingCreateDto)
        {
            var booking = mapper.Map <Booking>(bookingCreateDto);

            booking.DateCreated = DateTime.Now;

            await repository.PostBookingAsync(booking);

            await repository.SaveChangesAsync();

            var bookingReadDto = mapper.Map <BookingReadDto>(booking);

            return(CreatedAtAction(nameof(GetBooking), new { id = bookingReadDto.Id }, bookingReadDto));
        }