public void Update(ZoneDTO zoneDTO) { Zone zone = null; using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { var query = from z in context.Zones where z.Id == zoneDTO.Id select z; zone = query.ToList().FirstOrDefault(); if (zone != null) { zone.MaxCapacity = zoneDTO.MaxCapacity; zone.Name = zoneDTO.Name; context.Zones.Attach(zone); var entry = context.Entry(zone); entry.Property(e => e.MaxCapacity).IsModified = true; entry.Property(e => e.Name).IsModified = true; 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 AssignZone(Guid From, Guid To) { using (VehicleTrackingDbContext context = new VehicleTrackingDbContext()) { Zone subZone = context.Zones .Include("Vehicles") .Include("SubZones") .Where(z => z.Id == From) .ToList().FirstOrDefault(); Zone zone = context.Zones .Include("Vehicles") .Include("SubZones") .Where(z => z.Id == To) .ToList().FirstOrDefault(); List <Zone> zones = context.Zones .Include("Vehicles") .Include("SubZones") .ToList(); Zone exParentZone = zones.Find(z => z.SubZones.Contains(subZone)); if (!zone.IsSubZone) { int capacityLeft = GetZoneCapacityLeft(To); if (subZone.MaxCapacity <= capacityLeft) { context.Zones.Attach(subZone); zone.SubZones.Add(subZone); if (exParentZone != null) { exParentZone.SubZones.Remove(subZone); } subZone.IsSubZone = true; var entrySZ = context.Entry(subZone); entrySZ.Property(sz => sz.IsSubZone).IsModified = true; context.SaveChanges(); } } } }
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 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(); } } }