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 PlaceStorage : IPlaceStorage
|
|
|
|
|
{
|
2024-05-07 21:53:37 +04:00
|
|
|
|
public void ClearEntity()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
|
|
|
|
|
string deleteAllQuery = "DELETE FROM \"Places\"";
|
|
|
|
|
context.Database.ExecuteSqlRaw(deleteAllQuery);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 13:27:17 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|