SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/AdStorage.cs

105 lines
3.0 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Implements
{
2024-05-24 13:05:47 +04:00
public class AdStorage : IAdStorage
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
public AdViewModel? GetElement(AdSearchModel model)
2024-05-24 12:23:50 +04:00
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new FurnitureAssemblyDatabase();
return context.Orders.Include(x => x.Furniture).
FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public List<AdViewModel> GetFilteredList(AdSearchModel model)
2024-05-24 12:23:50 +04:00
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new FurnitureAssemblyDatabase();
return context.Orders
.Include(x => x.Furniture)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
2024-05-24 13:05:47 +04:00
public List<AdViewModel> GetFullList()
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
return context.Orders.Include(x => x.Furniture)
.Select(x => x.GetViewModel).ToList();
}
2024-05-24 13:05:47 +04:00
public AdViewModel? Insert(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
var newOrder = Ad.Create(model);
2024-05-24 12:23:50 +04:00
if (newOrder == null)
{
return null;
}
using var context = new FurnitureAssemblyDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders.Include(x => x.Furniture)
.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public AdViewModel? Update(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders.Include(x => x.Furniture).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public AdViewModel? Delete(AdBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}