public IHttpActionResult UpdateVehicle([FromUri] int vehicleId, [FromBody] VehicleSummary vehicleSummary)
 {
     try
     {
         if (vehicleSummary.Year < 1900)
         {
             ModelState.AddModelError("vehicleSummary.Year", "Invalid Vehicle Year");
         }
         if (vehicleSummary.Price <= 0)
         {
             ModelState.AddModelError("vehicleSummary.Price", "Price must be positive value");
         }
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         if (!storeRepository.DoesStoreExists(vehicleSummary.StoreID))
         {
             return(BadRequest($"Store with id {vehicleSummary.StoreID} does not exists"));
         }
         return(Ok(vehicleService.EditVehicle(vehicleSummary, vehicleId)));
     }
     catch (InvalidOperationException)
     {
         return(BadRequest($"Vehicle with id {vehicleId} does not exists"));
     }
 }
        public Vehicle EditVehicle(VehicleSummary vehicleSummary, int vehicleId)
        {
            var vehicle = vehicleRepository.GetById(vehicleId);

            vehicle.StoreID = vehicleSummary.StoreID;
            vehicle.Make    = vehicleSummary.Make;
            vehicle.Model   = vehicleSummary.Model;
            vehicle.Price   = vehicleSummary.Price;
            vehicle.Year    = vehicleSummary.Year;
            return(vehicleRepository.ModifyVehicle(vehicle));
        }
        public Vehicle CreateVehicle(VehicleSummary vehicleSummary)
        {
            //todo mapper
            var vehicle = new Vehicle
            {
                StoreID = vehicleSummary.StoreID,
                Make    = vehicleSummary.Make,
                Model   = vehicleSummary.Model,
                Price   = vehicleSummary.Price,
                Year    = vehicleSummary.Year
            };

            return(vehicleRepository.AddVehicle(vehicle));
        }
 public IHttpActionResult CreateVehicle([FromBody] VehicleSummary vehicleSummary)
 {
     if (vehicleSummary.Year < 1900)
     {
         ModelState.AddModelError("vehicleSummary.Year", "Invalid Vehicle Year");
     }
     if (vehicleSummary.Price <= 0)
     {
         ModelState.AddModelError("vehicleSummary.Price", "Price must be positive value");
     }
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (!storeRepository.DoesStoreExists(vehicleSummary.StoreID))
     {
         return(BadRequest($"Store with id {vehicleSummary.StoreID} does not exists"));
     }
     return(Ok(vehicleService.CreateVehicle(vehicleSummary)));
 }
Пример #5
0
        public async Task <IActionResult> UpdateCrewDriver(string Code, string VehicleNumber)
        {
            try
            {
                Driver driver = dbContext.Drivers.Where(x => x.Code.Equals(Code)).FirstOrDefault();
                driver.VehicleNumber = VehicleNumber;

                //Update Summary

                VehicleSummary vehicleSummary = (from v in dbContext.VehicleSummaries
                                                 where v.DriverCode == driver.Code
                                                 select v).OrderByDescending(x => x.LastUpdate).FirstOrDefault();

                if (vehicleSummary.VehicleNumber == VehicleNumber)
                {
                }
                else
                {
                    vehicleSummary.LeavingDate = DateTime.Now;

                    VehicleSummary newVehicleSummary = new VehicleSummary()
                    {
                        Id             = Guid.NewGuid(),
                        AssignmentDate = DateTime.Now,
                        DriverCode     = driver.Code,
                        DriverName     = driver.Name,
                        VehicleNumber  = VehicleNumber,
                        LeavingDate    = DateTime.MinValue
                    };
                    await dbContext.VehicleSummaries.AddAsync(newVehicleSummary);
                }

                await dbContext.SaveChangesAsync();

                return(Ok("Driver updated!"));
            }
            catch (Exception ex)
            {
                return(BadRequest("Driver not updated!"));
            }
        }
