This commit is contained in:
Sergey Kozyrev 2024-04-30 22:42:33 +04:00
commit 7f6c257c99
2 changed files with 31 additions and 13 deletions

View File

@ -34,10 +34,12 @@ namespace DatabaseImplement.Models
return _workerMachines; return _workerMachines;
} }
} }
[ForeignKey("WorkerId")] [ForeignKey("MachineId")]
public virtual List<WorkerMachine> Workers { get; set; } = new(); public virtual List<WorkerMachine> Workers { get; set; } = new();
public virtual Guarantor Guarantor { get; set; } public virtual Guarantor Guarantor { get; set; }
public static Machine? Create(MachineBindingModel model, FactoryGoWorkDatabase context) [ForeignKey("MachineId")]
public virtual List<Product> Products { get; set; }
public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context)
{ {
return new Machine() return new Machine()
{ {
@ -69,12 +71,7 @@ namespace DatabaseImplement.Models
var machineWorkers = context.WorkerMachines.Where(rec => rec.MachineId == model.Id).ToList(); var machineWorkers = context.WorkerMachines.Where(rec => rec.MachineId == model.Id).ToList();
if (machineWorkers != null && machineWorkers.Count > 0) if (machineWorkers != null && machineWorkers.Count > 0)
{ {
context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => !model.MachineWorker.ContainsKey(rec.WorkerId))); context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => model.MachineWorker.ContainsKey(rec.WorkerId)));
context.SaveChanges();
foreach (var upWorker in machineWorkers)
{
model.MachineWorker.Remove(upWorker.WorkerId);
}
context.SaveChanges(); context.SaveChanges();
} }
var machine = context.Machines.First(x => x.Id == model.Id); var machine = context.Machines.First(x => x.Id == model.Id);

View File

@ -17,7 +17,6 @@ namespace DatabaseImplement.Models
public string Director { get; set; } = string.Empty; public string Director { get; set; } = string.Empty;
[Required] [Required]
public int UserId { get; set; } public int UserId { get; set; }
public virtual Guarantor User { get; set; }
public int? ProductionId { get; set; } public int? ProductionId { get; set; }
public virtual Production? Production { get; set; } public virtual Production? Production { get; set; }
private Dictionary<int, IWorkerModel>? _workerWorkshops = null; private Dictionary<int, IWorkerModel>? _workerWorkshops = null;
@ -37,7 +36,11 @@ namespace DatabaseImplement.Models
public List<WorkerWorkshop> Workers { get; set; } = new(); public List<WorkerWorkshop> Workers { get; set; } = new();
public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model) public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model)
{ {
return new Workshop() if(model == null)
{
return null;
}
return new Workshop
{ {
Id = model.Id, Id = model.Id,
Title = model.Title, Title = model.Title,
@ -52,11 +55,26 @@ namespace DatabaseImplement.Models
}).ToList(), }).ToList(),
}; };
} }
public static Workshop Create(WorkshopViewModel model)
{
return new Workshop
{
Id = model.Id,
Title = model.Title,
Address = model.Address,
Director = model.Director,
ProductionId = model.ProductionId,
UserId = model.UserId,
};
}
public void Update(WorkshopBindingModel model) public void Update(WorkshopBindingModel model)
{ {
if (model == null)
return;
Title = model.Title; Title = model.Title;
Address = model.Address; Address = model.Address;
Director = model.Director; Director = model.Director;
ProductionId = model.ProductionId;
} }
public WorkshopViewModel GetViewModel => new() public WorkshopViewModel GetViewModel => new()
{ {
@ -77,11 +95,14 @@ namespace DatabaseImplement.Models
context.SaveChanges(); context.SaveChanges();
foreach (var upWorker in workshopWorkers) foreach (var upWorker in workshopWorkers)
{ {
model.WorkshopWorker.Remove(upWorker.WorkerId); if (model.WorkshopWorker.ContainsKey(upWorker.WorkerId))
} {
model.WorkshopWorker.Remove(upWorker.WorkerId);
}
}
context.SaveChanges(); context.SaveChanges();
} }
var workshop = context.Workshops.First(x => x.Id == model.Id); var workshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
foreach (var dp in model.WorkshopWorker) foreach (var dp in model.WorkshopWorker)
{ {
context.WorkerWorkshops.Add(new WorkerWorkshop context.WorkerWorkshops.Add(new WorkerWorkshop