Case_accounting/CaseAccounting/CaseAccountingDataBaseImplement/Implements/HearingStorage.cs
Артём Алейкин f5a2f51591 Половина Implements DbStorage,
Добавил UserId в большинство классов SearchModel
2023-04-08 00:22:45 +04:00

143 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)
{
var newHearing = Hearing.Create(model);
if (newHearing == null)
{
return null;
}
using var context = new CaseAccountingDatabase();
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;
}
}
}