2024-05-27 21:26:21 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
2024-04-18 20:20:50 +04:00
|
|
|
|
using System.Collections.Generic;
|
2024-04-21 19:12:38 +04:00
|
|
|
|
using System.ComponentModel;
|
2024-04-18 20:20:50 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-04-21 19:12:38 +04:00
|
|
|
|
using UniversityContracts.BindingModels;
|
|
|
|
|
using UniversityContracts.SearchModels;
|
2024-04-18 20:20:50 +04:00
|
|
|
|
using UniversityContracts.StorageContracts;
|
2024-04-21 19:12:38 +04:00
|
|
|
|
using UniversityContracts.ViewModels;
|
|
|
|
|
using UniversityDatabaseImplement.Models;
|
2024-05-01 20:40:47 +04:00
|
|
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
2024-04-18 20:20:50 +04:00
|
|
|
|
|
|
|
|
|
namespace UniversityDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class StudentStorage : IStudentStorage
|
|
|
|
|
{
|
2024-04-21 19:12:38 +04:00
|
|
|
|
public List<StudentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Students.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
|
|
|
|
|
{
|
2024-05-27 22:35:38 +04:00
|
|
|
|
if (model == null)
|
2024-04-21 19:12:38 +04:00
|
|
|
|
{
|
2024-05-28 23:13:49 +04:00
|
|
|
|
return new List<StudentViewModel>();
|
2024-04-21 19:12:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
2024-05-28 23:13:49 +04:00
|
|
|
|
// Начальный запрос без фильтрации
|
|
|
|
|
var query = context.Students
|
2024-05-27 21:26:21 +04:00
|
|
|
|
.Include(x => x.User)
|
2024-05-27 22:35:38 +04:00
|
|
|
|
.Include(x => x.PlanOfStudy)
|
2024-05-28 23:13:49 +04:00
|
|
|
|
.AsQueryable();
|
|
|
|
|
|
|
|
|
|
// Если в модели поиска указан Id, добавляем условие фильтрации
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(x => x.Id == model.Id.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Выполняем запрос и получаем результаты
|
|
|
|
|
var results = query
|
2024-05-27 21:26:21 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-05-28 23:13:49 +04:00
|
|
|
|
|
|
|
|
|
return results;
|
2024-04-21 19:12:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StudentViewModel? GetElement(StudentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Students.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name)
|
|
|
|
|
&& x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StudentViewModel? Insert(StudentBindingModel model)
|
|
|
|
|
{
|
2024-04-29 19:06:11 +04:00
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
var newStudent = Student.Create(context, model);
|
2024-04-21 19:12:38 +04:00
|
|
|
|
|
|
|
|
|
if (newStudent == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Students.Add(newStudent);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newStudent.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StudentViewModel? Update(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
var student = context.Students.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (student == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
student.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return student.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StudentViewModel? Delete(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
|
|
|
|
|
|
|
|
|
var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Students.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-18 20:20:50 +04:00
|
|
|
|
}
|
|
|
|
|
}
|