113 lines
3.2 KiB
C#
113 lines
3.2 KiB
C#
using CanteenContracts.BindingModels;
|
|
using CanteenContracts.SearchModel;
|
|
using CanteenContracts.StoragesContracts;
|
|
using CanteenContracts.View;
|
|
using CanteenDatabaseImplement.Models;
|
|
using CanteenDataModels.Models;
|
|
using FluentNHibernate.Conventions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CanteenDatabaseImplement.Implements
|
|
{
|
|
public class CookStorage : ICookStorage
|
|
{
|
|
public CookViewModel? GetElement(CookSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
return context.Cooks
|
|
.Include(x => x.Products)
|
|
.ThenInclude(x => x.Product)
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<CookViewModel> GetFilteredList(CookSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.OrderId.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new CanteenDatabase();
|
|
|
|
return context.Cooks
|
|
.Include(x => x.Manager)
|
|
.Include(x => x.Products)
|
|
.ThenInclude(x => x.Product)
|
|
.Include(x => x.Orders)
|
|
.ThenInclude(x => x.Order)
|
|
.Where(x =>
|
|
(model.Id.HasValue && x.Id == model.Id) ||
|
|
(model.ManagerId.HasValue && model.ManagerId == x.ManagerId) ||
|
|
(model.OrderId.HasValue && x.Orders.Find(x => x.CookId == model.OrderId) != null))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<CookViewModel> GetFullList()
|
|
{
|
|
using var context = new CanteenDatabase();
|
|
return context.Cooks.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public CookViewModel? Insert(CookBindingModel model)
|
|
{
|
|
var newCook = Cook.Create(model);
|
|
if (newCook == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
context.Cooks.Add(newCook);
|
|
context.SaveChanges();
|
|
|
|
return newCook.GetViewModel;
|
|
}
|
|
|
|
public CookViewModel? Update(CookBindingModel model)
|
|
{
|
|
using var context = new CanteenDatabase();
|
|
|
|
var cook = context.Cooks.FirstOrDefault(x => x.Id == model.Id);
|
|
if (cook == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
cook.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return cook.GetViewModel;
|
|
}
|
|
|
|
public CookViewModel? Delete(CookBindingModel model)
|
|
{
|
|
using var context = new CanteenDatabase();
|
|
|
|
var cook = context.Cooks.FirstOrDefault(x => x.Id == model.Id);
|
|
if (cook != null)
|
|
{
|
|
context.Cooks.Remove(cook);
|
|
context.SaveChanges();
|
|
|
|
return cook.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|