DatabaseImplement

This commit is contained in:
Vadim 2023-04-08 21:47:17 +04:00
parent 14f80b4425
commit 29030d6829
22 changed files with 1395 additions and 0 deletions

View File

@ -0,0 +1,97 @@
using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
namespace STODatabaseImplement.Implements
{
public class CarStorage : ICarStorage
{
public List<CarViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Cars.Include(x => x.Spares).ThenInclude(x => x.Spare).ToList()
.Select(x => x.GetViewModel).ToList();
}
public List<CarViewModel> GetFilteredList(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.Brand))
{
return new();
}
using var context = new STODatabase();
return context.Cars.Include(x => x.Spares).ThenInclude(x => x.Spare)
.Where(x => x.Brand.Contains(model.Brand)).ToList().Select(x => x.GetViewModel).ToList();
}
public CarViewModel? GetElement(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.Brand) && !model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.Cars.Include(x => x.Spares).ThenInclude(x => x.Spare)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Brand) && x.Brand == model.Brand) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public CarViewModel? Insert(CarBindingModel model)
{
using var context = new STODatabase();
var newCar = Car.Create(context, model);
if (newCar == null)
{
return null;
}
context.Cars.Add(newCar);
context.SaveChanges();
return newCar.GetViewModel;
}
public CarViewModel? Update(CarBindingModel model)
{
using var context = new STODatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Car = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);
if (Car == null)
{
return null;
}
Car.Update(model);
context.SaveChanges();
Car.UpdateComponents(context, model);
transaction.Commit();
return Car.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CarViewModel? Delete(CarBindingModel model)
{
using var context = new STODatabase();
var element = context.Cars.Include(x => x.Spares).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cars.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,81 @@
using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
namespace STODatabaseImplement.Implements
{
public class EmployerStorage : IEmployerStorage
{
public List<EmployerViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Employers.Select(x => x.GetViewModel).ToList();
}
public List<EmployerViewModel> GetFilteredList(EmployerSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new STODatabase();
return context.Employers.Where(x => (!string.IsNullOrEmpty(model.Login) && x.Login.Contains(model.Login)) ||
(!string.IsNullOrEmpty(model.Email) && !x.Email.Contains(model.Email))).Select(x => x.GetViewModel).ToList();
}
public EmployerViewModel? GetElement(EmployerSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login) && (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)))
{
return null;
}
using var context = new STODatabase();
return context.Employers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && x.Email == model.Email && x.Password == model.Password))?.GetViewModel;
}
public EmployerViewModel? Insert(EmployerBindingModel model)
{
var newEmployer = Employer.Create(model);
if (newEmployer == null)
{
return null;
}
using var context = new STODatabase();
context.Employers.Add(newEmployer);
context.SaveChanges();
return newEmployer.GetViewModel;
}
public EmployerViewModel? Update(EmployerBindingModel model)
{
using var context = new STODatabase();
var storekeeper = context.Employers.FirstOrDefault(x => x.Id == model.Id);
if (storekeeper == null)
{
return null;
}
storekeeper.Update(model);
context.SaveChanges();
return storekeeper.GetViewModel;
}
public EmployerViewModel? Delete(EmployerBindingModel model)
{
using var context = new STODatabase();
var storekeeper = context.Employers.FirstOrDefault(x => x.Id == model.Id);
if (storekeeper != null)
{
context.Employers.Remove(storekeeper);
context.SaveChanges();
return storekeeper.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
namespace STODatabaseImplement.Implements
{
public class MaintenanceStorage : IMaintenanceStorage
{
public List<MaintenanceViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Maintenances.Include(x => x.Cars).ThenInclude(x => x.Car).ToList()
.Select(x => x.GetViewModel).ToList();
}
public List<MaintenanceViewModel> GetFilteredList(MaintenanceSearchModel model)
{
using var context = new STODatabase();
return context.Maintenances.Include(x => x.Cars).ThenInclude(x => x.Car).ToList().Select(x => x.GetViewModel).ToList();
}
public MaintenanceViewModel? GetElement(MaintenanceSearchModel model)
{
if ( !model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.Maintenances.Include(x => x.Cars).ThenInclude(x => x.Car)
.FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MaintenanceViewModel? Insert(MaintenanceBindingModel model)
{
using var context = new STODatabase();
var newMaintenance = Maintenance.Create(context, model);
if (newMaintenance == null)
{
return null;
}
context.Maintenances.Add(newMaintenance);
context.SaveChanges();
return newMaintenance.GetViewModel;
}
public MaintenanceViewModel? Update(MaintenanceBindingModel model)
{
using var context = new STODatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Maintenance = context.Maintenances.FirstOrDefault(rec => rec.Id == model.Id);
if (Maintenance == null)
{
return null;
}
Maintenance.Update(model);
context.SaveChanges();
Maintenance.UpdateComponents(context, model);
transaction.Commit();
return Maintenance.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MaintenanceViewModel? Delete(MaintenanceBindingModel model)
{
using var context = new STODatabase();
var element = context.Maintenances.Include(x => x.Cars).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Maintenances.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,87 @@
using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
namespace STODatabaseImplement.Implements
{
public class ServiceStorage : IServiceStorage
{
public List<ServiceViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Services.Select(x => x.GetViewModel).ToList();
}
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
{
using var context = new STODatabase();
return context.Services.Select(x => x.GetViewModel).ToList();
}
public ServiceViewModel? GetElement(ServiceSearchModel model)
{
if (string.IsNullOrEmpty(model.ServiceDescription) && !model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.Services.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ServiceDescription) && x.ServiceDescription == model.ServiceDescription) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ServiceViewModel? Insert(ServiceBindingModel model)
{
using var context = new STODatabase();
var newService = Service.Create(model);
if (newService == null)
{
return null;
}
context.Services.Add(newService);
context.SaveChanges();
return newService.GetViewModel;
}
public ServiceViewModel? Update(ServiceBindingModel model)
{
using var context = new STODatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Service = context.Services.FirstOrDefault(rec => rec.Id == model.Id);
if (Service == null)
{
return null;
}
Service.Update(model);
context.SaveChanges();
transaction.Commit();
return Service.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ServiceViewModel? Delete(ServiceBindingModel model)
{
using var context = new STODatabase();
var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Services.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,79 @@

using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Implements
{
public class SpareStorage : ISpareStorage
{
public List<SpareViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Spares.Select(x => x.GetViewModel).ToList();
}
public List<SpareViewModel> GetFilteredList(SpareSearchModel model)
{
using var context = new STODatabase();
return context.Spares.Select(x => x.GetViewModel).ToList();
}
public SpareViewModel? GetElement(SpareSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.Spares.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public SpareViewModel? Insert(SpareBindingModel model)
{
using var context = new STODatabase();
var newSpares = Spare.Create( model);
if(newSpares == null)
{
return null;
}
context.Spares.Add(newSpares);
context.SaveChanges();
return newSpares.GetViewModel;
}
public SpareViewModel? Update(SpareBindingModel model)
{
using var context = new STODatabase();
var spares = context.Spares.FirstOrDefault(x=>x.Id == model.Id);
if(spares == null)
{
return null;
}
spares.Update(model);
context.SaveChanges();
return spares.GetViewModel;
}
public SpareViewModel? Delete(SpareBindingModel model)
{
using var context = new STODatabase();
var spare = context.Spares.FirstOrDefault(x=>x.Id==model.Id);
if(spare != null)
{
context.Spares.Remove(spare);
context.SaveChanges();
return spare.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,86 @@

using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Implements
{
public class StorekeeperStorage : IStorekeeperStorage
{
public List<StorekeeperViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Storekeepers.Select(x => x.GetViewModel).ToList();
}
public List<StorekeeperViewModel> GetFilteredList(StorekeeperSearchModel model)
{
if(string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new STODatabase();
return context.Storekeepers.Where(x => (!string.IsNullOrEmpty(model.Login) && x.Login.Contains(model.Login)) ||
(!string.IsNullOrEmpty(model.Email) && !x.Email.Contains(model.Email))).Select(x => x.GetViewModel).ToList();
}
public StorekeeperViewModel? GetElement(StorekeeperSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login) && (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)))
{
return null;
}
using var context = new STODatabase();
return context.Storekeepers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && x.Email == model.Email && x.Password == model.Password))?.GetViewModel;
}
public StorekeeperViewModel? Insert(StorekeeperBindingModel model)
{
var newStorekeeper = Storekeeper.Create(model);
if(newStorekeeper == null)
{
return null;
}
using var context = new STODatabase();
context.Storekeepers.Add(newStorekeeper);
context.SaveChanges();
return newStorekeeper.GetViewModel;
}
public StorekeeperViewModel? Update(StorekeeperBindingModel model)
{
using var context = new STODatabase();
var storekeeper = context.Storekeepers.FirstOrDefault(x=>x.Id == model.Id);
if(storekeeper == null)
{
return null;
}
storekeeper.Update(model);
context.SaveChanges();
return storekeeper.GetViewModel;
}
public StorekeeperViewModel? Delete(StorekeeperBindingModel model)
{
using var context = new STODatabase();
var storekeeper = context.Storekeepers.FirstOrDefault(x=>x.Id==model.Id);
if(storekeeper != null)
{
context.Storekeepers.Remove(storekeeper);
context.SaveChanges();
return storekeeper.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,79 @@

using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Implements
{
public class WorkDurationStorage : IWorkDurationStorage
{
public List<WorkDurationViewModel> GetFullList()
{
using var context = new STODatabase();
return context.WorkDurations.Select(x => x.GetViewModel).ToList();
}
public List<WorkDurationViewModel> GetFilteredList(WorkDurationSearchModel model)
{
using var context = new STODatabase();
return context.WorkDurations.Select(x => x.GetViewModel).ToList();
}
public WorkDurationViewModel? GetElement(WorkDurationSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.WorkDurations.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public WorkDurationViewModel? Insert(WorkDurationBindingModel model)
{
using var context = new STODatabase();
var newWorkDuration = WorkDuration.Create( model);
if(newWorkDuration == null)
{
return null;
}
context.WorkDurations.Add(newWorkDuration);
context.SaveChanges();
return newWorkDuration.GetViewModel;
}
public WorkDurationViewModel? Update(WorkDurationBindingModel model)
{
using var context = new STODatabase();
var workDuration = context.WorkDurations.FirstOrDefault(x=>x.Id == model.Id);
if(workDuration == null)
{
return null;
}
workDuration.Update(model);
context.SaveChanges();
return workDuration.GetViewModel;
}
public WorkDurationViewModel? Delete(WorkDurationBindingModel model)
{
using var context = new STODatabase();
var workDuration = context.WorkDurations.FirstOrDefault(x=>x.Id==model.Id);
if(workDuration != null)
{
context.WorkDurations.Remove(workDuration);
context.SaveChanges();
return workDuration.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,86 @@

using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StoragesContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using STODatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Implements
{
public class WorkStorage : IWorkStorage
{
public List<WorkViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Works.Select(x => x.GetViewModel).ToList();
}
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
{
if(string.IsNullOrEmpty(model.Title))
{
return new();
}
using var context = new STODatabase();
return context.Works.Select(x => x.GetViewModel).ToList();
}
public WorkViewModel? GetElement(WorkSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title))
{
return null;
}
using var context = new STODatabase();
return context.Works.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
(!string.IsNullOrEmpty(model.Title) && x.Title == model.Title))?.GetViewModel;
}
public WorkViewModel? Insert(WorkBindingModel model)
{
using var context = new STODatabase();
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 STODatabase();
var work = context.Works.FirstOrDefault(x=>x.Id == model.Id);
if(work == null)
{
return null;
}
work.Update(model);
work.UpdateSpares(context, model);
context.SaveChanges();
return work.GetViewModel;
}
public WorkViewModel? Delete(WorkBindingModel model)
{
using var context = new STODatabase();
var work = context.Works.FirstOrDefault(x=>x.Id==model.Id);
if(work != null)
{
context.Works.Remove(work);
context.SaveChanges();
return work.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,92 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace STODatabaseImplement.Models
{
public class Car : ICarModel
{
public int Id { get; set; }
[Required]
public string Brand { get; set; } =string.Empty;
[Required]
public string Model { get; set; } = string.Empty;
[Required]
public string VIN { get; set; } = string.Empty;
[ForeignKey("CarId")]
public virtual List<Service> Services { get; set; } = new();
private Dictionary<int, (ISpareModel, int)>? _carSpares = null;
[NotMapped]
public Dictionary<int, (ISpareModel, int)> CarSpares
{
get
{
if (_carSpares == null)
{
_carSpares = Spares.ToDictionary(recCS => recCS.SpareId, recCS => (recCS.Spare as ISpareModel, recCS.Count));
}
return _carSpares;
}
}
[ForeignKey("CarId")]
public virtual List<CarSpare> Spares { get; set; } = new();
public static Car Create(STODatabase context, CarBindingModel model)
{
return new Car()
{
Id = model.Id,
Brand = model.Brand,
Model = model.Model,
VIN = model.VIN,
Spares = model.CarSpares.Select(x => new CarSpare
{
Car = context.Cars.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(CarBindingModel model)
{
}
public CarViewModel GetViewModel => new()
{
Id = Id,
Brand = Brand,
Model = Model,
VIN = VIN,
CarSpares = CarSpares
};
public void UpdateComponents(STODatabase context, CarBindingModel model)
{
var CarSpares = context.CarSpares.Where(recCS => recCS.CarId == model.Id).ToList();
if (CarSpares != null && CarSpares.Count > 0)
{
context.CarSpares.RemoveRange(CarSpares.Where(rec => !model.CarSpares.ContainsKey(rec.CarId)));
context.SaveChanges();
CarSpares = context.CarSpares.Where(rec => rec.CarId == model.Id).ToList();
foreach (var updateComponent in CarSpares)
{
updateComponent.Count = model.CarSpares[updateComponent.CarId].Item2;
model.CarSpares.Remove(updateComponent.CarId);
}
context.SaveChanges();
}
var Car = context.Cars.First(x => x.Id == Id);
foreach (var pc in model.CarSpares)
{
context.CarSpares.Add(new CarSpare
{
Car = Car,
Spare = context.Spares.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_carSpares = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
namespace STODatabaseImplement.Models
{
public class CarSpare
{
public int Id { get; set; }
[Required]
public int SpareId { get; set; }
[Required]
public int CarId { get; set; }
[Required]
public int Count { get; set; }
public virtual Car Car { get; set; } = new();
public virtual Spare Spare { get; set; } = new();
}
}

View File

@ -0,0 +1,48 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace STODatabaseImplement.Models
{
public class Employer : IEmployerModel
{
public int Id { get; set; }
[Required]
public string Login { get; set; } = String.Empty;
[Required]
public string Password { get; set; } = String.Empty;
[Required]
public string Email { get; set; } = String.Empty;
[ForeignKey("EmployerId")]
public virtual List<Maintenance> Maintenances { get; set; } = new();
public static Employer Create(EmployerBindingModel model)
{
return new Employer()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
Email = model.Email,
};
}
public void Update(EmployerBindingModel model)
{
Login = model.Login;
Password = model.Password;
Email = model.Email;
}
public EmployerViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Email = Email,
Password = Password
};
}
}

View File

@ -0,0 +1,92 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace STODatabaseImplement.Models
{
public class Maintenance : IMaintenanceModel
{
public int Id { get; set; }
[Required]
public int EmployerId { get; set; }
[Required]
public double Cost { get; set; }
[Required]
public DateTime DateCreate { get; set; }
private Dictionary<int, (ICarModel, int)> _maintenanceCars = null;
[NotMapped]
public Dictionary<int, (ICarModel, int)> MaintenanceCars
{
get
{
if (_maintenanceCars == null)
{
_maintenanceCars = Cars
.ToDictionary(recCM => recCM.CarId, recCM => (recCM.Car as ICarModel,recCM.Count));
}
return _maintenanceCars;
}
}
[ForeignKey("MaintenanceId")]
public virtual List<MaintenanceCar> Cars { get; set; } = new();
public static Maintenance Create(STODatabase context, MaintenanceBindingModel model)
{
return new Maintenance()
{
Id = model.Id,
EmployerId = model.EmployerId,
Cars = model.MaintenanceCars.Select(x => new MaintenanceCar
{
Car = context.Cars.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(MaintenanceBindingModel model)
{
Cost = model.Cost;
}
public MaintenanceViewModel GetViewModel => new()
{
Id = Id,
EmployerId = EmployerId,
Cost = Cost,
DateCreate = DateCreate,
MaintenanceCars = MaintenanceCars
};
public void UpdateComponents(STODatabase context, MaintenanceBindingModel model)
{
var MaintenanceCars = context.MaintenanceCars.Where(rec => rec.MaintenanceId == model.Id).ToList();
if (MaintenanceCars != null && MaintenanceCars.Count > 0)
{
context.MaintenanceCars.RemoveRange(MaintenanceCars.Where(rec => !model.MaintenanceCars.ContainsKey(rec.CarId)));
context.SaveChanges();
MaintenanceCars = context.MaintenanceCars.Where(rec => rec.MaintenanceId == model.Id).ToList();
foreach (var updateComponent in MaintenanceCars)
{
updateComponent.Count = model.MaintenanceCars[updateComponent.CarId].Item2;
model.MaintenanceCars.Remove(updateComponent.CarId);
}
context.SaveChanges();
}
var Maintenance = context.Maintenances.First(x => x.Id == Id);
foreach (var pc in model.MaintenanceCars)
{
context.MaintenanceCars.Add(new MaintenanceCar
{
Maintenance = Maintenance,
Car = context.Cars.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_maintenanceCars = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace STODatabaseImplement.Models
{
public class MaintenanceCar
{
public int Id { get; set; }
[Required]
public int MaintenanceId { get; set; }
[Required]
public int CarId { get; set; }
[Required]
public int Count { get; set; }
public virtual Car Car { get; set; } = new();
public virtual Maintenance Maintenance { get; set; } = new();
}
}

View File

@ -0,0 +1,32 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace STODatabaseImplement.Models
{
public class Service : IServiceModel
{
public int Id { get; set; }
[Required]
public string ServiceDescription { get; set; } = string.Empty;
public static Service Create(ServiceBindingModel model)
{
return new Service()
{
Id = model.Id,
ServiceDescription = model.ServiceDescription,
};
}
public void Update(ServiceBindingModel model)
{
ServiceDescription = model.ServiceDescription;
}
public ServiceViewModel GetViewModel => new()
{
Id = Id,
ServiceDescription = ServiceDescription,
};
}
}

View File

@ -0,0 +1,43 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace STODatabaseImplement.Models
{
public class Spare : ISpareModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = String.Empty;
[Required]
public double Price { get; set; }
public static Spare Create(SpareBindingModel model)
{
return new Spare
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
};
}
public void Update(SpareBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
public SpareViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
};
}
}

View File

@ -0,0 +1,53 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.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 STODatabaseImplement.Models
{
public class Storekeeper : IStorekeeperModel
{
public int Id { get; set; }
[Required]
public string Login { get; set; } = String.Empty;
[Required]
public string Password { get; set; } = String.Empty;
[Required]
public string Email { get; set; } = String.Empty;
[ForeignKey("StorekeeperId")]
public virtual List<Work> Works { get; set; } = new();
public static Storekeeper Create(StorekeeperBindingModel model)
{
return new Storekeeper()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
Email = model.Email,
};
}
public void Update(StorekeeperBindingModel model)
{
Login = model.Login;
Password = model.Password;
Email = model.Email;
}
public StorekeeperViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Email = Email,
Password = Password
};
}
}

View File

@ -0,0 +1,154 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Models
{
public class Work : IWorkModel
{
public int Id { get; set; }
[Required]
public string Title { get; set; } = String.Empty;
[Required]
public double Price { get; set; }
[Required]
public int StorekeeperId { get; set; }
[Required]
public int DurationId { get; set; }
public DateTime Date { get; set; } = DateTime.Now;
private Dictionary<int, (ISpareModel, int)>? _workSpares { get; set; } = null;
[NotMapped]
public Dictionary<int, (ISpareModel, int)> WorkSpares {
get
{
if (_workSpares == null)
{
_workSpares = Spares.ToDictionary(recWS => recWS.SpareId, recWS =>
(recWS.Spare as ISpareModel, recWS.Count));
}
return _workSpares;
}
}
[ForeignKey("WorkId")]
public virtual List<WorkSpare> Spares { get; set; } = new();
private Dictionary<int, (IMaintenanceModel, int)>? _workMaintenances = null;
[NotMapped]
public Dictionary<int, (IMaintenanceModel, int)> WorkMaintences
{
get
{
if (_workMaintenances == null)
{
_workMaintenances = Maintences.ToDictionary(recWM => recWM.MaintenceId, recWM =>
(recWM.Maintenance as IMaintenanceModel, recWM.Count));
}
return _workMaintenances;
}
}
[ForeignKey("WorkId")]
public virtual List<WorkMaintence> Maintences { get; set; } = new();
public static Work Create(STODatabase context, WorkBindingModel model)
{
return new Work()
{
Id = model.Id,
Title = model.Title,
Price = model.Price,
Spares = model.WorkSpares.Select(x => new WorkSpare {
Spare = context.Spares.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Maintences = model.WorkMaintences.Select(x => new WorkMaintence
{
Maintenance = context.Maintenances.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(WorkBindingModel model)
{
Title = model.Title;
Price = model.Price;
}
public WorkViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
Price = Price,
};
public void UpdateSpares(STODatabase context, WorkBindingModel model)
{
var WorkSpares = context.WorkSpares.Where(rec => rec.WorkId == model.Id).ToList();
if (WorkSpares != null && WorkSpares.Count > 0)
{
context.WorkSpares.RemoveRange(WorkSpares.Where(rec => !model.WorkSpares.ContainsKey(rec.SpareId)));
context.SaveChanges();
foreach (var updateComponent in WorkSpares)
{
updateComponent.Count = model.WorkSpares[updateComponent.SpareId].Item2;
model.WorkSpares.Remove(updateComponent.SpareId);
}
context.SaveChanges();
}
var Work = context.Works.First(x => x.Id == Id);
foreach (var pc in model.WorkSpares)
{
context.WorkSpares.Add(new WorkSpare
{
Work = Work,
Spare = context.Spares.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_workSpares = null;
}
public void UpdateMaintences(STODatabase context, WorkBindingModel model)
{
var WorkMaintence = context.WorkMaintences.Where(rec => rec.WorkId == model.Id).ToList();
if (WorkMaintence != null && WorkMaintence.Count > 0)
{
context.WorkMaintences.RemoveRange(WorkMaintence.Where(rec => !model.WorkMaintences.ContainsKey(rec.MaintenceId)));
context.SaveChanges();
foreach (var updateComponent in WorkMaintence)
{
updateComponent.Count = model.WorkMaintences[updateComponent.MaintenceId].Item2;
model.WorkMaintences.Remove(updateComponent.MaintenceId);
}
context.SaveChanges();
}
var Work = context.Works.First(x => x.Id == Id);
foreach (var pc in model.WorkMaintences)
{
context.WorkMaintences.Add(new WorkMaintence
{
Work = Work,
Maintenance = context.Maintenances.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_workMaintenances = null;
}
}
}

View File

@ -0,0 +1,43 @@
using STOContracts.BindingModels;
using STOContracts.ViewModels;
using STODataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Models
{
public class WorkDuration : IWorkDurationModel
{
public int Id { get; set; }
[Required]
public int Duration { get; set; }
[ForeignKey("DurationId")]
public virtual List<Work> Works { get; set; }
public static WorkDuration Create(WorkDurationBindingModel model)
{
return new WorkDuration()
{
Id = model.Id,
Duration = model.Duration,
};
}
public void Update(WorkDurationBindingModel model)
{
Duration = model.Duration;
}
public WorkDurationViewModel GetViewModel => new()
{
Id = Id,
Duration = Duration,
};
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Models
{
public class WorkMaintence
{
public int Id { get; set; }
[Required]
public int WorkId { get; set; }
[Required]
public int MaintenceId { get; set; }
[Required]
public int Count { get; set; }
public virtual Work Work { get; set; } = new();
public virtual Maintenance Maintenance { get; set; } = new();
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STODatabaseImplement.Models
{
public class WorkSpare
{
public int Id { get; set; }
[Required]
public int WorkId { get; set; }
[Required]
public int SpareId { get; set; }
[Required]
public int Count { get; set; }
public virtual Work Work { get; set; } = new();
public virtual Spare Spare { get; set; } = new();
}
}

View File

@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using STODatabaseImplement.Models;
using System.Diagnostics.Metrics;
namespace STODatabaseImplement
{
public class STODatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=STO;Integrated Security=True;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Car> Cars { set; get; }
public virtual DbSet<CarSpare> CarSpares { set; get; }
public virtual DbSet<Employer> Employers { set; get; }
public virtual DbSet<Maintenance> Maintenances { set; get; }
public virtual DbSet<MaintenanceCar> MaintenanceCars { set; get; }
public virtual DbSet<Service> Services { set; get; }
public virtual DbSet<Spare> Spares { set; get; }
public virtual DbSet<Storekeeper> Storekeepers { set; get; }
public virtual DbSet<Work> Works { set; get; }
public virtual DbSet<WorkDuration> WorkDurations { set; get; }
public virtual DbSet<WorkMaintence> WorkMaintences { set; get; }
public virtual DbSet<WorkSpare> WorkSpares { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\STOContracts\STOContracts.csproj" />
<ProjectReference Include="..\STODataModels\STODataModels.csproj" />
</ItemGroup>
</Project>