public async Task <Company> EditCompany(EditCompany companyForEdit) { var company = await _context.Companies.FindAsync(companyForEdit.Id); company.Name = companyForEdit.Name; company.Address = companyForEdit.Address; company.Telephone = companyForEdit.Telephone ?? string.Empty; company.Email = companyForEdit.Email; var companySubscribers = this._context.UserCompanies.Where(u => u.CompanyId == company.Id).ToList(); foreach (var companySubscriber in companySubscribers) { var subscriberRemain = companyForEdit.Subscribers.Any(x => x.Id == companySubscriber.UserId); if (!subscriberRemain) { this._context.UserCompanies.Remove(companySubscriber); } } foreach (var subscriber in companyForEdit.Subscribers) { if (subscriber != null && !companySubscribers.Any(s => s.UserId == subscriber.Id)) { this._context.UserCompanies.Add(new UserCompany { CompanyId = company.Id, UserId = subscriber.Id }); } } await _context.SaveChangesAsync(); return((Company)_mapper.Map(company, typeof(Data.Models.Company), typeof(Company))); }
public static async Task CalculateNextService(string serviceId, FleetManagementDbContext context) { var service = await context.Services.FirstOrDefaultAsync(s => s.Id == serviceId); if (service == null) { return; } // BasedOn == time if (service.BasedOn == 0) { var currentTime = await context.Database.SqlQuery <DateTime>("SELECT GETUTCDATE()").FirstOrDefaultAsync(); var newCreatedTime = new DateTimeOffset(new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, currentTime.Minute, currentTime.Second, DateTimeKind.Utc)); service.Created = newCreatedTime; service.NextServiceTime = CalculateNextServiceTime(service); service.NextServiceReminderTime = CalculateNextServiceReminderTime(service); } // BasedOn == mileage else { var mileage = await context.TelematicsDatas.FirstOrDefaultAsync(t => t.VIN == service.Vehicle.VIN); if (mileage != null) { service.NextServiceMileage = CalculateNextServiceMileage(service, mileage.Mileage); service.NextServiceReminderMileage = CalculateNextServiceReminderMileage(service, service.NextServiceMileage); } } await context.SaveChangesAsync(); }
public async Task <Service> EditService(EditService serviceForEdit) { var service = await _context.Services.FindAsync(serviceForEdit.Id); if (service == null) { return(null); } service.Name = serviceForEdit.Name; service.Recipient = serviceForEdit.Recipient; service.Description = serviceForEdit.Description; service.MileageReminder = serviceForEdit.MileageReminder; service.MileageRule = serviceForEdit.MileageRule; service.TimeReminder = serviceForEdit.TimeReminder; service.TimeReminderEntity = serviceForEdit.TimeReminderEntity; service.TimeRule = serviceForEdit.TimeRule; service.TimeRuleEntity = serviceForEdit.TimeRuleEntity; service.BasedOn = serviceForEdit.BasedOn; await _context.SaveChangesAsync(); await NextServiceCalculation.CalculateNextService(service.Id, _context); return(this._mapper.Map <Data.Models.Service, Service>(service)); }
public async Task Execute(IJobExecutionContext context) { try { using (FleetManagementDbContext dbContext = new FleetManagementDbContext()) { var vehicles = await dbContext.Vehicles.ToListAsync(); foreach (var vehicle in vehicles) { var telematicsData = await dbContext.TelematicsDatas.FirstOrDefaultAsync(t => t.VIN == vehicle.VIN); TelematicsData newTelematicsData = new TelematicsData { VIN = vehicle.VIN, Mileage = TelematicsDataGenerator.GenerateNextMileageValue(telematicsData?.Mileage), FuelLevel = TelematicsDataGenerator.GenerateNextFuelLevelValue(telematicsData?.FuelLevel), CurrentSpeed = TelematicsDataGenerator.GenerateNextCurrentSpeedValue(telematicsData?.CurrentSpeed), WorkingTime = telematicsData?.WorkingTime != null ? telematicsData.WorkingTime += TimeSpan.FromMinutes(1) : new TimeSpan(0, 0, 1, 0), }; newTelematicsData.Idling = newTelematicsData.Idling.HasValue ? telematicsData?.CurrentSpeed == 0 ? telematicsData.Idling += TimeSpan.FromMinutes(3) : new TimeSpan(0, 0, 0, 0) : new TimeSpan(0, 0, 3, 0); if (telematicsData == null) { dbContext.TelematicsDatas.Add(newTelematicsData); } else { telematicsData.Mileage = newTelematicsData.Mileage; telematicsData.FuelLevel = newTelematicsData.FuelLevel; telematicsData.CurrentSpeed = newTelematicsData.CurrentSpeed; telematicsData.WorkingTime = newTelematicsData.WorkingTime; telematicsData.Idling = newTelematicsData.Idling; } await SeedTelematicsHistory.UpdateTelematicsHistory(newTelematicsData, dbContext); } await dbContext.SaveChangesAsync(); } } catch (Exception exception) { throw exception; } }
public async Task <Driver> EditDriver(EditDriver driverForEdit) { var driver = await _context.Drivers.FindAsync(driverForEdit.Id); driver.Name = driverForEdit.Name; driver.Address = driverForEdit.Address; driver.Telephone = driverForEdit.Telephone ?? string.Empty; driver.Email = driverForEdit.Email; driver.CompanyId = driverForEdit.CompanyId; await _context.SaveChangesAsync(); return((Driver)_mapper.Map(driver, typeof(Data.Models.Driver), typeof(Driver))); }
public async Task <Vehicle> EditVehicle(EditVehicle vehicleForEdit) { var vehicle = await _context.Vehicles.FindAsync(vehicleForEdit.Id); vehicle.VIN = vehicleForEdit.VIN; vehicle.PlateNumber = vehicleForEdit.PlateNumber; vehicle.Type = vehicleForEdit.Type; vehicle.Brand = vehicleForEdit.Brand; vehicle.Model = vehicleForEdit.Model; vehicle.ProductionYear = vehicleForEdit.ProductionYear; vehicle.CompanyId = vehicleForEdit.CompanyId; vehicle.DriverId = string.IsNullOrWhiteSpace(vehicleForEdit.DriverId) ? null : vehicleForEdit.DriverId; await _context.SaveChangesAsync(); return((Vehicle)_mapper.Map(vehicle, typeof(Data.Models.Vehicle), typeof(Vehicle))); }