Ejemplo n.º 1
0
        public async Task ReservationCanBeUpdatedAsync_ReservationCanBeExtended_ReturnsTrue()
        {
            //Arrange
            var reservationDto = new ReservationUpdateDto()
            {
                ReservationId = 1
            };
            var reservations = new List <Reservation>()
            {
                new Reservation()
                {
                    ReservationId = 1
                }
            };

            mockReservationRepository
            .Setup(p => p.FilterReservationsAsync(It.IsAny <Reservation>()))
            .ReturnsAsync(reservations);
            var service = new ReservationService(mockReservationRepository.Object, mapper);
            //Act
            var result = await service.ReservationCanBeUpdatedAsync(reservationDto);

            //Assert
            Assert.True(result);
        }
Ejemplo n.º 2
0
        public async Task UpdateCarAsync_PassedValidObject_ReturnsObject()
        {
            //Arrange
            ReservationUpdateDto reservationDto = new ReservationUpdateDto()
            {
                ReservationId = 1,
                RentalDate    = DateTime.Today,
                ReturnDate    = DateTime.Today.AddDays(5),
                IsFinished    = true
            };
            Reservation reservation = new Reservation()
            {
                ReservationId = 1,
                RentalDate    = DateTime.Today,
                ReturnDate    = DateTime.Today.AddDays(10),
                IsFinished    = false
            };

            mockReservationRepository
            .Setup(p => p.FindByIdAsync(reservationDto.ReservationId))
            .ReturnsAsync(reservation);
            mockReservationRepository
            .Setup(s => s.SaveChangesAsync())
            .Verifiable();
            var service = new ReservationService(mockReservationRepository.Object, mapper);
            //Act
            var result = await service.UpdateReservationAsync(reservationDto);

            //Assert
            Assert.Equal(result.ReservationId, reservationDto.ReservationId);
            Assert.Equal(Convert.ToDateTime(result.RentalDate), reservationDto.RentalDate);
            Assert.Equal(Convert.ToDateTime(result.ReturnDate), reservationDto.ReturnDate);
            Assert.Equal(result.IsFinished, reservationDto.IsFinished);
            Assert.IsType <ReservationDto>(result);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Update(ReservationUpdateDto reservationCreateDto)
        {
            Reservation reservation = mapper.Map <Reservation>(reservationCreateDto);

            reservation.Status = reservationCreateDto.Paid ? ReservationStatus.Paid : ReservationStatus.Pending;
            reservation.Car    = await carService.GetById(reservation.CarId);

            reservation.UserId = long.Parse(User.FindFirst("Id").Value);

            await reservationService.Update(reservation);

            return(NoContent());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Change one or many properities of existing reservation in the database.
        /// </summary>
        /// <param name="reservationUpdateDto"></param>
        /// <returns>Returns updated reservations object mapped to reservationDto.</returns>
        public async Task <ReservationDto> UpdateReservationAsync(ReservationUpdateDto reservationUpdateDto)
        {
            var reservation = await reservationRepository.FindByIdAsync(reservationUpdateDto.ReservationId);

            reservation.Update(reservationUpdateDto.RentalDate,
                               reservationUpdateDto.ReturnDate,
                               reservationUpdateDto.IsFinished);
            reservationRepository.Update(reservation);
            await reservationRepository.SaveChangesAsync();

            reservation = await reservationRepository.FindByIdAsync(reservationUpdateDto.ReservationId);

            return(mapper.Map <ReservationDto>(reservation));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> UpdateReservationAsync(int id, ReservationUpdateDto reservationUpdateDto)
        {
            if (id != reservationUpdateDto.ReservationId)
            {
                return(BadRequest());
            }
            if (await reservationService.ReservationCanBeUpdatedAsync(reservationUpdateDto))
            {
                var reservation = await reservationService.UpdateReservationAsync(reservationUpdateDto);

                return(Ok(reservation));
            }
            return(BadRequest());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Check if reservation can be updated. Method is being executed before UpdateReservationAsync() method.
        /// It checks how many reservations are in the database for given term and car.
        /// If 0 then new reservation doesn't collide with other reservations.
        /// If 1 then the method checks if found reservation has the same is as passed object.
        /// If ids are different than reservation can't be updated.
        /// </summary>
        /// <param name="reservationDto"></param>
        /// <returns>Returns true if new reservation can be created and false if can't.</returns>
        public async Task <bool> ReservationCanBeUpdatedAsync(ReservationUpdateDto reservationDto)
        {
            var reservation = new Reservation()
            {
                ReservationId = reservationDto.ReservationId,
                RentalDate    = Convert.ToDateTime(reservationDto.RentalDate),
                ReturnDate    = Convert.ToDateTime(reservationDto.ReturnDate),
                CarId         = reservationDto.CarId
            };
            List <Reservation> reservations = await reservationRepository.FilterReservationsAsync(reservation);

            int count = reservations.Count;
            int id    = count == 0 ? 0 : reservations.FirstOrDefault().ReservationId;

            return((count == 0 || (count == 1 && id == reservation.ReservationId)) ? true : false);
        }
Ejemplo n.º 7
0
        public ActionResult UpdateReservation(int id, ReservationUpdateDto reservationUpdateDto)
        {
            var reservationModelFromRepo = _repository.GetReservationById(id);

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

            _mapper.Map(reservationUpdateDto, reservationModelFromRepo);

            _repository.UpdateReservation(reservationModelFromRepo);

            _repository.SaveChanges();

            return(NoContent());
        }
Ejemplo n.º 8
0
        public async Task ReservationCanBeUpdatedAsync_CarIsNotAvailable_ReturnsFalse()
        {
            //Arrange
            var reservationDto = new ReservationUpdateDto()
            {
            };
            var reservations = new List <Reservation>()
            {
                new Reservation(), new Reservation()
            };

            mockReservationRepository
            .Setup(p => p.FilterReservationsAsync(It.IsAny <Reservation>()))
            .ReturnsAsync(reservations);
            var service = new ReservationService(mockReservationRepository.Object, mapper);
            //Act
            var result = await service.ReservationCanBeUpdatedAsync(reservationDto);

            //Assert
            Assert.False(result);
        }
        public virtual async Task UpdateAsync(ReservationUpdateDto input)
        {
            var reservation = await _reservationRepository.GetAsync(input.Id);

            ObjectMapper.Map(input, reservation);
        }