законченная 3 лаба

This commit is contained in:
evasina2312@gmail.com 2023-04-11 20:40:25 +04:00
parent ea077a3fa4
commit d30cd30aef
5 changed files with 184 additions and 14 deletions

View File

@ -1,7 +1,7 @@
using ComputersShopBusinessLogic; using ComputersShopBusinessLogic;
using ComputersShopContracts.BusinessLogicsContracts; using ComputersShopContracts.BusinessLogicsContracts;
using ComputersShopContracts.StoragesContracts; using ComputersShopContracts.StoragesContracts;
using ComputersShopFileImplement.Implements; using ComputersShopDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;

View File

@ -6,7 +6,7 @@ using ComputersShopDatabaseImplement.Models;
namespace ComputersShopDatabaseImplement.Implements namespace ComputersShopDatabaseImplement.Implements
{ {
internal class ComponentStorage : IComponentStorage public class ComponentStorage : IComponentStorage
{ {
public List<ComponentViewModel> GetFullList() public List<ComponentViewModel> GetFullList()
{ {

View File

@ -1,12 +1,102 @@
using System; using ComputersShopContracts.BindingModels;
using System.Collections.Generic; using ComputersShopContracts.SearchModels;
using System.Linq; using ComputersShopContracts.StoragesContracts;
using System.Text; using ComputersShopContracts.ViewModels;
using System.Threading.Tasks; using ComputersShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputersShopDatabaseImplement.Implements namespace ComputersShopDatabaseImplement.Implements
{ {
internal class ComputerStorage public class ComputerStorage : IComputerStorage
{ {
public List<ComputerViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComputerViewModel> GetFilteredList(ComputerSearchModel model)
{
if (string.IsNullOrEmpty(model.ComputerName))
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.ComputerName.Contains(model.ComputerName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ComputerViewModel? GetElement(ComputerSearchModel model)
{
if (string.IsNullOrEmpty(model.ComputerName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && x.ComputerName == model.ComputerName) || (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComputerViewModel? Insert(ComputerBindingModel model)
{
using var context = new ComputersShopDatabase();
var newComputer = Computer.Create(context, model);
if (newComputer == null)
{
return null;
}
context.Computers.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
}
public ComputerViewModel? Update(ComputerBindingModel model)
{
using var context = new ComputersShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var computer = context.Computers.FirstOrDefault(rec =>
rec.Id == model.Id);
if (computer == null)
{
return null;
}
computer.Update(model);
context.SaveChanges();
computer.UpdateComponents(context, model);
transaction.Commit();
return computer.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ComputerViewModel? Delete(ComputerBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Computers
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Computers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
} }
} }

View File

@ -1,12 +1,91 @@
using System; using ComputersShopContracts.BindingModels;
using System.Collections.Generic; using ComputersShopContracts.SearchModels;
using System.Linq; using ComputersShopContracts.StoragesContracts;
using System.Text; using ComputersShopContracts.ViewModels;
using System.Threading.Tasks; using ComputersShopDatabaseImplement.Models;
namespace ComputersShopDatabaseImplement.Implements namespace ComputersShopDatabaseImplement.Implements
{ {
internal class OrderStorage public class OrderStorage : IOrderStorage
{ {
public List<OrderViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
List<OrderViewModel> orderList = context.Orders
.Select(x => x.GetViewModel)
.ToList();
foreach (var order in orderList)
{
string manufactureName = context.Computers
.SingleOrDefault(x => x.Id == order.ComputerId)?.ComputerName ?? string.Empty;
order.ComputerName = manufactureName;
}
return orderList;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputersShopDatabase();
List<OrderViewModel> orderList = context.Orders
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
foreach (var order in orderList)
{
string manufactureName = context.Computers
.SingleOrDefault(x => x.Id == order.ComputerId)?.ComputerName ?? string.Empty;
order.ComputerName = manufactureName;
}
return orderList;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Orders
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ComputersShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new ComputersShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
} }
} }

View File

@ -20,6 +20,7 @@ namespace ComputersShopDatabaseImplement.Models
[Required] [Required]
public DateTime DateCreate { get; private set; } = DateTime.Now; public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; } public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel model) public static Order? Create(OrderBindingModel model)
{ {
if (model == null) if (model == null)