public async Task<ActionResult> CreateBookingForRoom(
            Guid roomId, [FromBody] BookingForm bookingForm)
        {
            var room = await _roomService.GetRoomAsync(roomId);
            if (room == null) return NotFound();

            var minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;
            if (tooShort) return BadRequest(new ApiError(
                $"The minimum booking duration is {minimumStay.TotalHours} hours."));

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);
            if (conflictedSlots.Any()) return BadRequest(new ApiError(
                "This time conflicts with an existing booking."));

            // Get the current user (TODO)
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(
                userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            return Created(
                Url.Link(nameof(BookingsController.GetBookingById),
                new { bookingId }),
                null);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> CreateBookingForRoom(Guid roomId, [FromBody] BookingForm bookingForm) //object je bindovany z body POST requestu, asp.net core robi model validaciu automaticky
        {
            var room = await _roomService.GetRoomAsync(roomId);

            if (room == null)
            {
                return(NotFound());
            }

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError($"The minimum booking duration is {minimumStay.TotalHours} hours.")));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            if (conflictedSlots.Any())
            {
                return(BadRequest(new ApiError("This time conflicts with an existing booking.")));
            }

            //TODO: Get the current user
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            return(Created(Url.Link(nameof(BookingsController.GetBookingById), new { bookingId }), null));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreateBookingForRoomAsync(
            Guid roomId,
            [FromBody] BookingForm bookingForm,
            CancellationToken cancellationToken
            )
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiError(ModelState)));
            }

            var room = await _roomService.GetRoomAsync(roomId, cancellationToken);

            if (room == null)
            {
                return(NotFound());
            }

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(
                           new ApiError($"The minimum booking duration is {minimumStay.TotalHours}.")
                           ));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value, cancellationToken
                );

            if (conflictedSlots.Any())
            {
                return(BadRequest(
                           new ApiError("This time conflicts with an existing booking.")
                           ));
            }

            // Get the user ID (TODO)
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(
                userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value, cancellationToken
                );

            return(Created(
                       Url.Link(nameof(BookingsController.GetBookingByIdAsync),
                                new { bookingId }),
                       null
                       ));
        }
        public async Task <ActionResult> CreateBookingForRoom(
            Guid roomId, [FromBody] BookingForm form)
        {
            var userId = await _userService.GetUserIdAsync(User);

            if (userId == null)
            {
                return(Unauthorized());
            }

            var room = await _roomService.GetRoomAsync(roomId);

            if (room == null)
            {
                return(NotFound());
            }

            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (form.EndAt.Value - form.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError(
                                      $"The minimum booking duration is {minimumStay.TotalHours} hours.")));
            }

            var conflictedSlots = await _openingService.GetConflictingSlots(
                roomId, form.StartAt.Value, form.EndAt.Value);

            if (conflictedSlots.Any())
            {
                return(BadRequest(new ApiError(
                                      "This time conflicts with an existing booking.")));
            }

            var bookingId = await _bookingService.CreateBookingAsync(
                userId.Value, roomId, form.StartAt.Value, form.EndAt.Value);

            return(Created(
                       Url.Link(nameof(BookingsController.GetBookingById),
                                new { bookingId }),
                       null));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> CreateBookingForRoom(Guid roomId, [FromBody] BookingForm bookingForm)
        {
            // Assume parameters have been validated (by framework)
            var room = await _roomService.GetRoomAsync(roomId);

            if (room == null)
            {
                return(NotFound());
            }

            // Not less than the allowed time for the bookings.
            var  minimumStay = _dateLogicService.GetMinimumStay();
            bool tooShort    = (bookingForm.EndAt.Value - bookingForm.StartAt.Value) < minimumStay;

            if (tooShort)
            {
                return(BadRequest(new ApiError($"Minimum booking duration is {minimumStay.TotalHours} hours")));
            }

            // Need list of conflicting slots
            var conflictingSlots = await _openingService.GetConflictingSlots(roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            if (conflictingSlots.Any())
            {
                return(BadRequest(new ApiError("Time conflicts with an existing booking.")));
            }

            // TODO current user (authentication needed)
            // Need a user id, future problem to solve
            var userId = Guid.NewGuid();

            var bookingId = await _bookingService.CreateBookingAsync(userId, roomId, bookingForm.StartAt.Value, bookingForm.EndAt.Value);

            // REturn result with a link that allows navigation to the new booking
            return(Created(Url.Link(nameof(BookingsController.GetBookingById), new { bookingId }), null));
        }