diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs index 1378964..3dcc2c0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs @@ -3,6 +3,5 @@ public class CommentSearchModel { public int? Id { get; set; } - public DateTime? Date { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 046e6d7..7c72d29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,6 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int? StoreKeeperId { get; set; } + public int? VendorId { get; set; } + public int? Build { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index ed748fe..21c4265 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -4,6 +4,5 @@ { public int? Id { get; set; } public string? Login { get; set; } - public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs new file mode 100644 index 0000000..a357455 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs @@ -0,0 +1,77 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class CommentStorage : ICommentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CommentSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public CommentViewModel? GetElement(CommentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CommentViewModel? Insert(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComment = Comment.Create(model); + if (newComment == null) + { + return null; + } + context.Comments.Add(newComment); + context.SaveChanges(); + return newComment.GetViewModel; + } + public CommentViewModel? Update(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + + public CommentViewModel? Delete(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Comments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5bb8224..9fdc559 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -58,14 +58,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(c => c.Id == model.Id); - if (component == null) + var element = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (element == null) { return null; } - component.Update(model); + element.Update(model); context.SaveChanges(); - return component.GetViewModel; + return element.GetViewModel; } public ComponentViewModel? Delete(ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs new file mode 100644 index 0000000..bd4aff8 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs @@ -0,0 +1,86 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class StoreKeeperStorage : IStoreKeeperStorage + { + public StoreKeeperViewModel? GetElement(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Include(x => x.Components) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreKeeperViewModel? Insert(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newStoreKeeper = StoreKeeper.Create(context, model); + if (newStoreKeeper == null) + { + return null; + } + context.StoreKeepers.Add(newStoreKeeper); + context.SaveChanges(); + return newStoreKeeper.GetViewModel; + } + + public StoreKeeperViewModel? Update(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public StoreKeeperViewModel? Delete(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.StoreKeepers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs new file mode 100644 index 0000000..1e74a2a --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class VendorStorage : IVendorStorage + { + public VendorViewModel? GetElement(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Select(x => x.GetViewModel) + .ToList(); + } + + public VendorViewModel? Insert(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newVendor = Vendor.Create(context, model); + if (newVendor == null) + { + return null; + } + context.Vendors.Add(newVendor); + context.SaveChanges(); + return newVendor.GetViewModel; + } + + public VendorViewModel? Update(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public VendorViewModel? Delete(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Vendors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index e26f654..4acbece 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,13 +14,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public virtual StoreKeeper? StoreKeeper { get; set; } + public virtual StoreKeeper? StoreKeeperЗЛ { get; set; } [NotMapped] - IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); + public IStoreKeeperModel? StoreKeeper { get; set; } [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) + public static Component? Create(ComponentBindingModel model) { if (model == null) { @@ -31,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) + StoreKeeper = model.StoreKeeper }; } public void Update (ComponentBindingModel model)