52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|