diff --git a/STODataBaseImplement/STODatabase.cs b/STODataBaseImplement/STODatabase.cs new file mode 100644 index 0000000..83f27ee --- /dev/null +++ b/STODataBaseImplement/STODatabase.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.SqlServer; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DatabaseImplement; + +namespace STODatabaseImplement +{ + public class STODatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=STODataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Clients { set; get; } + public virtual DbSet TechnicalWorks { set; get; } + public virtual DbSet Cars { set; get; } + public virtual DbSet Services { set; get; } + public virtual DbSet CarParts { set; get; } + public virtual DbSet Works { set; get; } + + //public virtual DbSet CarPartCar { set; get; } + public virtual DbSet CarPartWork { set; get; } + public virtual DbSet CarTechnicalWork { set; get; } + public virtual DbSet WorkTechnicalWork { set; get; } + } +} \ No newline at end of file diff --git a/STODataBaseImplement/ServiceStorage.cs b/STODataBaseImplement/ServiceStorage.cs new file mode 100644 index 0000000..c3ecfb0 --- /dev/null +++ b/STODataBaseImplement/ServiceStorage.cs @@ -0,0 +1,86 @@ +using STOContracts.BindingModels; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using STODatabaseImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement +{ + public class ServiceStorage : IServiceStorage + { + public List GetFullList() + { + using var context = new STODatabase(); + return context.Services.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ServiceSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new STODatabase(); + return context.Services.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public ServiceViewModel? GetElement(ServiceSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new STODatabase(); + return context.Services.FirstOrDefault(x => + (x.Id == model.Id)) + ?.GetViewModel; + } + + public ServiceViewModel? Insert(ServiceBindingModel model) + { + using var context = new STODatabase(); + if (model == null) + return null; + var newService = Service.Create(context, 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(); + var component = context.Services.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + 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; + } + } + +} diff --git a/STODataBaseImplement/TechnicalWork.cs b/STODataBaseImplement/TechnicalWork.cs new file mode 100644 index 0000000..49e3c98 --- /dev/null +++ b/STODataBaseImplement/TechnicalWork.cs @@ -0,0 +1,114 @@ +using STOContracts.BindingModels; +using STOContracts.ViewModels; +using STODatabaseImplement; +using STODataModels; +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 DatabaseImplement +{ + public class TechnicalWork: ITechnicalWorkModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + public string Description { get; private set; } = string.Empty; + [Required] + public DateTime Date { get; private set; } = DateTime.MinValue; + + public Dictionary _Works = null; + + [NotMapped] + public Dictionary TechnicalworkWorks + { + get + { + if (_Works == null) + { + _Works = Works + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.Work as IWorkModel, recPC.Count)); + } + return _Works; + } + } + [ForeignKey("TechnicalWorkId")] + public virtual List Works { get; set; } = new(); + + + public static TechnicalWork? Create(STODatabase context, TechnicalWorkBindingModel model) + { + if (model == null) + { + return null; + } + return new TechnicalWork() + { + Id = model.Id, + Name = model.Name, + Description = model.Description, + Date = model.Date, + Works = model.TechnicalworkWorks.Select(x => new + WorkTechnicalWork + { + Work = context.Works.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(TechnicalWorkBindingModel model) + { + if (model == null) + { + return; + } + Id = model.Id; + Name = model.Name; + Description = model.Description; + Date = model.Date; + } + public TechnicalWorkViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Description = Description, + Date = Date + }; + + public void UpdateWorks(STODatabase context, TechnicalWorkBindingModel model) + { + var works = context.WorkTechnicalWork.Where(rec => + rec.TechnicalWorkId == model.Id).ToList(); + if (works != null && TechnicalworkWorks.Count > 0) + { + context.WorkTechnicalWork.RemoveRange(works.Where(rec + => !model.TechnicalworkWorks.ContainsKey(rec.WorkId))); + context.SaveChanges(); + foreach (var updateComponent in works) + { + updateComponent.Count = + model.TechnicalworkWorks[updateComponent.WorkId].Item2; + model.TechnicalworkWorks.Remove(updateComponent.WorkId); + } + context.SaveChanges(); + } + var manufacture = context.TechnicalWorks.First(x => x.Id == Id); + foreach (var pc in model.TechnicalworkWorks) + { + context.WorkTechnicalWork.Add(new WorkTechnicalWork + { + TechnicalWork = manufacture, + Work = context.Works.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _Works = null; + } + } +} diff --git a/STODataBaseImplement/TechnicalWorkStorage.cs b/STODataBaseImplement/TechnicalWorkStorage.cs new file mode 100644 index 0000000..5a6bb75 --- /dev/null +++ b/STODataBaseImplement/TechnicalWorkStorage.cs @@ -0,0 +1,110 @@ +using Microsoft.EntityFrameworkCore; +using STOContracts.BindingModels; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; +using STODatabaseImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement +{ + public class TechnicalWorkStorage : ITechnicalWorkStorage + { + public List GetFullList() + { + using var context = new STODatabase(); + return context.TechnicalWorks + .Include(x => x.Works) + .ThenInclude(x => x.Work) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(TechnicalWorkSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new STODatabase(); + return context.TechnicalWorks.Include(x => x.Works) + .ThenInclude(x => x.Work) + .Where(x => x.Name.Contains(model.Name)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public TechnicalWorkViewModel? GetElement(TechnicalWorkSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new STODatabase(); + return context.TechnicalWorks + .Include(x => x.Works) + .ThenInclude(x => x.Work) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && + x.Name == model.Name) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public TechnicalWorkViewModel? Insert(TechnicalWorkBindingModel model) + { + using var context = new STODatabase(); + var newTechnicalWork = TechnicalWork.Create(context, model); + if (newTechnicalWork == null) + { + return null; + } + context.TechnicalWorks.Add(newTechnicalWork); + context.SaveChanges(); + return newTechnicalWork.GetViewModel; + } + public TechnicalWorkViewModel? Update(TechnicalWorkBindingModel model) + { + using var context = new STODatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var car = context.TechnicalWorks.FirstOrDefault(rec => + rec.Id == model.Id); + if (car == null) + { + return null; + } + car.Update(model); + context.SaveChanges(); + car.UpdateWorks(context, model); + transaction.Commit(); + return car.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public TechnicalWorkViewModel? Delete(TechnicalWorkBindingModel model) + { + using var context = new STODatabase(); + var element = context.TechnicalWorks + .Include(x => x.Works) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.TechnicalWorks.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} + diff --git a/STODataBaseImplement/Work.cs b/STODataBaseImplement/Work.cs new file mode 100644 index 0000000..d9e1bb0 --- /dev/null +++ b/STODataBaseImplement/Work.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using STODataModels; +using STOContracts.BindingModels; +using STOContracts.ViewModels; +using STODatabaseImplement; + +namespace DatabaseImplement +{ + public class Work: IWorkModel + { + public int Id { get; private set; } + + [Required] + public string Name { get; private set; } = string.Empty; + + public int Time_duration { get; private set; } = 0; + public Dictionary _CarParts = null; + + [NotMapped] + public Dictionary CarParts + { + get + { + if (_CarParts == null) + { + _CarParts = CarPartCar + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.CarPart as ICarPartModel, recPC.Count)); + } + return _CarParts; + } + } + [ForeignKey("WorkId")] + public virtual List CarPartCar { get; set; } = new(); + + + public static Work? Create(STODatabase context, WorkBindingModel model) + { + if (model == null) + { + return null; + } + return new Work() + { + Id = model.Id, + Name = model.Name, + Time_duration = model.Time_duration, + CarPartCar = model.CarParts.Select(x => new + CarPartWork + { + CarPart = context.CarParts.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(WorkBindingModel model) + { + if (model == null) + { + return; + } + Id = model.Id; + Name = model.Name; + Time_duration = model.Time_duration; + } + public WorkViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Time_duration = Time_duration + }; + + public void UpdateCarParts(STODatabase context, WorkBindingModel model) + { + var parts = context.CarPartWork.Where(rec => + rec.WorkId == model.Id).ToList(); + if (parts != null && CarParts.Count > 0) + { + context.CarPartWork.RemoveRange(parts.Where(rec + => !model.CarParts.ContainsKey(rec.CarPartId))); + context.SaveChanges(); + foreach (var updateComponent in parts) + { + updateComponent.Count = + model.CarParts[updateComponent.CarPartId].Item2; + model.CarParts.Remove(updateComponent.CarPartId); + } + context.SaveChanges(); + } + var manufacture = context.Works.First(x => x.Id == Id); + foreach (var pc in model.CarParts) + { + context.CarPartWork.Add(new CarPartWork + { + Work = manufacture, + CarPart = context.CarParts.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _CarParts = null; + } + } +}