Ejemplo n.º 1
0
        public async Task <bool> UpdateVehicleAssignments(int id, VehicleAssignmentDTO model)
        {
            if (model is null)
            {
                return(false);
            }

            var vehicle = await _vehicleRepo.GetAsync(id);

            if (vehicle == null)
            {
                return(false);
            }

            var driver = (await _driversvc.FirstOrDefaultAsync(c => c.Code == model.PryDriver));


            if (driver == null)
            {
                throw new LMEGenericException($"Driver ({model.PryDriver}) not found");
            }

            Driver handOverDriver = null;

            if (!string.IsNullOrEmpty(model.NewHandOverDriverCode))
            {
                handOverDriver = (await _driversvc.FirstOrDefaultAsync(c => c.Code == model.NewHandOverDriverCode));
                if (handOverDriver == null)
                {
                    throw new LMEGenericException($"Hand over driver ({model.NewHandOverDriverCode}) not found");
                }
            }

            // Set primary captain  vehicle/ but first take away current captain from this vehicle
            if (driver.VehicleRegistrationNumber != vehicle.RegistrationNumber)
            {
                var currAssignedCaptains = await _driversvc.GetDriverByBusNumberAsync(vehicle.RegistrationNumber);

                if (currAssignedCaptains != null && currAssignedCaptains.Code != model.PryDriver)
                {
                    var curPry = await _driversvc.GetAsync(currAssignedCaptains.Id);

                    if (curPry == null)
                    {
                        throw new LMEGenericException($"Unable to load previous owner of vehicle({vehicle.RegistrationNumber}) for deassignement");
                    }

                    curPry.VehicleRegistrationNumber = "";
                }
            }

            driver.VehicleRegistrationNumber = vehicle.RegistrationNumber;

            await _unitOfWork.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 2
0
        public async Task <ServiceResponse <bool> > UpdateVehicleAssignments(int id, VehicleAssignmentDTO model)
        {
            return(await HandleApiOperationAsync(async() => {
                var result = await _vehicleService.UpdateVehicleAssignments(id, model);

                return new ServiceResponse <bool>
                {
                    Object = result
                };
            }));
        }