77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using LawFirmContracts.BindingModels;
|
|||
|
using LawFirmContracts.SearchModels;
|
|||
|
using LawFirmContracts.StorageContracts;
|
|||
|
using LawFirmContracts.ViewModels;
|
|||
|
using LawFirmDatabase.Models;
|
|||
|
|
|||
|
namespace LawFirmDatabase.Implements
|
|||
|
{
|
|||
|
public class CaseStorage : ICaseStorage
|
|||
|
{
|
|||
|
public List<CaseViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
return context.Cases.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
public List<CaseViewModel> GetFilteredList(CaseSearchModel model)
|
|||
|
{
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
return context.Cases.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
public CaseViewModel? GetElement(CaseSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
return context.Cases.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
public CaseViewModel? Insert(CaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
var newCase = Case.Create(context, model);
|
|||
|
if (newCase != null)
|
|||
|
{
|
|||
|
context.Cases.Add(newCase);
|
|||
|
context.SaveChanges();
|
|||
|
return newCase.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
public CaseViewModel? Update(CaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
var casee = context.Cases.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (casee == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
casee.Update(context, model);
|
|||
|
context.SaveChanges();
|
|||
|
return casee.GetViewModel;
|
|||
|
}
|
|||
|
public CaseViewModel? Delete(CaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new LawFirmDBContext();
|
|||
|
var casee = context.Cases.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (casee == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Cases.Remove(casee);
|
|||
|
context.SaveChanges();
|
|||
|
return casee.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|