public async Task <InvoiceReadModel> GetInvoice(string invoiceId)
        {
            Invoice invoice = await _eventRepository.GetByIdAsync(new InvoiceId(invoiceId));

            var taskInvoiceLines = Task.WhenAll(invoice.Lines.Select(async x =>
            {
                var getShipTask    = _shipRepository.GetShip(x.ShipId.ToString());
                var getServiceTask = _shipServiceRepository.GetShipService(x.ServiceId.ToString());
                await Task.WhenAll(getShipTask, getServiceTask);

                Ship ship           = getShipTask.Result;
                ShipService service = getServiceTask.Result;

                return(new InvoiceLineReadModel
                {
                    Description = $"Service: {service.Name} applied for ship: {ship.Name}",
                    Price = service.Price
                });
            }));


            var taskCustomer = Task.Run(async() =>
            {
                Customer foundCustomer      = await _customerRepository.GetCustomerAsync(invoice.CustomerId.ToString());
                CustomerReadModel readModel = new CustomerReadModel
                {
                    Address    = foundCustomer.Address,
                    Email      = foundCustomer.Email,
                    PostalCode = foundCustomer.PostalCode,
                    Residence  = foundCustomer.Residence
                };

                return(readModel);
            });

            var taskRental = Task.Run(async() =>
            {
                Rental foundRental        = await _rentalRepository.GetRental(invoice.RentalId.ToString());
                RentalReadModel readModel = new RentalReadModel
                {
                    Price = foundRental.Price
                };

                return(readModel);
            });

            await Task.WhenAll(taskInvoiceLines, taskCustomer, taskRental);

            var customer = await taskCustomer;
            var lines    = await taskInvoiceLines;
            var rental   = await taskRental;

            return(new InvoiceReadModel
            {
                Customer = customer,
                Lines = lines,
                Rental = rental,
                TotalPrice = rental.Price + lines.Sum(x => x.Price)
            });
        }
        private void Run(Rental rentToCreate)
        {
            var carToRent = _unitOfWork.CarRepository.Get(rentToCreate.CarId);

            carToRent.Status = Status.wypożyczony;

            var driver = _unitOfWork.DriverRepository.Get(rentToCreate.DriverId);

            _unitOfWork.RentalRepository.Insert(rentToCreate);

            var rentalReadModel = new RentalReadModel()
            {
                CarId              = rentToCreate.CarId,
                Driver             = driver.GetFullName(),
                DriverId           = driver.DriverId,
                Created            = DateTime.Now,
                RegistrationNumber = carToRent.RegistrationNumber,
                RentalId           = rentToCreate.RentalId,
                StartXPosition     = carToRent.XPosition,
                StartYPosition     = carToRent.YPosition,
                Total              = 0
            };

            _unitOfWork.RentalReadModel.Insert(rentalReadModel);

            var carReadModel = _unitOfWork.CarReadModel.Get(carToRent.CarId);

            carReadModel.Status = Status.wypożyczony;

            _unitOfWork.Commit();
        }
        public void Execute(StopRentingCarCommand command)
        {
            var rent = _unitOfWork.RentalRepository.GetRentalWithDriverAndCarDetails(command.RentId);

            Car car = _unitOfWork.CarRepository.Get(rent.CarId);

            car.UpdatePositions(command.StopX, command.StopY);
            car.ChangeStatus();

            rent.Finished = command.Finished;
            rent.Total    = rent.GetTotalPrice();

            RentalReadModel rental = _unitOfWork.RentalReadModel.Get(command.RentId);

            if (rental != null)
            {
                rental.Finished      = command.Finished;
                rental.Total         = rent.Total;
                rental.StopXPosition = command.StopX;
                rental.StopYPosition = command.StopY;
            }

            var carReadModel = _unitOfWork.CarReadModel.Get(rent.CarId);

            carReadModel.UpdatePositions(command.StopX, command.StopY);
            carReadModel.ChangeStatus();

            _unitOfWork.Commit();
        }
