120 lines
3.9 KiB
C#
120 lines
3.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
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 StudentStorage : IStudentStorage
|
|
{
|
|
public StudentViewModel? GetElement(StudentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
{
|
|
using var context = new Database();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.EducationStatus)
|
|
.Include(x => x.User)
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(x => x.EducationStatus)
|
|
.Include(x => x.User)
|
|
.Where(x => x.UserId == model.UserId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else
|
|
{
|
|
return new();
|
|
}
|
|
}
|
|
public List<StudentViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.Students
|
|
.Include(x => x.User)
|
|
.Include(x => x.EducationStatus)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public StudentViewModel? Insert(StudentBindingModel model)
|
|
{
|
|
var newStudent = Student.Create(model);
|
|
if (newStudent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
context.Students.Add(newStudent);
|
|
context.SaveChanges();
|
|
return newStudent.GetViewModel;
|
|
}
|
|
public StudentViewModel? Update(StudentBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var student = context.Students
|
|
.Include(x => x.User)
|
|
.Include(x => x.EducationStatus)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
if (student == null)
|
|
{
|
|
return null;
|
|
}
|
|
student.Update(model);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return student.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public StudentViewModel? Delete(StudentBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var student = context.Students
|
|
.Include(x => x.User)
|
|
.Include(x => x.EducationStatus)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
if (student == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Students.Remove(student);
|
|
context.SaveChanges();
|
|
return student.GetViewModel;
|
|
}
|
|
}
|
|
}
|