117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UniversityContracts.BindingModels;
|
|
using UniversityContracts.SearchModels;
|
|
using UniversityContracts.StoragesContracts;
|
|
using UniversityContracts.ViewModels;
|
|
using UniversityDataBaseImplemet.Models;
|
|
|
|
namespace UniversityDataBaseImplemet.Implements
|
|
{
|
|
public class EducationStatusStorage : IEducationStatusStorage
|
|
{
|
|
public EducationStatusViewModel? GetElement(EducationStatusSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
return context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.FirstOrDefault(record => record.Id == model.Id
|
|
|| record.Name.Equals(model.Name))
|
|
?.GetViewModel;
|
|
}
|
|
public List<EducationStatusViewModel> GetFilteredList(EducationStatusSearchModel model)
|
|
{
|
|
using var context = new Database();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
return context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.Where(record => record.UserId == model.UserId)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else
|
|
{
|
|
return new();
|
|
}
|
|
}
|
|
public List<EducationStatusViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public EducationStatusViewModel? Insert(EducationStatusBindingModel model)
|
|
{
|
|
var newEducationStatus = EducationStatus.Create(model);
|
|
if (newEducationStatus == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
context.EducationStatuses.Add(newEducationStatus);
|
|
context.SaveChanges();
|
|
return context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.FirstOrDefault(record => record.Id.Equals(newEducationStatus.Id))
|
|
?.GetViewModel;
|
|
}
|
|
public EducationStatusViewModel? Update(EducationStatusBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var educationStatus = context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
if (educationStatus == null)
|
|
{
|
|
return null;
|
|
}
|
|
educationStatus.Update(model);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return educationStatus.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public EducationStatusViewModel? Delete(EducationStatusBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var educationStatus = context.EducationStatuses
|
|
.Include(record => record.User)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
if (educationStatus == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.EducationStatuses.Remove(educationStatus);
|
|
context.SaveChanges();
|
|
return educationStatus.GetViewModel;
|
|
}
|
|
}
|
|
}
|