PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairDatabaseImplement/Implements/OrderStorage.cs

182 lines
5.9 KiB
C#
Raw Normal View History

2024-03-23 21:01:40 +04:00
using Microsoft.EntityFrameworkCore;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDatabaseImplement.Models;
namespace PlumbingRepairDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new PlumbingRepairDatabase();
2024-05-03 12:37:02 +04:00
var element = context.Orders.Include(x => x.Work).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(rec => rec.Id == model.Id);
2024-03-23 21:01:40 +04:00
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
2024-04-12 19:30:31 +04:00
return AttachNames(element.GetViewModel);
2024-03-23 21:01:40 +04:00
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
2024-05-11 19:41:48 +04:00
if (!model.Id.HasValue)
{
2024-05-03 12:37:02 +04:00
return null;
2024-05-11 19:41:48 +04:00
}
2024-05-03 12:37:02 +04:00
using var context = new PlumbingRepairDatabase();
2024-05-11 19:41:48 +04:00
if (model.Id.HasValue)
return AttachNames(context.Orders
.Include(x => x.Work)
.Include(x => x.Implementer)
.Include(x => x.Client)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel);
else if (model.ImplementerId.HasValue && model.Status.HasValue)
return AttachNames(context.Orders
.Include(x => x.Implementer)
.Include(x => x.Work)
.Include(x => x.Client)
.FirstOrDefault(x => model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId)
?.GetViewModel);
return null;
2024-05-03 12:37:02 +04:00
}
2024-03-23 21:01:40 +04:00
2024-05-03 12:37:02 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
2024-03-23 21:01:40 +04:00
{
using var context = new PlumbingRepairDatabase();
2024-04-12 19:30:31 +04:00
2024-05-11 19:41:48 +04:00
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.ClientId.HasValue && !model.Status.HasValue)
{
return new();
}
if (model.Id.HasValue)
{
return context.Orders
.Include(x => x.Work)
.Include(x => x.Implementer)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Select(x => AttachNames(x.GetViewModel)).ToList();
}
if (model.Status.HasValue)
{
return context.Orders
.Include(x => x.Work)
.Include(x => x.Implementer)
.Include(x => x.Client)
.Where(x => x.ImplementerId == model.ImplementerId && x.Status == model.Status)
.Select(x => AttachNames(x.GetViewModel)).ToList();
}
if (model.ClientId.HasValue)
{
return context.Orders
.Include(x => x.Client)
.Include(x => x.Work)
.Include(x => x.Implementer)
.Where(x => x.ClientId == model.ClientId)
.Select(x => AttachNames(x.GetViewModel)).ToList();
}
if (model.ImplementerId.HasValue)
{
return context.Orders
.Include(x => x.Work)
.Include(x => x.Implementer)
.Include(x => x.Client)
.Where(x => x.ImplementerId == model.ImplementerId)
.Select(x => AttachNames(x.GetViewModel)).ToList();
}
if (model.DateFrom != null && model.DateTo != null)
2024-05-03 12:37:02 +04:00
{
return context.Orders
.Include(x => x.Work)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Where(x => (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
&& (!model.ClientId.HasValue || x.ClientId == model.ClientId))
.Select(x => x.GetViewModel)
.ToList();
2024-04-12 19:30:31 +04:00
2024-05-11 19:41:48 +04:00
}
return new();
}
2024-03-23 21:01:40 +04:00
public List<OrderViewModel> GetFullList()
{
using var context = new PlumbingRepairDatabase();
return context.Orders
.Include(x => x.Work)
2024-04-19 18:02:08 +04:00
.Include(x => x.Implementer)
2024-05-03 12:37:02 +04:00
.Include(x => x.Client)
2024-04-12 19:30:31 +04:00
.Select(x =>AttachNames(x.GetViewModel))
2024-03-23 21:01:40 +04:00
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new PlumbingRepairDatabase();
2024-04-12 19:30:31 +04:00
2024-03-23 21:01:40 +04:00
context.Orders.Add(newOrder);
context.SaveChanges();
2024-05-11 19:41:48 +04:00
return AttachNames(context.Orders.Include(x => x.Work).Include(x => x.Implementer).Include(x => x.Client).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel);
2024-03-23 21:01:40 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new PlumbingRepairDatabase();
2024-05-03 12:37:02 +04:00
var order = context.Orders.Include(x => x.Work).Include(x => x.Implementer).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
2024-03-23 21:01:40 +04:00
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
2024-04-12 19:30:31 +04:00
return AttachNames(order.GetViewModel);
}
public static OrderViewModel? AttachNames(OrderViewModel? model)
{
if (model == null)
{
return null;
}
using var context = new PlumbingRepairDatabase();
foreach (var work in context.Works)
{
if(work.Id == model.WorkId)
{
model.WorkName = work.WorkName;
break;
}
}
foreach ( var client in context.Clients)
{
if (client.Id == model.ClientId)
{
model.ClientFIO = client.ClientFIO;
2024-05-03 12:37:02 +04:00
model.ClientEmail = client.Email;
2024-04-12 19:30:31 +04:00
return model;
}
}
2024-04-19 18:02:08 +04:00
foreach (var implementer in context.Implementers)
{
if (implementer.Id == model.ImplementerId)
{
model.ImplementerFIO = implementer.ImplementerFIO;
return model;
}
}
2024-04-12 19:30:31 +04:00
return model;
2024-03-23 21:01:40 +04:00
}
}
}