Creating models and some realization

This commit is contained in:
2023-04-07 16:10:59 +04:00
parent a07e4a771a
commit 4ee76a9693
9 changed files with 408 additions and 17 deletions

View File

@@ -0,0 +1,87 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class EquipmentReceivingStorage : IEquipmentReceivingStorage
{
public EquipmentReceivingViewModel? Delete(EquipmentReceivingBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.EquipmentReceivings.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.EquipmentReceivings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public EquipmentReceivingViewModel? GetElement(EquipmentReceivingSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<EquipmentReceivingViewModel> GetFilteredList(EquipmentReceivingSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<EquipmentReceivingViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.EquipmentReceivings.Select(x => x.GetViewModel).ToList();
}
public EquipmentReceivingViewModel? Insert(EquipmentReceivingBindingModel model)
{
var newOrder = EquipmentReceiving.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ComputerShopDatabase();
context.EquipmentReceivings.Add(newOrder);
context.SaveChanges();
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public EquipmentReceivingViewModel? Update(EquipmentReceivingBindingModel model)
{
using var context = new ComputerShopDatabase();
var order = context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
}
}

View File

@@ -15,17 +15,42 @@ namespace ComputerShopDatabaseImplement.Implements
{
public OrderViewModel? Delete(OrderBindingModel model)
{
throw new NotImplementedException();
using var context = new ComputerShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
throw new NotImplementedException();
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Orders
.FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
throw new NotImplementedException();
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputerShopDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()

View File

@@ -0,0 +1,113 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class SupplyStorage : ISupplyStorage
{
public SupplyViewModel? Delete(SupplyBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.Supplies
.Include(x => x.Orders)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Supplies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SupplyViewModel? GetElement(SupplySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Supplies
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.FirstOrDefault(x =>
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputerShopDatabase();
return context.Supplies
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.Where(x => x.Id == model.Id)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SupplyViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Supplies
.Include(x => x.Orders)
.ThenInclude(x => x.Order)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SupplyViewModel? Insert(SupplyBindingModel model)
{
using var context = new ComputerShopDatabase();
var newProduct = Supply.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Supplies.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public SupplyViewModel? Update(SupplyBindingModel model)
{
using var context = new ComputerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Supplies.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateOrders(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}