2023-03-28 21:24:52 +04:00
|
|
|
|
using BeautySaloonContracts.BindingModels;
|
|
|
|
|
using BeautySaloonContracts.SearchModels;
|
|
|
|
|
using BeautySaloonContracts.StoragesContracts;
|
|
|
|
|
using BeautySaloonContracts.ViewModels;
|
2023-03-29 22:56:00 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
|
|
|
|
|
namespace BeautySaloonDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
var element = context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(rec => rec.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 NewdbContext();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
return context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
if (model.ClientId.HasValue)
|
|
|
|
|
return context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (model.EmployeeId.HasValue)
|
|
|
|
|
return context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.Where(x => x.EmployeeId == model.EmployeeId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (model.Date.HasValue)
|
|
|
|
|
return context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.Where(x => x.Date == model.Date)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
return context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
2023-03-29 20:00:53 +04:00
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
model.Id = context.Orders
|
2023-03-29 22:56:00 +04:00
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client)
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Count() > 0 ? context.Orders.Max(x => x.Id) + 1 : 1;
|
|
|
|
|
var newOrder = Order.Create(context, model);
|
2023-03-28 21:24:52 +04:00
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Add(newOrder);
|
|
|
|
|
context.SaveChanges();
|
2023-03-29 22:56:00 +04:00
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client).FirstOrDefault(x => x.Id == newOrder.Id)
|
|
|
|
|
?.GetViewModel;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
2023-03-29 20:00:53 +04:00
|
|
|
|
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-29 22:56:00 +04:00
|
|
|
|
var order = context.Orders.Include(x => x.Employee)
|
|
|
|
|
.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
|
2023-03-29 20:00:53 +04:00
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
order.UpdateServices(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return order.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|