157 lines
5.4 KiB
C#
157 lines
5.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
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)
|
|
{
|
|
using var context = new Database();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
else if (model.StudentCard.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.FirstOrDefault(record => record.StudentCard.Equals(model.StudentCard))
|
|
?.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
{
|
|
using var context = new Database();
|
|
if (model.UserId.HasValue && model.PageNumber.HasValue && model.PageSize.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.Where(x => x.UserId == model.UserId)
|
|
.Skip(model.PageSize.Value * (model.PageNumber.Value - 1))
|
|
.Take(model.PageSize.Value)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.Id.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue && model.EducationStatusId.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.Where(record => record.UserId == model.UserId && record.EducationStatusId != model.EducationStatusId)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.Where(record => record.UserId == model.UserId)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else
|
|
{
|
|
return new();
|
|
}
|
|
}
|
|
public List<StudentViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.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(record => record.User)
|
|
.Include(record => record.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(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
if (student == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Students.Remove(student);
|
|
context.SaveChanges();
|
|
return student.GetViewModel;
|
|
}
|
|
|
|
public int GetNumberOfPages(int userId, int pageSize)
|
|
{
|
|
using var context = new Database();
|
|
int carsCount = context.Students.Where(c => c.UserId == userId).Count();
|
|
int numberOfpages = (int)Math.Ceiling((double)carsCount / pageSize);
|
|
return numberOfpages != 0 ? numberOfpages : 1;
|
|
}
|
|
}
|
|
}
|