Реализованы классы хранилища работающие с бд
This commit is contained in:
parent
4702f666e1
commit
1a8bfc323e
@ -1,8 +1,8 @@
|
|||||||
using ConfectioneryFileImplement.Implements;
|
using ConfectioneryDatabaseImplement.Implements;
|
||||||
|
using ConfectioneryDatabaseImplement;
|
||||||
using ConfectioneryBusinessLogic.BusinessLogics;
|
using ConfectioneryBusinessLogic.BusinessLogics;
|
||||||
using ConfectioneryContracts.BusinessLogicsContracts;
|
using ConfectioneryContracts.BusinessLogicsContracts;
|
||||||
using ConfectioneryContracts.StoragesContract;
|
using ConfectioneryContracts.StoragesContract;
|
||||||
using ConfectioneryFileImplement;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NLog.Extensions.Logging;
|
using NLog.Extensions.Logging;
|
||||||
|
80
ConfectioneryDatabaseImplement/ComponentStorage.cs
Normal file
80
ConfectioneryDatabaseImplement/ComponentStorage.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
using ConfectioneryContracts.BindingModels;
|
||||||
|
using ConfectioneryContracts.SearchModels;
|
||||||
|
using ConfectioneryContracts.StoragesContract;
|
||||||
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDatabaseImplement.Models;
|
||||||
|
|
||||||
|
namespace ConfectioneryDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
public class ComponentStorage : IComponentStorage
|
||||||
|
{
|
||||||
|
public List<ComponentViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Components
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.ComponentName))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Components
|
||||||
|
.Where(x => x.ComponentName.Contains(model.ComponentName))
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Components
|
||||||
|
.FirstOrDefault(x =>
|
||||||
|
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
|
||||||
|
(model.Id.HasValue && x.Id == model.Id))
|
||||||
|
?.GetViewModel;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Insert(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
var newComponent = Component.Create(model);
|
||||||
|
if (newComponent == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
context.Components.Add(newComponent);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newComponent.GetViewModel;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (component == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
component.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Components.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ namespace ConfectioneryDatabaseImplement
|
|||||||
{
|
{
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
|
// Перед работой необходимо установить SQL Express
|
||||||
if (optionsBuilder.IsConfigured == false)
|
if (optionsBuilder.IsConfigured == false)
|
||||||
{
|
{
|
||||||
optionsBuilder.UseSqlServer(@"
|
optionsBuilder.UseSqlServer(@"
|
||||||
|
74
ConfectioneryDatabaseImplement/OrderStorage.cs
Normal file
74
ConfectioneryDatabaseImplement/OrderStorage.cs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
using ConfectioneryContracts.BindingModels;
|
||||||
|
using ConfectioneryContracts.SearchModels;
|
||||||
|
using ConfectioneryContracts.StoragesContract;
|
||||||
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDatabaseImplement.Models;
|
||||||
|
|
||||||
|
namespace ConfectioneryDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
public class OrderStorage : IOrderStorage
|
||||||
|
{
|
||||||
|
public OrderViewModel? Delete(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Orders.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
if (!model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||||
|
{
|
||||||
|
var result = GetElement(model);
|
||||||
|
return result != null ? new() { result } : new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Orders
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel? Insert(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
var newOrder = Order.Create(model);
|
||||||
|
if (newOrder == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
context.Orders.Add(newOrder);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newOrder.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel? Update(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (order == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
order.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return order.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
89
ConfectioneryDatabaseImplement/PastryStorage.cs
Normal file
89
ConfectioneryDatabaseImplement/PastryStorage.cs
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
using ConfectioneryContracts.BindingModels;
|
||||||
|
using ConfectioneryContracts.SearchModels;
|
||||||
|
using ConfectioneryContracts.StoragesContract;
|
||||||
|
using ConfectioneryContracts.ViewModels;
|
||||||
|
using ConfectioneryDatabaseImplement;
|
||||||
|
using ConfectioneryDatabaseImplement.Models;
|
||||||
|
|
||||||
|
namespace ConfectioneryDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
public class PastryStorage : IPastryStorage
|
||||||
|
{
|
||||||
|
public PastryViewModel? Delete(PastryBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var element = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Pastries.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PastryViewModel? GetElement(PastrySearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Pastries.FirstOrDefault
|
||||||
|
(x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) ||
|
||||||
|
(model.Id.HasValue && x.Id == model.Id)
|
||||||
|
)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.PastryName))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Pastries
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.Where(x => x.PastryName.Contains(model.PastryName))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PastryViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
return context.Pastries
|
||||||
|
.Select(x => x.GetViewModel)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PastryViewModel? Insert(PastryBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var newPastry = Pastry.Create(context, model);
|
||||||
|
if (newPastry == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
newPastry = context.Pastries.Add(newPastry).Entity;
|
||||||
|
context.SaveChanges();
|
||||||
|
model.Id = newPastry.Id;
|
||||||
|
newPastry.UpdateComponents(context, model);
|
||||||
|
return newPastry.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PastryViewModel? Update(PastryBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ConfectioneryDatabase();
|
||||||
|
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (pastry == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
pastry.Update(model);
|
||||||
|
pastry.UpdateComponents(context, model);
|
||||||
|
context.Update(pastry);
|
||||||
|
context.SaveChanges();
|
||||||
|
return pastry.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user