155 lines
6.1 KiB
C#
155 lines
6.1 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)
|
|
{
|
|
if (!model.Id.HasValue && model.StudentCard <= 0)
|
|
{
|
|
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)
|
|
|| record.StudentCard.Equals(model.StudentCard))
|
|
?.GetViewModel;
|
|
}
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
{
|
|
using var context = new Database();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.EducationStatus)
|
|
.Include(record => record.User)
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
return context.Students
|
|
.Include(record => record.EducationStatus)
|
|
.Include(record => record.User)
|
|
.Where(record => record.UserId == model.UserId)
|
|
.Select(record => record.GetViewModel)
|
|
.ToList();
|
|
}
|
|
/*else if (model.Disciplines == true) // для отчета#1 Поставщик
|
|
{
|
|
return context.Students
|
|
.Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream })
|
|
.Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Stream = stream })
|
|
.Join(context.Disciplines, record => record.stream.Id, discipline => discipline.StreamId, (record, discipline) => new { record.student, discipline })
|
|
.GroupBy(sd => sd.student)
|
|
.Select(g => new {
|
|
Student = g.Key.Name,
|
|
Disciplines = g.Select(sd => sd.discipline.Name).ToList()
|
|
})
|
|
.ToList();
|
|
}*/
|
|
/*else if (model.DateFrom != null && model.DateTo != null) // для отчета#2 Поставщик
|
|
{
|
|
return context.Students
|
|
.Include(record => record.User)
|
|
.Include(record => record.EducationStatus)
|
|
.Join(context.StudentStreams,
|
|
student => student.Id,
|
|
studentStream => studentStream.StudentId,
|
|
(student, studentStream) => new { Student = student, StreamId = studentStream })
|
|
.Join(context.Streams,
|
|
studentStream => studentStream.StreamId,
|
|
stream => stream.Id,
|
|
(studentStream, stream) => new { Stream = stream })
|
|
.Select(result => new
|
|
{
|
|
Stream = result.Stream,
|
|
Students = result.Select(record => (record.Student, record.EducationStatus)).ToList()
|
|
})
|
|
.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;
|
|
}
|
|
}
|
|
}
|