2024-04-27 20:23:32 +04:00
|
|
|
|
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;
|
2024-04-30 23:42:05 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateCreate { get; set; } = DateTime.Now;
|
|
|
|
|
[Required]
|
2024-04-27 20:23:32 +04:00
|
|
|
|
public int UserId { get; set; }
|
2024-04-27 22:13:33 +04:00
|
|
|
|
public int? ProductionId { get; set; }
|
|
|
|
|
public virtual Production? Production { get; set; }
|
|
|
|
|
private Dictionary<int, IWorkerModel>? _workerWorkshops = null;
|
2024-04-27 20:23:32 +04:00
|
|
|
|
[NotMapped]
|
2024-04-30 21:51:55 +04:00
|
|
|
|
public Dictionary<int, IWorkerModel> WorkerWorkshops
|
2024-04-27 20:23:32 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(_workerWorkshops == null)
|
|
|
|
|
{
|
2024-04-27 22:13:33 +04:00
|
|
|
|
_workerWorkshops = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel));
|
2024-04-27 20:23:32 +04:00
|
|
|
|
}
|
|
|
|
|
return _workerWorkshops;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("WorkshopId")]
|
|
|
|
|
public List<WorkerWorkshop> Workers { get; set; } = new();
|
2024-04-27 22:13:33 +04:00
|
|
|
|
public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model)
|
2024-04-27 20:23:32 +04:00
|
|
|
|
{
|
2024-04-30 22:38:48 +04:00
|
|
|
|
if(model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Workshop
|
2024-04-27 20:23:32 +04:00
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Title = model.Title,
|
|
|
|
|
Address = model.Address,
|
|
|
|
|
Director = model.Director,
|
|
|
|
|
ProductionId = model.ProductionId,
|
2024-04-27 22:13:33 +04:00
|
|
|
|
Production = model.ProductionId.HasValue ? context.Productions.FirstOrDefault(x => x.Id == model.ProductionId) : null,
|
2024-04-27 20:23:32 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
Workers = model.WorkshopWorker.Select(x => new WorkerWorkshop
|
|
|
|
|
{
|
|
|
|
|
Worker = context.Workers.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-30 22:38:48 +04:00
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-27 20:23:32 +04:00
|
|
|
|
public void Update(WorkshopBindingModel model)
|
|
|
|
|
{
|
2024-04-30 22:38:48 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
return;
|
2024-04-27 20:23:32 +04:00
|
|
|
|
Title = model.Title;
|
|
|
|
|
Address = model.Address;
|
|
|
|
|
Director = model.Director;
|
2024-04-30 22:38:48 +04:00
|
|
|
|
ProductionId = model.ProductionId;
|
2024-04-27 20:23:32 +04:00
|
|
|
|
}
|
|
|
|
|
public WorkshopViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Title = Title,
|
|
|
|
|
Address = Address,
|
|
|
|
|
Director = Director,
|
2024-04-27 22:13:33 +04:00
|
|
|
|
ProductionId = ProductionId,
|
2024-04-27 20:23:32 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-04-30 22:38:48 +04:00
|
|
|
|
if (model.WorkshopWorker.ContainsKey(upWorker.WorkerId))
|
|
|
|
|
{
|
|
|
|
|
model.WorkshopWorker.Remove(upWorker.WorkerId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-27 20:23:32 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-04-30 22:38:48 +04:00
|
|
|
|
var workshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id);
|
2024-04-27 20:23:32 +04:00
|
|
|
|
foreach (var dp in model.WorkshopWorker)
|
|
|
|
|
{
|
|
|
|
|
context.WorkerWorkshops.Add(new WorkerWorkshop
|
|
|
|
|
{
|
|
|
|
|
Workshop = workshop,
|
2024-04-27 22:13:33 +04:00
|
|
|
|
Worker = context.Workers.First(x => x.Id == dp.Key)
|
2024-04-27 20:23:32 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_workerWorkshops = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|