Beispiel #1
0
        public async Task <BookingResponse> UpdateAsync(int id, BookingSaveResource bookingSaveResource)
        {
            var booking = await _bookingRepository.FindByIdAsync(id);

            if (booking == null)
            {
                return(new BookingResponse(404, "Item id not found", "Id", "Booking id not found."));
            }

            BookingResponse bookingResponse = await CommonValidations(bookingSaveResource);

            if (bookingResponse != null)
            {
                return(bookingResponse);
            }

            booking.MemberId    = bookingSaveResource.MemberId;
            booking.CourtId     = bookingSaveResource.CourtId;
            booking.Reservation = bookingSaveResource.Reservation;

            try
            {
                _bookingRepository.Update(booking);
                var responseResource = _mapper.Map <Booking, BookingResource>(booking);
                return(new BookingResponse(responseResource));
            }
            catch (Exception ex)
            {
                return(new BookingResponse(400, "Unexpected error", "Error", ex.Message));
            }
        }
Beispiel #2
0
        private async Task <BookingResponse> CommonValidations(BookingSaveResource bookingSaveResource)
        {
            var court = await _courtRepository.FindByIdAsync(bookingSaveResource.CourtId);

            if (court == null)
            {
                return(new BookingResponse(404, "Item id not found", "CourtId", "Court id not found."));
            }

            var member = await _memberRepository.FindByIdAsync(bookingSaveResource.MemberId);

            if (member == null)
            {
                return(new BookingResponse(404, "Item id not found", "MemberId", "Member id not found."));
            }

            if (bookingSaveResource.Reservation.Hour < 8 || bookingSaveResource.Reservation.Hour > 22)
            {
                return(new BookingResponse(400, "Bad booking time", "DateTime", "Bookings hour must be between 08:00 and 22:00"));
            }

            if (bookingSaveResource.Reservation.Minute != 0 || bookingSaveResource.Reservation.Second != 0 || bookingSaveResource.Reservation.Millisecond != 0)
            {
                return(new BookingResponse(400, "Bad booking time", "DateTime", "Bookings only accept full hours"));
            }

            if (bookingSaveResource.Reservation < DateTime.Today)
            {
                return(new BookingResponse(400, "Bad booking time", "DateTime", "DateTime is older than today"));
            }

            var bookingsOfCourt = await _bookingRepository.GetBookingsOfHour(bookingSaveResource.Reservation, 0, bookingSaveResource.CourtId);

            if (bookingsOfCourt.Count > 0)
            {
                return(new BookingResponse(400, "Court already booked", "CourtId", "Court is already booked for " + bookingSaveResource.Reservation.ToString("yyyy-MM-dd HH:mm")));
            }

            var bookingsOfMember = await _bookingRepository.GetBookingsOfDate(bookingSaveResource.Reservation, bookingSaveResource.MemberId);

            if (bookingsOfMember.Count == 3)
            {
                return(new BookingResponse(400, "Too many bookings", "MemberId", "A member only is allowed to book 3 courts per day"));
            }

            var bookingsOfMemberHour = await _bookingRepository.GetBookingsOfHour(bookingSaveResource.Reservation, bookingSaveResource.MemberId);

            if (bookingsOfMemberHour.Count == 2)
            {
                return(new BookingResponse(400, "Too many bookings today", "MemberId", "A member only is allowed to book 2 courts at the same hour"));
            }

            return(null);
        }
Beispiel #3
0
        public async Task <IActionResult> PostAsync([FromBody] BookingSaveResource saveResource)
        {
            var response = await _bookingService.SaveAsync(saveResource);

            if (!response.Success)
            {
                return(BadRequest(response.Error));
            }

            return(CreatedAtAction(nameof(GetAsync), new { id = response.Result.Id }, response.Result));
        }
Beispiel #4
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] BookingSaveResource saveResource)
        {
            var response = await _bookingService.UpdateAsync(id, saveResource);

            if (!response.Success && response.Error.Status == 404)
            {
                return(NotFound(response.Error));
            }

            if (!response.Success)
            {
                return(BadRequest(response.Error));
            }

            return(Ok(response.Result));
        }
Beispiel #5
0
        public async Task <BookingResponse> SaveAsync(BookingSaveResource bookingSaveResource)
        {
            try
            {
                BookingResponse bookingResponse = await CommonValidations(bookingSaveResource);

                if (bookingResponse != null)
                {
                    return(bookingResponse);
                }

                var item = _mapper.Map <BookingSaveResource, Booking>(bookingSaveResource);
                await _bookingRepository.AddAsync(item);

                var responseResource = _mapper.Map <Booking, BookingResource>(item);
                return(new BookingResponse(responseResource));
            }
            catch (Exception ex)
            {
                return(new BookingResponse(400, "Unexpected error", "Error", ex.Message));
            }
        }