83 lines
2.8 KiB
C#
83 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using LawFirmContracts.BindingModels;
|
|
using LawFirmContracts.Models;
|
|
using LawFirmContracts.SearchModels;
|
|
using LawFirmContracts.StorageContracts;
|
|
using LawFirmContracts.ViewModels;
|
|
using LawFirmDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LawFirmDatabase.Implements
|
|
{
|
|
public class ItemStorage : IItemStorage
|
|
{
|
|
public List<ItemViewModel> GetFullList()
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
return context.Items.Include(x => x.Payment).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public List<ItemViewModel> GetFilteredList(ItemSearchModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
return context.Items.Where(x => x.Id == model.Id).Include(x => x.Payment).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public ItemViewModel? GetElement(ItemSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new LawFirmDBContext();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Items.Include(x => x.Payment).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
if (!string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return context.Items.Include(x => x.Payment).FirstOrDefault(x => x.Name == model.Name)?.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
public ItemViewModel? Insert(ItemBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var newItem = Item.Create(context, model);
|
|
if (newItem != null)
|
|
{
|
|
context.Items.Add(newItem);
|
|
context.SaveChanges();
|
|
return newItem.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
public ItemViewModel? Update(ItemBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var item = context.Items.FirstOrDefault(x => x.Id == model.Id);
|
|
if (item == null)
|
|
{
|
|
return null;
|
|
}
|
|
item.Update(context, model);
|
|
context.SaveChanges();
|
|
return item.GetViewModel;
|
|
}
|
|
public ItemViewModel? Delete(ItemBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var item = context.Items.FirstOrDefault(x => x.Id == model.Id);
|
|
if (item == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Items.Remove(item);
|
|
context.SaveChanges();
|
|
return item.GetViewModel;
|
|
}
|
|
}
|
|
}
|