142 lines
4.7 KiB
C#
142 lines
4.7 KiB
C#
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();
|
|
var element = context.Orders.Include(x => x.Work).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Orders.Remove(element);
|
|
context.SaveChanges();
|
|
return AttachNames(element.GetViewModel);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.ImplementerId.HasValue && !model.Status.HasValue)
|
|
return null;
|
|
|
|
using var context = new PlumbingRepairDatabase();
|
|
|
|
return context.Orders
|
|
.Include(x => x.Work)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Implementer)
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)
|
|
|| (model.ImplementerId.HasValue && model.Status.HasValue && x.ImplementerId == model.ImplementerId && x.Status == model.Status))?
|
|
.GetViewModel;
|
|
|
|
}
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
using var context = new PlumbingRepairDatabase();
|
|
|
|
if (model.DateFrom.HasValue)
|
|
{
|
|
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();
|
|
}
|
|
|
|
return context.Orders
|
|
.Include(x => x.Work)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Implementer)
|
|
.Where(x => (model.Id.HasValue && x.Id == model.Id)
|
|
|| (model.ClientId.HasValue && x.ClientId == model.ClientId)
|
|
|| (model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId)
|
|
|| (model.Status.HasValue && x.Status == model.Status))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
using var context = new PlumbingRepairDatabase();
|
|
return context.Orders
|
|
.Include(x => x.Work)
|
|
.Include(x => x.Implementer)
|
|
.Include(x => x.Client)
|
|
.Select(x =>AttachNames(x.GetViewModel))
|
|
.ToList();
|
|
}
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
{
|
|
var newOrder = Order.Create(model);
|
|
if (newOrder == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new PlumbingRepairDatabase();
|
|
|
|
context.Orders.Add(newOrder);
|
|
context.SaveChanges();
|
|
return AttachNames(newOrder.GetViewModel);
|
|
}
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
{
|
|
using var context = new PlumbingRepairDatabase();
|
|
var order = context.Orders.Include(x => x.Work).Include(x => x.Implementer).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
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;
|
|
model.ClientEmail = client.Email;
|
|
return model;
|
|
}
|
|
}
|
|
foreach (var implementer in context.Implementers)
|
|
{
|
|
if (implementer.Id == model.ImplementerId)
|
|
{
|
|
model.ImplementerFIO = implementer.ImplementerFIO;
|
|
return model;
|
|
}
|
|
}
|
|
return model;
|
|
}
|
|
}
|
|
} |