public void RegisterClient(ClientView clientView) { using var ctx = new WorkshopContext(); Client client = new Client(); client.Email = clientView.Email; client.Name = clientView.Name; client.PhoneNumber = clientView.PhoneNumber; ctx.Clients.Add(client); ctx.SaveChanges(); }
public void AddRepairLog(RepairLogView log) { using var ctx = new WorkshopContext(); RepairLog newLog = new RepairLog(); newLog.Date = log.Date; newLog.Description = log.Description; newLog.Repair = ctx.Repairs.SingleOrDefault(r => r.Id == log.Repair.Id); newLog.TechnicianId = log.TechnicianId; ctx.RepairLogs.Add(newLog); ctx.SaveChanges(); }
public void UpdateRepair(RepairView repairView) { using var ctx = new WorkshopContext(); var r = ctx.Repairs.Include(r => r.BonusRepairs) .Include(r => r.RepairTechnicians) .SingleOrDefault(er => er.Id == repairView.Id); ctx.Repairs.Update(r); var auto = ctx.Automobiles.SingleOrDefault(a => a.Id == repairView.Auto.Id); auto.Brand = repairView.Auto.Brand; auto.Model = repairView.Auto.Model; auto.LicencePlate = repairView.Auto.LicencePlate; var client = ctx.Clients.SingleOrDefault(c => c.Id == repairView.Auto.Client.Id); client.Email = repairView.Auto.Client.Email; client.Name = repairView.Auto.Client.Name; client.PhoneNumber = repairView.Auto.Client.PhoneNumber; auto.Client = client; var manager = ctx.Managers.SingleOrDefault(m => m.Id == repairView.Manager.Id); r.Manager = manager; r.Auto = auto; r.State = repairView.State; r.Description = repairView.Description; r.Price = repairView.Price; r.RepairTechnicians?.Clear(); foreach (Technician tech in ctx.Technicians) { if (repairView.RepairTechnicians?.SingleOrDefault(b => b.TechnicianId == tech.Id) != null) { r.RepairTechnicians.Add(new RepairTechnician { RepairID = r.Id, TechnicianId = tech.Id }); } } r.BonusRepairs?.Clear(); foreach (Bonus b in ctx.Bonuses) { if (repairView.BonusRepairs?.SingleOrDefault(br => br.BonusName == b.Name) != null) { r.BonusRepairs.Add(new BonusRepair { BonusName = b.Name, RepairID = r.Id }); } } ctx.Entry(r).State = EntityState.Modified; ctx.SaveChanges(); }
public void UpdateRepairLog(RepairLogView log) { using var ctx = new WorkshopContext(); RepairLog updatedLog = ctx.RepairLogs.Single(rl => rl.Id == log.Id); updatedLog.Repair = ctx.Repairs.SingleOrDefault(r => r.Id == log.Repair.Id); updatedLog.Id = log.Id; updatedLog.Description = log.Description; updatedLog.Date = log.Date; updatedLog.TechnicianId = log.TechnicianId; ctx.RepairLogs.Update(updatedLog); ctx.SaveChanges(); }
public void RegisterAuto(AutoView autoView) { using var ctx = new WorkshopContext(); var validCar = ctx.Automobiles.SingleOrDefault(a => a.LicencePlate == autoView.LicencePlate); if (validCar != null) { ctx.Entry(validCar).State = EntityState.Modified; return; } RegisterClient(autoView.Client); Auto auto = new Auto(); auto.Brand = autoView.Brand; auto.Model = autoView.Model; auto.LicencePlate = autoView.LicencePlate; auto.Client = ctx.Clients.SingleOrDefault(c => c.Email == autoView.Client.Email); ctx.Automobiles.Add(auto); ctx.SaveChanges(); }
public void CreateRepair(RepairView repairView) { RegisterAuto(repairView.Auto); using var ctx = new WorkshopContext(); Repair repair = new Repair(); repair.Description = repairView.Description; repair.Manager = ctx.Managers.Single(m => m.Id == repairView.Manager.Id); repair.Price = repairView.Price; repair.State = repairView.State; repair.Auto = ctx.Automobiles.SingleOrDefault(a => a.LicencePlate == repairView.Auto.LicencePlate); ctx.Repairs.Add(repair); repair.BonusRepairs = new List <BonusRepair>(); foreach (string bonus in repairView.BonusRepairs.Select(br => br.BonusName)) { repair.BonusRepairs.Add(new BonusRepair { BonusName = bonus, RepairID = repair.Id }); } ctx.Entry(repair.Auto).State = EntityState.Modified; ctx.SaveChanges(); }