Exemplo n.º 1
0
        public void AddRequest(RideRequestDto requestDto)
        {
            var ride = _rideLogic.GetRideById(requestDto.RideId);

            if (ride.DriverEmail == requestDto.PassengerEmail)
            {
                throw new UnauthorizedAccessException();
            }

            if (ride == null)
            {
                throw new RideNoLongerExistsException();
            }

            if (ride.NumberOfSeats <= 0)
            {
                throw new NoSeatsInRideException();
            }

            if (ride.RideDateTime < DateTime.Now)
            {
                throw new ArgumentException();
            }

            if (_rideLogic.IsRideRequested(requestDto.RideId, requestDto.PassengerEmail))
            {
                throw new AlreadyRequestedException();
            }

            requestDto.SeenByDriver    = false;
            requestDto.SeenByPassenger = true;
            requestDto.DriverEmail     = ride.DriverEmail;
            int addressId = _addressLogic.GetAddressId(requestDto.Address);

            if (addressId == -1)
            {
                throw new ArgumentException("Failed to get address id");
            }

            requestDto.AddressId = addressId;
            var entity = _rideRequestRepository.AddRequest(_mapper.Map <RideRequestDto, RideRequest>(requestDto));

            var driverNote = _driverNoteLogic.GetNoteByRide(requestDto.RideId);


            _driverSeenNoteReposiotory.AddNote(new DriverSeenNote {
                RideRequestId = entity.RideRequestId, DriverNoteId = driverNote.DriverNoteId
            });

            if (requestDto.RequestNote != null)
            {
                _rideRequestNoteLogic.AddNote(new RideRequestNoteDto {
                    RideRequestId = entity.RideRequestId, Text = requestDto.RequestNote
                });
            }
        }