2023-04-07 16:35:57 +04:00
|
|
|
|
using CaseAccountingContracts.BindingModels;
|
|
|
|
|
using CaseAccountingContracts.ViewModels;
|
|
|
|
|
using CaseAccountingDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CaseAccountingDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Lawyer : ILawyerModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string Surname { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string Patronymic { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int Experience { get; set; }
|
|
|
|
|
|
2023-05-19 20:02:17 +04:00
|
|
|
|
public int? SpecializationId { get; set; }
|
|
|
|
|
public virtual Specialization? Specialization { get; set; } = new();
|
2023-04-07 16:35:57 +04:00
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; set; }
|
|
|
|
|
public virtual User User { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[ForeignKey("LawyerId")]
|
|
|
|
|
public virtual List<CaseLawyer> CaseLawyers { get; set; } = new();
|
|
|
|
|
|
2023-05-19 20:02:17 +04:00
|
|
|
|
private Dictionary<int, ICaseModel>? _cases;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, ICaseModel> Cases
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_cases == null)
|
|
|
|
|
{
|
|
|
|
|
_cases = CaseLawyers.ToDictionary(
|
|
|
|
|
x => x.CaseId, x => x.Case as ICaseModel);
|
|
|
|
|
}
|
|
|
|
|
return _cases;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:35:57 +04:00
|
|
|
|
[ForeignKey("LawyerId")]
|
2023-04-08 22:40:14 +04:00
|
|
|
|
public virtual List<LawyerContract> LawyerContracts { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, IContractModel>? _contracts;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IContractModel> Contracts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_contracts == null)
|
|
|
|
|
{
|
|
|
|
|
_contracts = LawyerContracts.ToDictionary(
|
|
|
|
|
x => x.ContractId, x => x.Contract as IContractModel);
|
|
|
|
|
}
|
|
|
|
|
return _contracts;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 16:35:57 +04:00
|
|
|
|
|
2023-05-19 13:20:14 +04:00
|
|
|
|
public static Lawyer? Create(CaseAccountingDatabase context, LawyerBindingModel? model)
|
2023-04-07 16:35:57 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Lawyer()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Surname = model.Surname,
|
|
|
|
|
Patronymic = model.Patronymic,
|
|
|
|
|
Experience = model.Experience,
|
|
|
|
|
SpecializationId = model.SpecializationId,
|
2023-05-19 20:02:17 +04:00
|
|
|
|
Specialization = context.Specializations.FirstOrDefault(x => model.SpecializationId.HasValue && x.Id == model.SpecializationId),
|
2023-05-19 13:20:14 +04:00
|
|
|
|
UserId = model.UserId,
|
2023-05-19 20:02:17 +04:00
|
|
|
|
User = context.Users.FirstOrDefault(x => x.Id == model.UserId) ?? throw new Exception("User not found"),
|
|
|
|
|
LawyerContracts = model.Contracts.Select(x => new LawyerContract
|
|
|
|
|
{
|
|
|
|
|
Contract = context.Contracts.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
2023-04-07 16:35:57 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(LawyerBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Surname = model.Surname;
|
|
|
|
|
Patronymic = model.Patronymic;
|
|
|
|
|
Experience = model.Experience;
|
|
|
|
|
SpecializationId = model.SpecializationId;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:40:14 +04:00
|
|
|
|
public void UpdateContracts(CaseAccountingDatabase context, LawyerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var lawyerContracts = context.LawyerContracts
|
|
|
|
|
.Where(x => x.LawyerId == model.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (lawyerContracts != null)
|
|
|
|
|
{
|
|
|
|
|
context.LawyerContracts
|
|
|
|
|
.RemoveRange(lawyerContracts
|
|
|
|
|
.Where(x => !model.Contracts
|
|
|
|
|
.ContainsKey(x.ContractId))
|
|
|
|
|
);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
var lawyer = context.Lawyers
|
|
|
|
|
.First(x => x.Id == Id);
|
|
|
|
|
foreach (var lc in lawyerContracts)
|
|
|
|
|
{
|
|
|
|
|
model.Contracts.Remove(lc.ContractId);
|
|
|
|
|
}
|
|
|
|
|
foreach (var lc in model.Contracts)
|
|
|
|
|
{
|
|
|
|
|
context.LawyerContracts.Add(new LawyerContract
|
|
|
|
|
{
|
|
|
|
|
Lawyer = lawyer,
|
|
|
|
|
Contract = context.Contracts.First(x => x.Id == lc.Key),
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_contracts = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:35:57 +04:00
|
|
|
|
public LawyerViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Surname = Surname,
|
|
|
|
|
Patronymic = Patronymic,
|
|
|
|
|
Experience = Experience,
|
|
|
|
|
SpecializationId = SpecializationId,
|
2023-05-19 20:02:17 +04:00
|
|
|
|
UserId = UserId,
|
|
|
|
|
Specialization = Specialization?.Name ?? "Не указан"
|
2023-04-07 16:35:57 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|