103 lines
3.3 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;
2024-04-29 13:44:53 +04:00
namespace SoftwareInstallationDatabaseImplement.Implements
2024-03-26 15:23:03 +04:00
{
public class OrderStorage : IOrderStorage
{
2024-04-29 13:44:53 +04:00
public OrderViewModel? Delete(OrderBindingModel model)
2024-03-26 15:23:03 +04:00
{
using var context = new SoftwareInstallationDatabase();
2024-04-29 13:44:53 +04:00
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
2024-04-21 17:55:38 +04:00
{
2024-04-29 13:44:53 +04:00
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
2024-04-21 17:55:38 +04:00
}
2024-04-29 13:44:53 +04:00
return null;
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-04-29 13:44:53 +04:00
using var context = new SoftwareInstallationDatabase();
2024-05-05 16:02:57 +04:00
return context.Orders.Include(x => x.Package)
.Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2024-04-29 13:44:53 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new SoftwareInstallationDatabase();
2024-05-05 16:02:57 +04:00
return context.Orders.Include(x => x.Package).Include(x => x.Implementer)
.Include(x => x.Client).Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
2024-04-29 13:44:53 +04:00
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) && (!model.DateTo.HasValue || x.DateCreate <= model.DateTo)
2024-05-05 16:02:57 +04:00
&&
(model.Statuses == null || model.Statuses != null && model.Statuses.Contains(x.Status)) &&
(!model.ImplementerId.HasValue || x.ImplementerId == model.ImplementerId)
2024-04-29 13:44:53 +04:00
&& (!model.ClientId.HasValue || x.ClientId == model.ClientId)))
.Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
2024-03-26 15:23:03 +04:00
using var context = new SoftwareInstallationDatabase();
2024-04-29 13:44:53 +04:00
2024-05-05 16:02:57 +04:00
return context.Orders.Include(x => x.Client).Include(x => x.Package).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
2024-03-26 15:23:03 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
2024-04-29 13:44:53 +04:00
2024-03-26 15:23:03 +04:00
if (newOrder == null)
{
return null;
}
2024-04-29 13:44:53 +04:00
2024-03-26 15:23:03 +04:00
using var context = new SoftwareInstallationDatabase();
2024-04-29 13:44:53 +04:00
2024-03-26 15:23:03 +04:00
context.Orders.Add(newOrder);
context.SaveChanges();
2024-04-29 13:44:53 +04:00
return newOrder.GetViewModel;
2024-03-26 15:23:03 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new SoftwareInstallationDatabase();
2024-04-29 13:44:53 +04:00
2024-03-27 10:21:01 +04:00
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
2024-04-29 13:44:53 +04:00
2024-03-26 15:23:03 +04:00
if (order == null)
{
return null;
}
2024-04-29 13:44:53 +04:00
2024-03-26 15:23:03 +04:00
order.Update(model);
context.SaveChanges();
2024-03-27 10:21:01 +04:00
2024-04-29 13:44:53 +04:00
return order.GetViewModel;
2024-03-26 15:23:03 +04:00
}
}
}