Реализация интерфейсов хранения данных и некоторые правки

This commit is contained in:
Табеев Александр 2024-04-27 19:12:43 +04:00
parent 739b0dff81
commit e92a3ae037
16 changed files with 938 additions and 5 deletions

View File

@ -10,5 +10,7 @@ namespace ServiceStationContracts.SearchModels
{
public int? Id { get; set; }
public string? RepairName { get; set; }
public int? GuarantorId { get; set; }
public int? DefectId { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace ServiceStationContracts.SearchModels
{
public int? Id { get; set; }
public string? SparePartName { get; set; }
public int? GuarantorId { get; set; }
}
}

View File

@ -10,5 +10,7 @@ namespace ServiceStationContracts.SearchModels
{
public int? Id { get; set; }
public string? WorkName { get; set; }
public int? GuarantorId { get; set; }
public int? TechnicalWorkId { get; set; }
}
}

View File

@ -22,9 +22,9 @@ namespace ServiceStationContracts.ViewModels
[DisplayName("Стоимость ремонта")]
public double RepairPrice { get; set; }
public int GuarantorId { get; }
public int GuarantorId { get; set; }
public int DefectId { get; }
public int DefectId { get; set; }
public Dictionary<int, ISparePartModel> RepairSpareParts { get; set; } = new();
}

View File

@ -13,7 +13,7 @@ namespace ServiceStationContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("наименование работы")]
[DisplayName("Наименование работы")]
public string WorkName { get; set; } = string.Empty;
[DisplayName("Статус работы")]
@ -22,9 +22,9 @@ namespace ServiceStationContracts.ViewModels
[DisplayName("Стоимость работы")]
public double WorkPrice { get; set; }
public int GuarantorId { get; }
public int GuarantorId { get; set; }
public int TechnicalWorkId { get; }
public int TechnicalWorkId { get; set; }
public Dictionary<int, ISparePartModel> WorkSpareParts { get; } = new();
}

View File

