SUBD_SushiBar/SushiBar/SushiBarDatabaseImplement/Implements/PlaceStorage.cs

79 lines
2.6 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 PlaceStorage : IPlaceStorage
{
public PlaceViewModel? Delete(PlaceBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Places.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Places.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public PlaceViewModel? GetElement(PlaceSearchModel model)
{
using var context = new SushiBarDatabase();
return context.Places.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaceNumber.ToString())) && x.PlaceNumber.ToString() == model.PlaceNumber.ToString() || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<PlaceViewModel> GetFilteredList(PlaceSearchModel model)
{
if (string.IsNullOrEmpty(model.PlaceNumber.ToString()))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Places.Where(x => x.PlaceNumber.ToString().Contains(model.PlaceNumber.ToString())).Select(x => x.GetViewModel).ToList();
}
public List<PlaceViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Places.Select(x => x.GetViewModel).ToList();
}
public PlaceViewModel? Insert(PlaceBindingModel model)
{
var newPlace = Place.Create(model);
if (newPlace == null)
{
return null;
}
using var context = new SushiBarDatabase();
context.Places.Add(newPlace);
context.SaveChanges();
return newPlace.GetViewModel;
}
public PlaceViewModel? Update(PlaceBindingModel model)
{
using var context = new SushiBarDatabase();
var component = context.Places.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}