Esempio n. 1
0
        public async Task <ActionResult <ReservationDetailDTO> > UpdateReservation([FromQuery] ReservationIndexDTO reservationIndexDTO, [FromBody] Reservation reservation)
        {
            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            try
            {
                var updatedReservation = await _rentService.UpdateReservation(reservationIndexDTO, reservation);

                return(Ok(updatedReservation));
            }
            catch (ArgumentNullException)
            {
                return(NotFound("Reservation not found"));
            }
            catch (ArgumentException exception)
            {
                return(BadRequest(exception.Message));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }
        }
Esempio n. 2
0
 private async Task <Reservation> FindReservation(ReservationIndexDTO reservationIndexDTO, bool asNoTracking = false)
 {
     if (asNoTracking)
     {
         return(await _context.Reservations.AsNoTracking().FirstOrDefaultAsync(r => r.ReservationNumber == reservationIndexDTO.ReservationNumber && r.Surname == reservationIndexDTO.Surname));
     }
     return(await _context.Reservations.FirstOrDefaultAsync(r => r.ReservationNumber == reservationIndexDTO.ReservationNumber && r.Surname == reservationIndexDTO.Surname));
 }
Esempio n. 3
0
        public async Task <ReservationDetailDTO> GetReservation(ReservationIndexDTO reservationIndexDTO)
        {
            var foundReservation = await FindReservation(reservationIndexDTO);

            if (foundReservation == null)
            {
                throw new ArgumentNullException("Reservation does not exists");
            }
            return(await ReservationToDTO(foundReservation));
        }
Esempio n. 4
0
        public async Task RemoveReservation(ReservationIndexDTO reservationIndexDTO)
        {
            var reservationToRemove = await FindReservation(reservationIndexDTO);

            if (reservationToRemove == null)
            {
                throw new ArgumentNullException("Reservation does not exists");
            }
            _context.Reservations.Remove(reservationToRemove);
            await _context.SaveChangesAsync();
        }
Esempio n. 5
0
        public async Task <ReservationDetailDTO> UpdateReservation(ReservationIndexDTO reservationIndexDTO, Reservation reservation)
        {
            if (reservationIndexDTO.ReservationNumber != reservation.ReservationNumber || reservationIndexDTO.Surname != reservation.Surname)
            {
                throw new ArgumentException("Given parameters does not match (ReservationNumber or Surname)");
            }
            if (await FindReservation(reservationIndexDTO, true) == null)
            {
                throw new ArgumentNullException("Reservation does not exists");
            }
            if (!await CheckCarAvailability(reservation.CarId, reservation.PickUpDate, reservation.ReturnDate, reservation.ReservationNumber))
            {
                throw new ArgumentException("Selected Car is not available at this time");
            }
            _context.Update(reservation);
            await _context.SaveChangesAsync();

            return(await ReservationToDTO(reservation));
        }
Esempio n. 6
0
        public async Task <ActionResult> RemoveReservation([FromQuery] ReservationIndexDTO reservationIndexDTO)
        {
            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }
            try
            {
                await _rentService.RemoveReservation(reservationIndexDTO);

                return(Ok());
            }
            catch (ArgumentNullException)
            {
                return(NotFound("Reservation not found"));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }
        }