@ -0,0 +1,103 @@
using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.StoragesContracts;
using ServiceStationContracts.ViewModels;
using ServiceStationDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Implements
{
public class GuarantorStorage : IGuarantorStorage
{
public List<GuarantorViewModel> GetFullList()
{
using var context = new ServiceStationDatabase();
return context.Guarantors
.Select(x => x.GetViewModel)
.ToList();
}
public List<GuarantorViewModel> GetFilteredList(GuarantorSearchModel model)
{
if (string.IsNullOrEmpty(model.GuarantorFIO)) return new();
using var context = new ServiceStationDatabase();
return context.Guarantors
.Include(x => x.SpareParts)
.Include(x => x.Repairs)
.Include(x => x.Works)
.Where(x => x.GuarantorFIO.Contains(model.GuarantorFIO))
.Select(x => x.GetViewModel)
.ToList();
}
public GuarantorViewModel? GetElement(GuarantorSearchModel model)
{
using var context = new ServiceStationDatabase();
if (!model.Id.HasValue && !string.IsNullOrEmpty(model.GuarantorFIO)) return null;
if (!string.IsNullOrEmpty(model.GuarantorFIO))
{
return context.Guarantors
.Include(x => x.SpareParts)
.Include(x => x.Repairs)
.Include(x => x.Works)
.FirstOrDefault(x => x.GuarantorFIO.Contains(model.GuarantorFIO))?
.GetViewModel;
}
return context.Guarantors
.Include(x => x.SpareParts)
.Include(x => x.Repairs)
.Include(x => x.Works)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
}
public GuarantorViewModel? Insert(GuarantorBindingModel model)
{
var newGuarantor = Guarantor.Create(model);
if (newGuarantor == null) return null;
using var context = new ServiceStationDatabase();
context.Guarantors.Add(newGuarantor);
context.SaveChanges();
return newGuarantor.GetViewModel;
}
public GuarantorViewModel? Update(GuarantorBindingModel model)
{
using var context = new ServiceStationDatabase();
var guarantor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
if (guarantor == null) return null;
guarantor.Update(model);
context.SaveChanges();
return guarantor.GetViewModel;
}
public GuarantorViewModel? Delete(GuarantorBindingModel model)
{
using var context = new ServiceStationDatabase();
var element = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Guarantors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,128 @@
using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.StoragesContracts;
using ServiceStationContracts.ViewModels;
using ServiceStationDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Implements
{
public class RepairStorage : IRepairStorage
{
public List<RepairViewModel> GetFullList()
{
using var context = new ServiceStationDatabase();
return context.Repairs
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Defect)
.Include(x => x.Guarantor)
.Select(x => x.GetViewModel)
.ToList();
}
public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
{
if (string.IsNullOrEmpty(model.RepairName) && !model.GuarantorId.HasValue) return new();
using var context = new ServiceStationDatabase();
if (model.GuarantorId.HasValue)
{
return context.Repairs
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Defect)
.Include(x => x.Guarantor)
.Where(x => x.GuarantorId == model.GuarantorId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Repairs
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Defect)
.Include(x => x.Guarantor)
.Where(x => x.RepairName == model.RepairName)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public RepairViewModel? GetElement(RepairSearchModel model)
{
if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue) return null;
using var context = new ServiceStationDatabase();
return context.Repairs
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Defect)
.Include(x => x.Guarantor)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && x.RepairName == model.RepairName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public RepairViewModel? Insert(RepairBindingModel model)
{
using var context = new ServiceStationDatabase();
var newRepair = Repair.Create(context, model);
if (newRepair == null) return null;
context.Add(newRepair);
context.SaveChanges();
return newRepair.GetViewModel;
}
public RepairViewModel? Update(RepairBindingModel model)
{
using var context = new ServiceStationDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var elem = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
if (elem == null) return null;
elem.Update(model);
context.SaveChanges();
if (model.RepairSpareParts != null) elem.UpdateSpareParts(context, model);
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public RepairViewModel? Delete(RepairBindingModel model)
{
using var context = new ServiceStationDatabase();
var element = context.Repairs
.Include(x => x.SpareParts)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Repairs.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,118 @@
using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.StoragesContracts;
using ServiceStationContracts.ViewModels;
using ServiceStationDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Implements
{
public class SparePartStorage : ISparePartStorage
{
public List<SparePartViewModel> GetFullList()
{
using var context = new ServiceStationDatabase();
return context.SpareParts
.Include(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Guarantor)
.Select(x => x.GetViewModel)
.ToList();
}
public List<SparePartViewModel> GetFilteredList(SparePartSearchModel model)
{
if (string.IsNullOrEmpty(model.SparePartName) && !model.GuarantorId.HasValue) return new();
using var context = new ServiceStationDatabase();
if (model.GuarantorId.HasValue)
{
return context.SpareParts
.Include(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Guarantor)
.Where(x => x.GuarantorId == model.GuarantorId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.SpareParts
.Include(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Guarantor)
.Where(x => x.SparePartName.Contains(model.SparePartName))
.Select(x => x.GetViewModel)
.ToList();
}
public SparePartViewModel? GetElement(SparePartSearchModel model)
{
if (string.IsNullOrEmpty(model.SparePartName) && !model.Id.HasValue) return null;
using var context = new ServiceStationDatabase();
return context.SpareParts
.Include(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.SparePartWorks)
.ThenInclude(x => x.Work)
.Include(x => x.Guarantor)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SparePartName) && model.SparePartName == x.SparePartName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public SparePartViewModel? Insert(SparePartBindingModel model)
{
using var context = new ServiceStationDatabase();
var newSparePart = SparePart.Create(model);
if (newSparePart == null) return null;
context.SpareParts.Add(newSparePart);
context.SaveChanges();
return newSparePart.GetViewModel;
}
public SparePartViewModel? Update(SparePartBindingModel model)
{
using var context = new ServiceStationDatabase();
var sparePart = context.SpareParts.FirstOrDefault(x => x.Id == model.Id);
if (sparePart == null) return null;
sparePart.Update(model);
context.SaveChanges();
return sparePart.GetViewModel;
}
public SparePartViewModel? Delete(SparePartBindingModel model)
{
using var context = new ServiceStationDatabase();
var element = context.SpareParts.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.SpareParts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,148 @@
using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.StoragesContracts;
using ServiceStationContracts.ViewModels;
using ServiceStationDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Implements
{
public class WorkStorage : IWorkStorage
{
public List<WorkViewModel> GetFullList()
{
using var context = new ServiceStationDatabase();
return context.Works
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.TechnicalWork)
.Include(x => x.Guarantor)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkName) && !model.GuarantorId.HasValue)
{
return new();
}
using var context = new ServiceStationDatabase();
if (model.GuarantorId.HasValue)
{
return context.Works
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.TechnicalWork)
.Include(x => x.Guarantor)
.Where(x => x.GuarantorId == model.GuarantorId)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.GuarantorId.HasValue)
{
return context.Works
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.TechnicalWork)
.Include(x => x.Guarantor)
.Where(x => x.GuarantorId == model.GuarantorId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Works
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.TechnicalWork)
.Include(x => x.Guarantor)
.Where(x => x.WorkName.Contains(model.WorkName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public WorkViewModel? GetElement(WorkSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkName) && !model.Id.HasValue) return null;
using var context = new ServiceStationDatabase();
return context.Works
.Include(x => x.SpareParts)
.ThenInclude(x => x.SparePart)
.ThenInclude(x => x.SparePartRepairs)
.ThenInclude(x => x.Repair)
.Include(x => x.TechnicalWork)
.Include(x => x.Guarantor)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkName) && x.WorkName == model.WorkName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public WorkViewModel? Insert(WorkBindingModel model)
{
using var context = new ServiceStationDatabase();
var newWork = Work.Create(context, model);
if (newWork == null) return null;
context.Works.Add(newWork);
context.SaveChanges();
return newWork.GetViewModel;
}
public WorkViewModel? Update(WorkBindingModel model)
{
using var context = new ServiceStationDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var elem = context.Works.FirstOrDefault(rec => rec.Id == model.Id);
if (elem == null) return null;
elem.Update(model);
context.SaveChanges();
if (model.WorkSpareParts != null) elem.UpdateSpareParts(context, model);
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public WorkViewModel? Delete(WorkBindingModel model)
{
using var context = new ServiceStationDatabase();
var element = context.Works
.Include(x => x.SpareParts)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Works.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,69 @@
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels.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 ServiceStationDatabaseImplement.Models
{
public class Guarantor : IGuarantorModel
{
public int Id { get; set; }
[Required]
public string GuarantorFIO { get; set; } = string.Empty;
public string? GuarantorEmail { get; set; } = string.Empty;
[Required]
public string GuarantorPassword { get; set; } = string.Empty;
[Required]
public string GuarantorNumber { get; set; } = string.Empty;
[ForeignKey("GuarantorId")]
public virtual List<Repair> Repairs { get; set; } = new();
[ForeignKey("GuarantorId")]
public virtual List<SparePart> SpareParts { get; set; } = new();
[ForeignKey("GuarantorId")]
public virtual List<Work> Works { get; set; } = new();
public static Guarantor? Create(GuarantorBindingModel model)
{
if (model == null) return null;
return new Guarantor()
{
Id = model.Id,
GuarantorFIO = model.GuarantorFIO,
GuarantorEmail = model.GuarantorEmail,
GuarantorPassword = model.GuarantorPassword,
GuarantorNumber = model.GuarantorNumber
};
}
public void Update(GuarantorBindingModel model)
{
if (model == null) return;
GuarantorFIO = model.GuarantorFIO;
GuarantorEmail = model.GuarantorEmail;
GuarantorPassword = model.GuarantorPassword;
GuarantorNumber = model.GuarantorNumber;
}
public GuarantorViewModel GetViewModel => new()
{
Id = Id,
GuarantorFIO = GuarantorFIO,
GuarantorEmail = GuarantorEmail,
GuarantorPassword = GuarantorPassword,
GuarantorNumber = GuarantorNumber
};
}
}

View File

@ -0,0 +1,123 @@
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels.Enums;
using ServiceStationDataModels.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 ServiceStationDatabaseImplement.Models
{
public class Repair : IRepairModel
{
public int Id { get; set; }
[Required]
public string RepairName { get; set; } = string.Empty;
[Required]
public RepairStatus Status { get; set; }
[Required]
public double RepairPrice { get; set; }
[Required]
public int GuarantorId { get; set; }
[Required]
public int DefectId { get; set; }
public virtual Guarantor Guarantor { get; set; }
public virtual Defect Defect { get; set; }
private Dictionary<int, ISparePartModel>? _repairSpareParts = null;
public Dictionary<int, ISparePartModel> RepairSpareParts
{
get
{
if (_repairSpareParts == null)
{
_repairSpareParts = SpareParts.ToDictionary(rec => rec.SparePartId, rec => rec.SparePart as ISparePartModel);
}
return _repairSpareParts;
}
}
[ForeignKey("RepairId")]
public virtual List<SparePartRepair> SpareParts { get; set; } = new();
[ForeignKey("RepairId")]
public virtual List<Defect> Defects { get; set; } = new();
public static Repair? Create(ServiceStationDatabase context, RepairBindingModel model)
{
if (model == null) return null;
return new Repair()
{
Id = model.Id,
RepairName = model.RepairName,
Status = model.Status,
RepairPrice = model.RepairPrice,
GuarantorId = model.GuarantorId,
DefectId = model.DefectId,
SpareParts = model.RepairSpareParts.Select(x => new SparePartRepair
{
SparePart = context.SpareParts.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(RepairBindingModel model)
{
if (model == null) return;
RepairName = model.RepairName;
Status = model.Status;
RepairPrice = model.RepairPrice;
}
public RepairViewModel GetViewModel => new()
{
Id = Id,
RepairName = RepairName,
Status = Status,
RepairPrice = RepairPrice,
GuarantorId = GuarantorId,
DefectId = DefectId,
RepairSpareParts = RepairSpareParts
};
public void UpdateSpareParts(ServiceStationDatabase context, RepairBindingModel model)
{
var sparepartRepairs = context.SparePartRepairs.Where(rec => rec.RepairId == model.Id).ToList();
if (sparepartRepairs != null && sparepartRepairs.Count > 0)
{
context.SparePartRepairs.RemoveRange(sparepartRepairs.Where(rec => !model.RepairSpareParts.ContainsKey(rec.SparePartId)));
context.SaveChanges();
foreach (var updateSparePart in sparepartRepairs)
{
model.RepairSpareParts.Remove(updateSparePart.SparePartId);
}
context.SaveChanges();
}
var repair = context.Repairs.First(x => x.Id == Id);
foreach (var cd in model.RepairSpareParts)
{
context.SparePartRepairs.Add(new SparePartRepair
{
Repair = repair,
SparePart = context.SpareParts.First(x => x.Id == cd.Key)
});
context.SaveChanges();
}
_repairSpareParts = null;
}
}
}

View File

@ -0,0 +1,64 @@
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels.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 ServiceStationDatabaseImplement.Models
{
public class SparePart : ISparePartModel
{
public int Id { get; set; }
[Required]
public string SparePartName { get; set; } = string.Empty;
[Required]
public double SparePartPrice { get; set; }
[Required]
public int GuarantorId { get; set; }
public virtual Guarantor Guarantor { get; set; }
[ForeignKey("SparePartId")]
public virtual List<SparePartRepair> SparePartRepairs { get; set; } = new();
[ForeignKey("SparePartId")]
public virtual List<SparePartWork> SparePartWorks { get; set; } = new();
public static SparePart? Create(SparePartBindingModel model)
{
if (model == null) return null;
return new SparePart()
{
Id = model.Id,
SparePartName = model.SparePartName,
SparePartPrice = model.SparePartPrice,
GuarantorId = model.GuarantorId
};
}
public void Update(SparePartBindingModel model)
{
if (model == null) return;
SparePartName = model.SparePartName;
SparePartPrice = model.SparePartPrice;
GuarantorId = model.GuarantorId;
}
public SparePartViewModel GetViewModel => new()
{
Id = Id,
SparePartName = SparePartName,
SparePartPrice = SparePartPrice,
GuarantorId = GuarantorId
};
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Models
{
public class SparePartRepair
{
public int Id { get; set; }
[Required]
public int SparePartId { get; set; }
[Required]
public int RepairId { get; set; }
public virtual SparePart SparePart { get; set; } = new();
public virtual Repair Repair { get; set; } = new();
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationDatabaseImplement.Models
{
public class SparePartWork
{
public int Id { get; set; }
[Required]
public int SparePartId { get; set; }
[Required]
public int WorkId { get; set; }
public virtual SparePart SparePart { get; set; } = new();
public virtual Work Work { get; set; } = new();
}
}

View File

@ -0,0 +1,122 @@
using ServiceStationContracts.BindingModels;
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels.Enums;
using ServiceStationDataModels.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 ServiceStationDatabaseImplement.Models
{
public class Work : IWorkModel
{
public int Id { get; set; }
[Required]
public string WorkName { get; set; } = string.Empty;
[Required]
public WorkStatus Status { get; set; }
[Required]
public double WorkPrice { get; set; }
[Required]
public int GuarantorId { get; set; }
[Required]
public int TechnicalWorkId { get; set; }
public virtual Guarantor Guarantor { get; set; }
public virtual TechnicalWork TechnicalWork { get; set; }
private Dictionary<int, ISparePartModel>? _workSpareParts = null;
public Dictionary<int, ISparePartModel> WorkSpareParts
{
get
{
if (_workSpareParts == null)
{
_workSpareParts = SpareParts.ToDictionary(rec => rec.SparePartId, rec => rec.SparePart as ISparePartModel);
}
return _workSpareParts;
}
}
[ForeignKey("WorkId")]
public virtual List<SparePartWork> SpareParts { get; set; } = new();
[ForeignKey("WorkId")]
public virtual List<TechnicalWork> TechnicalWorks { get; set; } = new();
public static Work? Create(ServiceStationDatabase context, WorkBindingModel model)
{
if (model == null) return null;
return new Work()
{
Id = model.Id,
WorkName = model.WorkName,
Status = model.Status,
WorkPrice = model.WorkPrice,
GuarantorId = model.GuarantorId,
TechnicalWorkId = model.TechnicalWorkId,
SpareParts = model.WorkSpareParts.Select(x => new SparePartWork
{
SparePart = context.SpareParts.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(WorkBindingModel model)
{
WorkName = model.WorkName;
Status = model.Status;
WorkPrice = model.WorkPrice;
GuarantorId = model.GuarantorId;
TechnicalWorkId = model.TechnicalWorkId;
}
public WorkViewModel GetViewModel => new()
{
Id = Id,
WorkName = WorkName,
Status = Status,
WorkPrice = WorkPrice,
GuarantorId = GuarantorId,
TechnicalWorkId = TechnicalWorkId
};
public void UpdateSpareParts(ServiceStationDatabase context, WorkBindingModel model)
{
var sparepartWorks = context.SparePartWorks.Where(rec => rec.WorkId == model.Id).ToList();
if (sparepartWorks != null && sparepartWorks.Count > 0)
{
context.SparePartWorks.RemoveRange(sparepartWorks.Where(rec => !model.WorkSpareParts.ContainsKey(rec.SparePartId)));
context.SaveChanges();
foreach (var updateCar in sparepartWorks)
{
model.WorkSpareParts.Remove(updateCar.SparePartId);
}
context.SaveChanges();
}
var work = context.Works.First(x => x.Id == Id);
foreach (var cd in model.WorkSpareParts)
{
context.SparePartWorks.Add(new SparePartWork
{
Work = work,
SparePart = context.SpareParts.First(x => x.Id == cd.Key)
});
context.SaveChanges();
}
_workSpareParts = null;
}
}
}

View File

@ -26,5 +26,11 @@ namespace ServiceStationDatabaseImplement
public virtual DbSet<Executor> Executors { get; set; }
public virtual DbSet<TechnicalWork> TechnicalWorks { get; set; }
public virtual DbSet<CarTechnicalWork> CarTechnicalWorks { get; set; }
public virtual DbSet<SparePart> SpareParts { get; set; }
public virtual DbSet<Repair> Repairs { get; set; }
public virtual DbSet<SparePartRepair> SparePartRepairs { get; set; }
public virtual DbSet<Guarantor> Guarantors { get; set; }
public virtual DbSet<Work> Works { get; set; }
public virtual DbSet<SparePartWork> SparePartWorks { get; set; }
}
}