Exemplo n.º 4
0
        public RentalModelResult Execute(GetRentalByIdQuery query)
        {
            if (query.RentalId != Guid.Empty)
            {
                RentalReadModel rental = _unitOfWork.RentalReadModel.GetRentalWithCarAndDriver(query.RentalId);

                var rentalToReturn = _mapper.Map <RentalModelResult>(rental);

                return(rentalToReturn);
            }

            throw new Exception("Given Rental Id was Empty!");
        }
Exemplo n.º 5
0
        public void InitialDbData()
        {
            if (!Cars.Any())
            {
                List <Vehicle> cars = new List <Vehicle>()
                {
                    new Car()
                    {
                        CarId = Guid.Parse("7295a624-dea8-479c-b920-a3254f92af77"), CurrentDistance = 20.3, RegistrationNumber = "KRA2436", Status = Status.wolny, TotalDistance = 15034.5, XPosition = 2.4, YPosition = 5.2
                    },
                    new Car()
                    {
                        CarId = Guid.Parse("6291ffd4-cb73-4c37-aa9a-1591247f554d"), CurrentDistance = 13.7, RegistrationNumber = "KOL0201", Status = Status.wypożyczony, TotalDistance = 20134.5, XPosition = 1.4, YPosition = -2.2
                    },
                    new Car()
                    {
                        CarId = Guid.Parse("7fb934b4-dae2-4659-a9ee-d5af7af36f40"), CurrentDistance = 20.3, RegistrationNumber = "KRA2436", Status = Status.wolny, TotalDistance = 15034.5, XPosition = 2.4, YPosition = 5.2
                    }
                };
                foreach (Car item in cars)
                {
                    Cars.Add(item);
                    var carReadModel = new CarViewModel()
                    {
                        CarId              = item.CarId,
                        CurrentDistance    = item.CurrentDistance,
                        RegistrationNumber = item.RegistrationNumber,
                        Status             = item.Status,
                        TotalDistance      = item.TotalDistance,
                        XPosition          = item.XPosition,
                        YPosition          = item.YPosition
                    };
                    CarViewModels.Add(carReadModel);
                }
            }

            if (!Drivers.Any())
            {
                Driver driver = new Driver("42222/12/01118", "Jan", "Kowalski");
                driver.DriverId = Guid.Parse("0871afd6-dae6-45f2-b9cf-8f91f184d6af");

                Drivers.Add(driver);

                DriverViewModel driverViewModel = new DriverViewModel()
                {
                    DriverId      = driver.DriverId,
                    FirstName     = driver.FirstName,
                    LastName      = driver.LastName,
                    LicenceNumber = driver.LicenceNumber
                };

                DriverViewModels.Add(driverViewModel);
            }

            if (!Rentals.Any())
            {
                Rental rental = new Rental()
                {
                    RentalId = Guid.Parse("1c3444e1-e09c-48a1-9cf2-db713731b5b1"),
                    CarId    = Guid.Parse("6291ffd4-cb73-4c37-aa9a-1591247f554d"),
                    Started  = DateTime.ParseExact("21/05/2020 07:25:47", "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture),
                    DriverId = Guid.Parse("0871afd6-dae6-45f2-b9cf-8f91f184d6af")
                };

                Rentals.Add(rental);

                if (Cars.Any())
                {
                    var carToUpdate = Cars.FirstOrDefault(x => x.CarId == rental.CarId);
                    carToUpdate.ChangeStatus();
                }

                RentalReadModel rentalReadModel = new RentalReadModel()
                {
                    RentalId           = rental.RentalId,
                    RegistrationNumber = "KOL0201",
                    CarId          = rental.CarId,
                    DriverId       = rental.DriverId,
                    Driver         = "Jan Kowalski",
                    Created        = rental.Started,
                    StartXPosition = 1.4,
                    StartYPosition = -2.2
                };

                RentalReadModels.Add(rentalReadModel);
            }

            this.SaveChanges();
        }