79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.StoragesContracts;
|
|
using SushiBarContracts.ViewModels;
|
|
using SushiBarDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
{
|
|
public class BuyerStorage : IBuyerStorage
|
|
{
|
|
|
|
public List<BuyerViewModel> GetFilteredList(BuyerSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.BuyerName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SushiBarDatabase();
|
|
return context.Buyers.Where(x => x.BuyerName.Contains(model.BuyerName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<BuyerViewModel> GetFullList()
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
return context.Buyers.Select(x=>x.GetViewModel).ToList();
|
|
}
|
|
|
|
public BuyerViewModel? Delete(BuyerBindingModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
var element = context.Buyers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Buyers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public BuyerViewModel? GetElement(BuyerSearchModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
return context.Buyers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuyerName)) && x.BuyerName == model.BuyerName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public BuyerViewModel? Insert(BuyerBindingModel model)
|
|
{
|
|
var newBuyer = Buyer.Create(model);
|
|
if (newBuyer == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SushiBarDatabase();
|
|
context.Buyers.Add(newBuyer);
|
|
context.SaveChanges();
|
|
return newBuyer.GetViewModel;
|
|
}
|
|
|
|
public BuyerViewModel? Update(BuyerBindingModel model)
|
|
{
|
|
using var context = new SushiBarDatabase();
|
|
var component = context.Buyers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|