92 lines
3.6 KiB
C#
92 lines
3.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using TaskTrackerContracts.BindingModels;
|
||
using TaskTrackerContracts.ViewModels;
|
||
using TaskTrackerDataModels.Models;
|
||
|
||
namespace TaskTrackerDatabase.Models
|
||
{
|
||
public class Organization : IOrganizationModel
|
||
{
|
||
public int Id { get; set; }
|
||
[Required]
|
||
public string OrganizationName { get; set; } = string.Empty;
|
||
|
||
private Dictionary<int, (IProjectModel, int)>? _organizationProjects = null;
|
||
[NotMapped]
|
||
public Dictionary<int, (IProjectModel, int)> OrganizationProjects
|
||
{
|
||
get
|
||
{
|
||
if (_organizationProjects == null)
|
||
{
|
||
_organizationProjects = Projects
|
||
.ToDictionary(recPC => recPC.ProjectId, recPC =>
|
||
(recPC.Project as IProjectModel, recPC.NumberEmployees));
|
||
}
|
||
return _organizationProjects;
|
||
}
|
||
}
|
||
[ForeignKey("OrganizationId")]
|
||
public virtual List<OrganizationProject> Projects { get; set; } = new();
|
||
|
||
public static Organization Create(TaskTrackerDatabase context, OrganizationBindingModel model)
|
||
{
|
||
return new Organization()
|
||
{
|
||
Id = model.Id,
|
||
OrganizationName = model.OrganizationName,
|
||
Projects = model.OrganizationProjects.Select(x => new OrganizationProject
|
||
{
|
||
Project = context.Projects.First(y => y.Id == x.Key),
|
||
NumberEmployees = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
public void Update(OrganizationBindingModel model)
|
||
{
|
||
OrganizationName = model.OrganizationName;
|
||
}
|
||
public OrganizationViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
OrganizationName = OrganizationName,
|
||
OrganizationProjects = OrganizationProjects
|
||
};
|
||
public void UpdateProjects(TaskTrackerDatabase context, OrganizationBindingModel model)
|
||
{
|
||
var organizationProjects = context.OrganizationProjects.Where(rec => rec.OrganizationId == model.Id).ToList();
|
||
if (organizationProjects != null && organizationProjects.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.OrganizationProjects.RemoveRange(organizationProjects.Where(rec => !model.OrganizationProjects.ContainsKey(rec.ProjectId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateProject in organizationProjects)
|
||
{
|
||
updateProject.NumberEmployees = model.OrganizationProjects[updateProject.ProjectId].Item2;
|
||
model.OrganizationProjects.Remove(updateProject.ProjectId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var reinforced = context.Organizations.First(x => x.Id == Id);
|
||
foreach (var rp in model.OrganizationProjects)
|
||
{
|
||
context.OrganizationProjects.Add(new OrganizationProject
|
||
{
|
||
Organization = reinforced,
|
||
Project = context.Projects.First(x => x.Id == rp.Key),
|
||
NumberEmployees = rp.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_organizationProjects = null;
|
||
}
|
||
}
|
||
}
|