Пример #1
0
            public async Task <Unit> Handle(Data request, CancellationToken cancellationToken)
            {
                Guid _Id    = Guid.NewGuid();
                var  driver = new Driver
                {
                    Id          = _Id,
                    DNI         = request.DNI,
                    FirstName   = request.FirstName,
                    LastName    = request.LastName,
                    TotalPoints = request.TotalPoints,
                    LostPoints  = 0,
                    Status      = true
                };

                var findDNI = await _driverRepository.FindBy(f => f.DNI == driver.DNI);

                if (findDNI != null & findDNI.Count > 0)
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = "DNI existing in the database" });
                }

                _driverRepository.Add(driver);

                if (request.VehiclesList != null)
                {
                    if (request.VehiclesList.Count > 10)
                    {
                        throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = "Driver cannot be habitual of more than 10 vehicles." });
                    }

                    foreach (var id in request.VehiclesList)
                    {
                        var findVehicle = await _vehiclesRepository.FindBy(f => f.Id == id);

                        if (!findVehicle.Any())
                        {
                            throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Vehicle does not exist: {id}." });
                        }

                        var driverVehicle = new DriverVehicle
                        {
                            Id        = Guid.NewGuid(),
                            DriverId  = driver.Id,
                            VehicleId = id
                        };
                        _driverVehicleRepository.Add(driverVehicle);
                    }
                }

                var value = await _driverRepository.Commit();

                if (value > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("The record was not inserted");
            }
Пример #2
0
            public async Task <Unit> Handle(Data request, CancellationToken cancellationToken)
            {
                Guid _Id     = Guid.NewGuid();
                var  vehicle = new Vehicle
                {
                    Id            = _Id,
                    Enrollment    = request.Enrollment,
                    Brand         = request.Brand,
                    Model         = request.Model,
                    RegularDriver = request.RegularDriver,
                    Status        = true
                };

                var findEnrollment = await _vehiclesRepository.FindBy(f => f.Enrollment == vehicle.Enrollment);

                if (findEnrollment != null & findEnrollment.Count > 0)
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = "Enrollment existing in the database" });
                }

                _vehiclesRepository.Add(vehicle);

                if (request.DriverList != null)
                {
                    foreach (var id in request.DriverList)
                    {
                        var findDriver = await _driverRepository.FindBy(f => f.Id == id);

                        if (!findDriver.Any())
                        {
                            throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Driver does not exist: {id}." });
                        }

                        var driverVehicle = new DriverVehicle
                        {
                            Id        = Guid.NewGuid(),
                            DriverId  = id,
                            VehicleId = vehicle.Id
                        };
                        _driverVehicleRepository.Add(driverVehicle);
                    }
                }

                var value = await _vehiclesRepository.Commit();

                if (value > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("The record was not inserted");
            }
Пример #3
0
            public async Task <Unit> Handle(Data request, CancellationToken cancellationToken)
            {
                Guid _Id    = Guid.NewGuid();
                var  record = new DriverVehicle
                {
                    Id        = _Id,
                    DriverId  = request.DriverId,
                    VehicleId = request.VehicleId
                };

                var findDriver = await _driverRepository.FindBy(f => f.Id == record.DriverId);

                if (!findDriver.Any())
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Driver does not exist: {record.DriverId}." });
                }

                var findVehicle = await _vehiclesRepository.FindBy(f => f.Id == record.VehicleId);

                if (!findVehicle.Any())
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Vehicle does not exist: {record.VehicleId}." });
                }

                var findDuplicated = await _driverVehicleRepository.FindBy(f =>
                                                                           f.DriverId == record.DriverId && f.VehicleId == record.VehicleId);

                if (findDuplicated != null & findDuplicated.Count() > 0)
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Record exist: {record.DriverId} - {record.VehicleId}." });
                }

                var findCount = await _driverVehicleRepository.FindBy(f => f.DriverId == record.DriverId);

                if (findCount.Count > 10)
                {
                    throw new ExceptionHandler(HttpStatusCode.Conflict, new { mensaje = $"Driver cannot be habitual of more than 10 vehicles. {record.DriverId}" });
                }


                _driverVehicleRepository.Add(record);
                var value = await _driverVehicleRepository.Commit();

                if (value > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("The record was not inserted");
            }