86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using DinerContracts.BindingModels;
|
|
using DinerContracts.SearchModels;
|
|
using DinerContracts.StoragesContracts;
|
|
using DinerContracts.ViewModels;
|
|
using DinerDataBaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DinerDataBaseImplement.Implements
|
|
{
|
|
public class FoodStorage : IFoodStorage
|
|
{
|
|
public FoodViewModel? Delete(FoodBindingModel model)
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var element = context.Components.FirstOrDefault(x => x.ID == model.ID);
|
|
if (element != null)
|
|
{
|
|
context.Components.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public FoodViewModel? GetElement(FoodSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ComponentName) && !model.ID.HasValue) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Components
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) &&
|
|
x.ComponentName == model.ComponentName) || model.ID.HasValue &&
|
|
x.ID == model.ID)?.GetViewModel;
|
|
}
|
|
|
|
public List<FoodViewModel> GetFilteredList(FoodSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ComponentName)) {
|
|
return new();
|
|
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName))
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
}
|
|
|
|
public List<FoodViewModel> GetFullList()
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
return context.Components.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public FoodViewModel? Insert(FoodBindingModel model)
|
|
{
|
|
var newComponent = Food.Create(model);
|
|
if (newComponent == null) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
context.Components.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public FoodViewModel? Update(FoodBindingModel model)
|
|
{
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var component = context.Components.FirstOrDefault(x => x.ID == model.ID);
|
|
if (component == null) {
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|
|
|