PIbd-32.-Stroev-V.M.-COP/WinFormsLibrary1/DatabaseImplement/Implements/CategoryStorage.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2024-11-11 22:20:20 +04:00
using Contracts.BindingModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class CategoryStorage : ICategoryStorage
{
public List<CategoryViewModel> GetFullList()
{
using var context = new Database();
return context.Categories.Select(x => x.GetViewModel).ToList();
}
public CategoryViewModel? Insert(CategoryBindingModel model)
{
var newProvider = Category.Create(model);
if (newProvider == null) return null;
using var context = new Database();
context.Categories.Add(newProvider);
context.SaveChanges();
return newProvider.GetViewModel;
}
public CategoryViewModel? Update(CategoryBindingModel model)
{
using var context = new Database();
var provider = context.Categories.FirstOrDefault(x => x.Id == model.Id);
if (provider == null) return null;
provider.Update(model);
context.SaveChanges();
return provider.GetViewModel;
}
public CategoryViewModel? Delete(CategoryBindingModel model)
{
using var context = new Database();
var element = context.Categories.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Categories.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}