public async Task <IActionResult> TripsByUserId(int id)
        {
            try
            {
                var trips = await _reportingRepository.GetTripsByUserAsync(id);

                return(View("UserTrips", trips.ToList()));
            }
            finally
            {
                _reportingRepository.Dispose();
            }
        }
Esempio n. 2
0
        public async Task Handle(InvoiceCreatedIntegrationEvent @event)
        {
            _logger.LogInformation("InvoiceCreatedIntegrationEvent handled");
            var trip = await _reportingRepository.GetTripAsync(@event.TripId);

            // we throw an exception in order to don't send the Acknowledgement to the service bus, probably the consumer read
            // this message before that the created one.
            if (trip == null)
            {
                throw new InvalidOperationException($"The trip {@event.TripId} doesn't exist. Error trying to update the materialized view.");
            }

            _logger.LogInformation($"Invoice {@event.InvoiceId} for Trip {@event.TripId} has been created.");

            trip.InvoiceId = @event.InvoiceId;
            trip.Fee       = @event.Fee;
            trip.Fare      = @event.Total - @event.Fee;

            try
            {
                await _reportingRepository.UpdateTripAsync(trip);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to update the Trip: {@event.TripId}", ex);
            }
            finally
            {
                _reportingRepository.Dispose();
            }
        }
        public async Task Handle(InvoicePaidIntegrationEvent @event)
        {
            var trip = await _reportingRepository.GetTripAsync(@event.TripId);

            // we throw an exception in order to don't send the Acknowledgement to the service bus, probably the consumer read
            // this message before that the created one.
            if (trip == null)
            {
                throw new InvalidOperationException($"The trip {@event.TripId} doesn't exist. Error trying to update the materialized view.");
            }

            trip.CardNumber    = @event.CardNumber;
            trip.CardType      = @event.CardType;
            trip.PaymentStatus = @event.Status == PaymentStatus.Accepted ? nameof(PaymentStatus.Accepted) : nameof(PaymentStatus.Rejected);

            try
            {
                await _reportingRepository.UpdateTripAsync(trip);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to update the Trip: {@event.TripId}", ex);
            }
            finally
            {
                _reportingRepository.Dispose();
            }
        }
        public async Task Handle(TripUpdatedIntegrationEvent @event)
        {
            var trip = await _reportingRepository.GetTripAsync(@event.TripId);

            // we throw an exception in order to don't send the Acknowledgement to the service bus, probably the consumer read the
            // updated message before that the created one.
            if (trip == null)
            {
                throw new InvalidOperationException($"The trip {@event.TripId} doesn't exist. Error trying to update the materialized view.");
            }

            if (trip.Status == "Finished")
            {
                return;
            }

            trip.Distance = @event.Distance;
            trip.Duration = @event.Duration;
            trip.Status   = @event.Status.Name;
            trip.Started  = @event.Started;
            trip.Ended    = @event.Ended;

            try
            {
                await _reportingRepository.UpdateTripAsync(trip);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to update the Trip: {@event.TripId} Trip status: {trip.Status}", ex);
            }
            finally
            {
                _reportingRepository.Dispose();
            }
        }
Esempio n. 5
0
        public async Task Handle(TripCreatedIntegrationEvent @event)
        {
            var existingTrip = _reportingRepository.GetTrip(@event.TripId);

            if (existingTrip != null)
            {
                return;
            }

            var driverTask = _driverRepository.GetDriverAsync(@event.DriverId);
            var userTask   = _userRepository.GetUserAsync(@event.UserTripId);
            await Task.WhenAll(driverTask, userTask);

            var driver = await driverTask;
            var user   = await userTask;

            var newTrip = new Trip
            {
                Id            = @event.TripId,
                Created       = @event.CreationDate,
                PaymentMethod = @event.PaymentMethod.Name,
                Status        = "Created",
                Model         = @event.VehicleInformation.Model,
                Brand         = @event.VehicleInformation.Brand,
                Plate         = @event.VehicleInformation.Plate,
                DriverId      = @event.DriverId,
                DriverName    = driver.Name,
                From          = @event.From.Description,
                To            = @event.To.Description,
                UserId        = @event.UserTripId,
                UserName      = user.Name
            };

            try
            {
                await _reportingRepository.AddTripAsync(newTrip);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Error trying to create the Trip: {@event.TripId}", ex);
            }
            finally
            {
                _reportingRepository.Dispose();
            }
        }