PIbd-22_Kaznacheeva.E.K._So.../SoftwareInstallation/SoftwareInstallationDatabaseImplement/OrderStorage.cs

171 lines
5.8 KiB
C#
Raw Normal View History

2024-03-26 15:23:03 +04:00
using Microsoft.EntityFrameworkCore;
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoftwareInstallationDatabaseImplement
{
2024-03-27 10:21:01 +04:00
//public class OrderStorage : IOrderStorage
//{
// public OrderViewModel? Delete(OrderBindingModel model)
// {
// using var context = new SoftwareInstallationDatabase();
// 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 SoftwareInstallationDatabase();
// if (!model.Id.HasValue)
// {
// return null;
// }
// return context.Orders
// .Include(x => x.Package)
// .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 SoftwareInstallationDatabase();
// return context.Orders
// .Include(x => x.Package)
// .Select(x => x.GetViewModel)
// .ToList();
// }
// public OrderViewModel? Insert(OrderBindingModel model)
// {
// var newOrder = Order.Create(model);
// if (newOrder == null)
// {
// return null;
// }
// using var context = new SoftwareInstallationDatabase();
// context.Orders.Add(newOrder);
// context.SaveChanges();
// return newOrder.GetViewModel;
// }
// public OrderViewModel? Update(OrderBindingModel model)
// {
// using var context = new SoftwareInstallationDatabase();
// var order = context.Orders.Include(x => x.Package).FirstOrDefault(x => x.Id == model.Id);
// if (order == null)
// {
// return null;
// }
// order.Update(model);
// context.SaveChanges();
// return order.GetViewModel;
// }
//}
2024-03-26 15:23:03 +04:00
public class OrderStorage : IOrderStorage
{
2024-03-27 10:21:01 +04:00
public List<OrderViewModel> GetFullList()
2024-03-26 15:23:03 +04:00
{
using var context = new SoftwareInstallationDatabase();
2024-03-27 10:21:01 +04:00
return context.Orders
.Include(x => x.Package)
.Select(x => x.GetViewModel)
.ToList();
2024-03-26 15:23:03 +04:00
}
2024-03-27 10:21:01 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
2024-03-26 15:23:03 +04:00
{
if (!model.Id.HasValue)
{
2024-03-27 10:21:01 +04:00
return new();
2024-03-26 15:23:03 +04:00
}
2024-03-27 10:21:01 +04:00
using var context = new SoftwareInstallationDatabase();
2024-03-26 15:23:03 +04:00
return context.Orders
2024-03-27 10:21:01 +04:00
.Include(x => x.Package)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
2024-03-26 15:23:03 +04:00
}
2024-03-27 10:21:01 +04:00
public OrderViewModel? GetElement(OrderSearchModel model)
2024-03-26 15:23:03 +04:00
{
2024-03-27 10:21:01 +04:00
if (!model.Id.HasValue)
{
return null;
}
2024-03-26 15:23:03 +04:00
using var context = new SoftwareInstallationDatabase();
return context.Orders
2024-03-27 10:21:01 +04:00
.Include(x => x.Package)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
2024-03-26 15:23:03 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new SoftwareInstallationDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
2024-03-27 10:21:01 +04:00
return context.Orders
.Include(x => x.Package)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
2024-03-26 15:23:03 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
2024-03-27 10:21:01 +04:00
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
2024-03-26 15:23:03 +04:00
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
2024-03-27 10:21:01 +04:00
return context.Orders
.Include(x => x.Package)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.Package)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
2024-03-26 15:23:03 +04:00
}
}
}