public void AddVehicle(VehicleDTO vehicleDTO) { Vehicle vehicle = mapper.ToEntity(vehicleDTO); vehicle.Id = Guid.NewGuid(); HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(vehicleDTO); using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { context.Vehicles.Add(vehicle); context.HistoricVehicles.Add(historicVehicle); context.SaveChanges(); } }
private HistoricVehicle mapVehicleToHistoricVehicle(Vehicle vehicle) { HistoricVehicle historicVehicle = new HistoricVehicle(); historicVehicle.Id = Guid.NewGuid(); historicVehicle.Brand = vehicle.Brand; historicVehicle.Color = vehicle.Color; historicVehicle.CurrentLocation = vehicle.CurrentLocation; historicVehicle.Date = DateTime.Now; historicVehicle.Model = vehicle.Model; historicVehicle.Status = vehicle.Status; historicVehicle.Type = vehicle.Type; historicVehicle.Vin = vehicle.Vin; historicVehicle.Year = vehicle.Year; return(historicVehicle); }
public Guid AddBatch(BatchDTO batchDTO) { Batch batch = this.batchMapper.ToEntity(batchDTO); using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { var queryUser = from u in context.Users where u.UserName == batchDTO.CreatorUserName select u; User user = queryUser.ToList().FirstOrDefault(); context.Users.Attach(user); List <Vehicle> realVehicles = new List <Vehicle>(); foreach (Vehicle vehicle in batch.Vehicles) { var queryVehicle = from v in context.Vehicles where v.Vin == vehicle.Vin select v; Vehicle realVehicle = queryVehicle.ToList().FirstOrDefault(); context.Vehicles.Attach(realVehicle); realVehicles.Add(realVehicle); if (realVehicle.Status == StatusCode.InspectedInPort) { realVehicle.Status = StatusCode.ReadyToGo; var entry = context.Entry(realVehicle); entry.Property(sz => sz.Status).IsModified = true; HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(realVehicle); context.HistoricVehicles.Add(historicVehicle); } } batch.Vehicles = realVehicles; batch.IdUser = user; if (batch.Vehicles.Count > 0) { context.Batches.Add(batch); context.SaveChanges(); } } return(batch.Id); }
public void UpdateVehicle(VehicleDTO vehicleDTO) { Vehicle vehicle = null; using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { var query = from v in context.Vehicles where v.Vin == vehicleDTO.Vin select v; vehicle = query.ToList().FirstOrDefault(); if (vehicle != null) { vehicle.Model = vehicleDTO.Model; vehicle.Status = vehicleDTO.Status; vehicle.Type = vehicleDTO.Type; vehicle.Vin = vehicleDTO.Vin; vehicle.Year = vehicleDTO.Year; vehicle.Brand = vehicleDTO.Brand; vehicle.Color = vehicleDTO.Color; vehicle.CurrentLocation = vehicleDTO.CurrentLocation; vehicle.Price = vehicleDTO.Price; context.Vehicles.Attach(vehicle); var entry = context.Entry(vehicle); entry.Property(e => e.Model).IsModified = true; entry.Property(e => e.Status).IsModified = true; entry.Property(e => e.Type).IsModified = true; entry.Property(e => e.Vin).IsModified = true; entry.Property(e => e.Year).IsModified = true; entry.Property(e => e.Brand).IsModified = true; entry.Property(e => e.Color).IsModified = true; entry.Property(e => e.CurrentLocation).IsModified = true; entry.Property(e => e.Price).IsModified = true; HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(vehicleDTO); context.HistoricVehicles.Add(historicVehicle); context.SaveChanges(); } } }
public Guid AddInspection(InspectionDTO inspection) { Inspection inspectionEntity = inspectionMapper.ToEntity(inspection); using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { inspectionEntity.IdUser = this.GetUser(context, inspection.CreatorUserName); Vehicle vehicle = this.GetVehicle(context, inspection.IdVehicle); inspectionEntity.IdVehicle = vehicle; inspectionEntity.IdLocation = this.GetLocation(context, inspection.Location); inspectionEntity.Damages = this.GetDamages(context, inspection.Damages); inspectionEntity.Date = DateTime.Now; if (vehicle.Status == StatusCode.Waiting) { vehicle.Status = StatusCode.ReadyToBeLocated; var entry = context.Entry(vehicle); entry.Property(sz => sz.Status).IsModified = true; HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(vehicle); context.HistoricVehicles.Add(historicVehicle); } else if (vehicle.Status == StatusCode.InPort) { vehicle.Status = StatusCode.InspectedInPort; var entry = context.Entry(vehicle); entry.Property(sz => sz.Status).IsModified = true; HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(vehicle); context.HistoricVehicles.Add(historicVehicle); } context.Users.Attach(inspectionEntity.IdUser); context.Inspections.Add(inspectionEntity); context.SaveChanges(); } return(inspectionEntity.Id); }
public void AssignVehicle(Guid idZone, string vin) { using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { Vehicle vehicle = context.Vehicles .Where(z => z.Vin == vin) .ToList().FirstOrDefault(); Zone zone = context.Zones .Include("Vehicles") .Include("SubZones") .Where(z => z.Id == idZone) .ToList().FirstOrDefault(); int capacityLeft = GetVehicleCapacityLeft(idZone); if (capacityLeft > 0) { context.Vehicles.Attach(vehicle); if (zone.Vehicles == null) { zone.Vehicles = new List <Vehicle>(); } zone.Vehicles.Add(vehicle); vehicle.Status = StatusCode.Located; var entry = context.Entry(vehicle); entry.Property(sz => sz.Status).IsModified = true; HistoricVehicle historicVehicle = mapVehicleToHistoricVehicle(vehicle); context.HistoricVehicles.Add(historicVehicle); context.SaveChanges(); } } }