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

125 lines
4.1 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 SpecializationStorage : ISpecializationStorage
{
public SpecializationViewModel? Delete(SpecializationBindingModel model)
{
using var context = new CaseAccountingDatabase();
var element = context.Specializations
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element == null)
{
return null;
}
context.Specializations.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
public SpecializationViewModel? GetElement(SpecializationSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new CaseAccountingDatabase();
return context.Specializations
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<SpecializationViewModel> GetFilteredList(SpecializationSearchModel model)
{
using var context = new CaseAccountingDatabase();
if (model.Id.HasValue)
{
return context.Specializations
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Name != null)
{
return context.Specializations
.Include(x => x.User)
.Where(x => x.Name == model.Name)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Specializations
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<SpecializationViewModel> GetFullList()
{
using var context = new CaseAccountingDatabase();
return context.Specializations
.Include(x => x.User)
.Select(x => x.GetViewModel).ToList();
}
public SpecializationViewModel? Insert(SpecializationBindingModel model)
{
var newSpecialization = Specialization.Create(model);
if (newSpecialization == null)
{
return null;
}
using var context = new CaseAccountingDatabase();
context.Specializations.Add(newSpecialization);
context.SaveChanges();
return context.Specializations
.Include(x => x.User)
.FirstOrDefault(x => x.Id == newSpecialization.Id)
?.GetViewModel;
}
public SpecializationViewModel? Update(SpecializationBindingModel model)
{
using var context = new CaseAccountingDatabase();
var specialization = context.Specializations
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (specialization == null)
{
return null;
}
specialization.Update(model);
context.SaveChanges();
return context.Specializations
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}
}