Case_accounting/CaseAccounting/CaseAccountingDataBaseImplement/Implements/SpecializationStorage.cs

125 lines
4.1 KiB
C#
Raw Normal View History

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