аааааааааааааа

This commit is contained in:
Дмитрий Блохин 2024-04-30 22:38:48 +04:00
parent f38b3743cd
commit d817f17b6f
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;
@ -38,7 +37,11 @@ namespace DatabaseImplement.Models
public virtual Guarantor Guarantor { get; set; } public virtual Guarantor Guarantor { get; set; }
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,
@ -53,11 +56,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()
{ {
@ -78,11 +96,14 @@ namespace DatabaseImplement.Models
context.SaveChanges(); context.SaveChanges();
foreach (var upWorker in workshopWorkers) foreach (var upWorker in workshopWorkers)
{ {
if (model.WorkshopWorker.ContainsKey(upWorker.WorkerId))
{
model.WorkshopWorker.Remove(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