Пример #6
0
        public async Task <IActionResult> SyncCrewDrivers()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://dev-sos-apis.azurewebsites.net/api/Fms/");
                    var response = await client.GetAsync("GetActiveCrews");

                    var responseString = await response.Content.ReadAsStringAsync();

                    List <Crew> CrewData = JsonConvert.DeserializeObject <List <Crew> >(responseString);

                    foreach (var crew in CrewData)
                    {
                        foreach (var member in crew.members)
                        {
                            if (member.designation.Contains("Driver"))
                            {
                                Driver driver = dbContext.Drivers.Where(x => x.Code.Equals(member.code)).FirstOrDefault();
                                if (driver == null)
                                {
                                    driver = new Driver()
                                    {
                                        Name          = member.name,
                                        Code          = member.code,
                                        VehicleNumber = crew.vehicle,
                                        Region        = dbContext.Vehicles.Where(x => x.VehicleNumber == crew.vehicle).FirstOrDefault().Region,
                                        SubRegion     = dbContext.Vehicles.Where(x => x.VehicleNumber == crew.vehicle).FirstOrDefault().SubRegion,
                                        Station       = dbContext.Vehicles.Where(x => x.VehicleNumber == crew.vehicle).FirstOrDefault().Station,

                                        Score = 100
                                    };
                                    await dbContext.Drivers.AddAsync(driver);

                                    VehicleSummary vehicleSummary = new VehicleSummary()
                                    {
                                        Id             = Guid.NewGuid(),
                                        AssignmentDate = DateTime.Now,
                                        DriverCode     = member.code,
                                        DriverName     = member.name,
                                        VehicleNumber  = crew.vehicle,
                                        LeavingDate    = DateTime.MinValue
                                    };
                                    await dbContext.VehicleSummaries.AddAsync(vehicleSummary);
                                }
                                else
                                {
                                    if (driver.VehicleNumber == crew.vehicle)
                                    {
                                        VehicleSummary vehicleSummary = (from v in dbContext.VehicleSummaries
                                                                         where v.DriverCode == driver.Code && v.VehicleNumber == crew.vehicle
                                                                         select v).OrderByDescending(x => x.LastUpdate).FirstOrDefault();
                                        if (vehicleSummary == null)
                                        {
                                            VehicleSummary newVehicleSummary = new VehicleSummary()
                                            {
                                                Id             = Guid.NewGuid(),
                                                AssignmentDate = DateTime.Now,
                                                DriverCode     = member.code,
                                                DriverName     = member.name,
                                                VehicleNumber  = crew.vehicle,
                                                LeavingDate    = DateTime.MinValue
                                            };
                                            await dbContext.VehicleSummaries.AddAsync(newVehicleSummary);
                                        }
                                    }
                                    else
                                    {
                                        VehicleSummary vehicleSummary = (from v in dbContext.VehicleSummaries
                                                                         where v.DriverCode == driver.Code
                                                                         select v).OrderByDescending(x => x.LastUpdate).FirstOrDefault();
                                        if (vehicleSummary != null)
                                        {
                                            vehicleSummary.LeavingDate = DateTime.Now;

                                            VehicleSummary newVehicleSummary = new VehicleSummary()
                                            {
                                                Id             = Guid.NewGuid(),
                                                AssignmentDate = DateTime.Now,
                                                DriverCode     = member.code,
                                                DriverName     = member.name,
                                                VehicleNumber  = crew.vehicle,
                                                LeavingDate    = DateTime.MinValue
                                            };
                                            await dbContext.VehicleSummaries.AddAsync(newVehicleSummary);
                                        }
                                    }
                                    if (driver.Score == 0)
                                    {
                                        driver.Score = 100;
                                    }
                                    driver.VehicleNumber = crew.vehicle;
                                    driver.Region        = (from v in dbContext.GBMSVehicles
                                                            join s in dbContext.SubRegions on v.Location equals s.XDescription
                                                            where v.Description == crew.vehicle
                                                            select s.XRegionDescription).SingleOrDefault();
                                    driver.SubRegion = (from v in dbContext.GBMSVehicles
                                                        join s in dbContext.SubRegions on v.Location equals s.XDescription
                                                        where v.Description == crew.vehicle
                                                        select s.XDescription).SingleOrDefault();

                                    driver.Station = (from v in dbContext.GBMSVehicles
                                                      join s in dbContext.Stations on v.Station equals s.XDescription
                                                      where v.Description == crew.vehicle
                                                      select s.XDescription).SingleOrDefault();
                                }
                            }
                        }
                    }
                    await dbContext.SaveChangesAsync();
                }
                return(Ok("Synchronized successfully!"));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }