SUBD_SushiBar/SushiBar/SushiBarDatabaseImplement/Implements/MenuStorage.cs

79 lines
2.5 KiB
C#
Raw Normal View History

using SushiBarContracts.BindingModels;
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 MenuStorage : IMenuStorage
{
public MenuViewModel? Delete(MenuBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Menus.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Menus.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public MenuViewModel? GetElement(MenuSearchModel model)
{
using var context = new SushiBarDatabase();
return context.Menus.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FoodName)) && x.FoodName == model.FoodName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<MenuViewModel> GetFilteredList(MenuSearchModel model)
{
if (string.IsNullOrEmpty(model.FoodName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Menus.Where(x => x.FoodName.Contains(model.FoodName)).Select(x => x.GetViewModel).ToList();
}
public List<MenuViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Menus.Select(x => x.GetViewModel).ToList();
}
public MenuViewModel? Insert(MenuBindingModel model)
{
var newMenu = Menu.Create(model);
if (newMenu == null)
{
return null;
}
using var context = new SushiBarDatabase();
context.Menus.Add(newMenu);
context.SaveChanges();
return newMenu.GetViewModel;
}
public MenuViewModel? Update(MenuBindingModel model)
{
using var context = new SushiBarDatabase();
var component = context.Menus.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}