Закончил databaseimplements

This commit is contained in:
Алексей Тихоненков 2024-04-30 20:39:36 +04:00
parent 647b797a19
commit 95973b888b
6 changed files with 299 additions and 2 deletions

View File

@ -5,5 +5,6 @@
public int? Id { get; set; }
public int? UserId { get; set; }
public string ComponentName { get; set; }
}
}

View File

@ -0,0 +1,85 @@
using DiningRoomContracts.BindingModels;
using DiningRoomContracts.SearchModels;
using DiningRoomContracts.StorageContracts;
using DiningRoomContracts.ViewModels;
using DiningRoomDatabaseImplement.Models;
namespace DiningRoomDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
//
public List<ComponentViewModel> GetFullList()
{
using var context = new DiningRoomDatabase();
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 DiningRoomDatabase();
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 DiningRoomDatabase();
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 DiningRoomDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new DiningRoomDatabase();
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 DiningRoomDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,106 @@
using DiningRoomContracts.BindingModels;
using DiningRoomContracts.SearchModels;
using DiningRoomContracts.StorageContracts;
using DiningRoomContracts.ViewModels;
using DiningRoomDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace DiningRoomDatabaseImplement.Implements
{
public class DrinkStorage : IDrinkStorage
{
public List<DrinkViewModel> GetFullList()
{
using var context = new DiningRoomDatabase();
return context.Drinks
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<DrinkViewModel> GetFilteredList(DrinkSearchModel model)
{
if (string.IsNullOrEmpty(model.DrinkName))
{
return new();
}
using var context = new DiningRoomDatabase();
return context.Drinks
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.DrinkName.Contains(model.DrinkName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public DrinkViewModel? GetElement(DrinkSearchModel model)
{
if (string.IsNullOrEmpty(model.DrinkName) && !model.Id.HasValue)
{
return null;
}
using var context = new DiningRoomDatabase();
return context.Drinks
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrinkName) && x.DrinkName == model.DrinkName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public DrinkViewModel? Insert(DrinkBindingModel model)
{
using var context = new DiningRoomDatabase();
var newDrink = Drink.Create(context, model);
if (newDrink == null)
{
return null;
}
context.Drinks.Add(newDrink);
context.SaveChanges();
return newDrink.GetViewModel;
}
public DrinkViewModel? Update(DrinkBindingModel model)
{
using var context = new DiningRoomDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Drink = context.Drinks.FirstOrDefault(rec => rec.Id == model.Id);
if (Drink == null)
{
return null;
}
Drink.Update(model);
context.SaveChanges();
Drink.UpdateComponents(context, model);
transaction.Commit();
return Drink.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DrinkViewModel? Delete(DrinkBindingModel model)
{
using var context = new DiningRoomDatabase();
var element = context.Drinks
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Drinks.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -13,7 +13,6 @@ using System.Threading.Tasks;
namespace DiningRoomDatabaseImplement.Implements
{
//!!!ПОДОБИЕ component
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)

View File

@ -0,0 +1,106 @@
using DiningRoomContracts.BindingModels;
using DiningRoomContracts.SearchModels;
using DiningRoomContracts.StorageContracts;
using DiningRoomContracts.ViewModels;
using DiningRoomDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace DiningRoomDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public List<ProductViewModel> GetFullList()
{
using var context = new DiningRoomDatabase();
return context.Products
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName))
{
return new();
}
using var context = new DiningRoomDatabase();
return context.Products
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.ProductName.Contains(model.ProductName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue)
{
return null;
}
using var context = new DiningRoomDatabase();
return context.Products
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new DiningRoomDatabase();
var newProduct = Product.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Products.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new DiningRoomDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Product = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
if (Product == null)
{
return null;
}
Product.Update(model);
context.SaveChanges();
Product.UpdateComponents(context, model);
transaction.Commit();
return Product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new DiningRoomDatabase();
var element = context.Products
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Products.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -44,7 +44,7 @@ namespace DiningRoomDatabaseImplement.Models
Unit = Model.Unit;
}
public ComponentViewModel ViewModel => new()
public ComponentViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,