2023-04-09 00:34:25 +04:00
|
|
|
|
using CanteenContracts.BindingModels;
|
|
|
|
|
using CanteenContracts.SearchModel;
|
|
|
|
|
using CanteenContracts.StoragesContracts;
|
|
|
|
|
using CanteenContracts.View;
|
|
|
|
|
using CanteenDatabaseImplement.Models;
|
|
|
|
|
using CanteenDataModels.Models;
|
2023-05-16 19:13:19 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-09 00:34:25 +04:00
|
|
|
|
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.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CookViewModel> GetFilteredList(CookSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CanteenDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Cooks
|
2023-05-16 19:13:19 +04:00
|
|
|
|
.Include(x => x.Manager)
|
|
|
|
|
.Where(x => (model.Id.HasValue && x.Id == model.Id) || (model.ManagerId.HasValue && model.ManagerId == x.ManagerId))
|
2023-04-09 00:34:25 +04:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|