comment, storekeeper, vendor + изменения в их поисковых моделях

This commit is contained in:
dex_moth 2024-05-01 11:09:52 +04:00
parent 1ae74a6fdb
commit e249402043
8 changed files with 257 additions and 10 deletions

View File

@ -3,6 +3,5 @@
public class CommentSearchModel public class CommentSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public DateTime? Date { get; set; }
} }
} }

View File

@ -4,5 +4,7 @@
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public int? VendorId { get; set; }
public int? Build { get; set; }
} }
} }

View File

@ -4,6 +4,5 @@
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string? Login { get; set; } public string? Login { get; set; }
public string? Password { get; set; }
} }
} }

View File

@ -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<CommentViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Comments
.Select(x => x.GetViewModel)
.ToList();
}
public List<CommentViewModel> 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;
}
}
}

View File

@ -58,14 +58,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
public ComponentViewModel? Update(ComponentBindingModel model) public ComponentViewModel? Update(ComponentBindingModel model)
{ {
using var context = new ComputerHardwareStoreDBContext(); using var context = new ComputerHardwareStoreDBContext();
var component = context.Components.FirstOrDefault(c => c.Id == model.Id); var element = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null) if (element == null)
{ {
return null; return null;
} }
component.Update(model); element.Update(model);
context.SaveChanges(); context.SaveChanges();
return component.GetViewModel; return element.GetViewModel;
} }
public ComponentViewModel? Delete(ComponentBindingModel model) public ComponentViewModel? Delete(ComponentBindingModel model)

View File

@ -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<StoreKeeperViewModel> 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<StoreKeeperViewModel> 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;
}
}
}

View File

@ -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<VendorViewModel> 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<VendorViewModel> 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;
}
}
}

View File

@ -14,13 +14,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models
[Required] [Required]
public double Cost { get; set; } public double Cost { get; set; }
[Required] [Required]
public virtual StoreKeeper? StoreKeeper { get; set; } public virtual StoreKeeper? StoreKeeperЗЛ { get; set; }
[NotMapped] [NotMapped]
IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); public IStoreKeeperModel? StoreKeeper { get; set; }
[ForeignKey("ComponentId")] [ForeignKey("ComponentId")]
public virtual List<ProductComponent> ProductComponents { get; set; } = new(); public virtual List<ProductComponent> ProductComponents { get; set; } = new();
public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) public static Component? Create(ComponentBindingModel model)
{ {
if (model == null) if (model == null)
{ {
@ -31,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models
Id = model.Id, Id = model.Id,
Name = model.Name, Name = model.Name,
Cost = model.Cost, Cost = model.Cost,
StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) StoreKeeper = model.StoreKeeper
}; };
} }
public void Update (ComponentBindingModel model) public void Update (ComponentBindingModel model)