2024-05-07 21:53:37 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
2024-04-21 13:27:17 +04:00
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class CookStorage : ICookStorage
|
|
|
|
|
{
|
2024-05-07 21:53:37 +04:00
|
|
|
|
public void ClearEntity()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
|
|
|
|
|
string deleteAllQuery = "DELETE FROM \"Cooks\"";
|
|
|
|
|
context.Database.ExecuteSqlRaw(deleteAllQuery);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 13:27:17 +04:00
|
|
|
|
public CookViewModel? Delete(CookBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var element = context.Cooks.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Cooks.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CookViewModel? GetElement(CookSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Cooks.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CookName)) && x.CookName == model.CookName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CookViewModel> GetFilteredList(CookSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.CookName))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Cooks.Where(x => x.CookName.Contains(model.CookName)).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CookViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
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 SushiBarDatabase();
|
|
|
|
|
context.Cooks.Add(newCook);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newCook.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CookViewModel? Update(CookBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var component = context.Cooks.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (component == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
component.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return component.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|