98 lines
2.9 KiB
C#
98 lines
2.9 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.ViewModels;
|
|
using DataModels.Models;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
namespace DatabaseImplement.Models
|
|
{
|
|
public class Workshop : IWorkshopModel
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string Title { get; set; } = string.Empty;
|
|
[Required]
|
|
public string Address { get; set; } = string.Empty;
|
|
[Required]
|
|
public string Director { get; set; } = string.Empty;
|
|
[Required]
|
|
public int UserId { get; set; }
|
|
public virtual Guarantor User { get; set; }
|
|
public int? ProductionId { get; set; }
|
|
public virtual Production? Production { get; set; }
|
|
private Dictionary<int, IWorkerModel>? _workerWorkshops = null;
|
|
[NotMapped]
|
|
public Dictionary<int, IWorkerModel> WorkerWorkshops
|
|
{
|
|
get
|
|
{
|
|
if(_workerWorkshops == null)
|
|
{
|
|
_workerWorkshops = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel));
|
|
}
|
|
return _workerWorkshops;
|
|
}
|
|
}
|
|
[ForeignKey("WorkshopId")]
|
|
public List<WorkerWorkshop> Workers { get; set; } = new();
|
|
public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model)
|
|
{
|
|
return new Workshop()
|
|
{
|
|
Id = model.Id,
|
|
Title = model.Title,
|
|
Address = model.Address,
|
|
Director = model.Director,
|
|
ProductionId = model.ProductionId,
|
|
Production = model.ProductionId.HasValue ? context.Productions.FirstOrDefault(x => x.Id == model.ProductionId) : null,
|
|
UserId = model.UserId,
|
|
Workers = model.WorkshopWorker.Select(x => new WorkerWorkshop
|
|
{
|
|
Worker = context.Workers.First(y => y.Id == x.Key),
|
|
}).ToList(),
|
|
};
|
|
}
|
|
public void Update(WorkshopBindingModel model)
|
|
{
|
|
Title = model.Title;
|
|
Address = model.Address;
|
|
Director = model.Director;
|
|
}
|
|
public WorkshopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Title = Title,
|
|
Address = Address,
|
|
Director = Director,
|
|
ProductionId = ProductionId,
|
|
UserId = UserId,
|
|
WorkerWorkshops = WorkerWorkshops
|
|
};
|
|
public void UpdateWorkers(FactoryGoWorkDatabase context, WorkshopBindingModel model)
|
|
{
|
|
var workshopWorkers = context.WorkerWorkshops.Where(rec => rec.WorkshopId == model.Id).ToList();
|
|
if (workshopWorkers != null && workshopWorkers.Count > 0)
|
|
{
|
|
context.WorkerWorkshops.RemoveRange(workshopWorkers.Where(rec => !model.WorkshopWorker.ContainsKey(rec.WorkerId)));
|
|
context.SaveChanges();
|
|
foreach (var upWorker in workshopWorkers)
|
|
{
|
|
model.WorkshopWorker.Remove(upWorker.WorkerId);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var workshop = context.Workshops.First(x => x.Id == model.Id);
|
|
foreach (var dp in model.WorkshopWorker)
|
|
{
|
|
context.WorkerWorkshops.Add(new WorkerWorkshop
|
|
{
|
|
Workshop = workshop,
|
|
Worker = context.Workers.First(x => x.Id == dp.Key)
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_workerWorkshops = null;
|
|
}
|
|
}
|
|
}
|