Case_accounting/CaseAccounting/CaseAccountingDataBaseImplement/Implements/HearingStorage.cs
2023-05-19 02:05:22 +04:00

142 lines
4.6 KiB
C#

using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseAccountingDataBaseImplement.Implements
{
public class HearingStorage : IHearingStorage
{
public HearingViewModel? Delete(HearingBindingModel model)
{
using var context = new CaseAccountingDatabase();
var element = context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element == null)
{
return null;
}
context.Hearings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
public HearingViewModel? GetElement(HearingSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new CaseAccountingDatabase();
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<HearingViewModel> GetFilteredList(HearingSearchModel model)
{
using var context = new CaseAccountingDatabase();
if (model.Id.HasValue)
{
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Date != null)
{
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.Where(x => x.Date == model.Date)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.CaseId.HasValue)
{
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.Where(x => x.CaseId == model.CaseId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<HearingViewModel> GetFullList()
{
using var context = new CaseAccountingDatabase();
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.Select(x => x.GetViewModel).ToList();
}
public HearingViewModel? Insert(HearingBindingModel model)
{
using var context = new CaseAccountingDatabase();
var newHearing = Hearing.Create(context, model);
if (newHearing == null)
{
return null;
}
context.Hearings.Add(newHearing);
context.SaveChanges();
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == newHearing.Id)
?.GetViewModel;
}
public HearingViewModel? Update(HearingBindingModel model)
{
using var context = new CaseAccountingDatabase();
var hearing = context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (hearing == null)
{
return null;
}
hearing.Update(model);
context.SaveChanges();
return context.Hearings
.Include(x => x.Case)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}
}