2023-11-09 04:07:06 +04:00
|
|
|
|
using UniversityContracts.BindingModels;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
using UniversityContracts.StoragesContracts;
|
|
|
|
|
using UniversityContracts.ViewModels;
|
2023-11-09 04:07:06 +04:00
|
|
|
|
using UniversityDatabaseImplement;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
using UniversityDatabaseImplement.Models;
|
|
|
|
|
|
2023-11-09 04:07:06 +04:00
|
|
|
|
namespace UniversityUniversityDatabaseImplement.Implements
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
public class StudentStorage : IStudentStorage
|
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
public void Delete(StudentBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var student = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (student != null)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
context.Students.Remove(student);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Студент не найден");
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public StudentViewModel GetElement(StudentBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
if (model == null)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new UniversityDatabase();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var student = context.Students
|
|
|
|
|
.ToList()
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
return student != null ? CreateModel(student) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<StudentViewModel> GetFilteredList(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new UniversityDatabase();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
return context.Students
|
2023-11-09 04:07:06 +04:00
|
|
|
|
.Where(student => student.FIO.Contains(model.FIO))
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(CreateModel)
|
|
|
|
|
.ToList();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public List<StudentViewModel> GetFullList()
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new UniversityDatabase();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
return context.Students
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(CreateModel)
|
|
|
|
|
.ToList();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public void Insert(StudentBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
context.Students.Add(CreateModel(model, new Student()));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
|
|
|
|
|
public void Update(StudentBindingModel model)
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var context = new UniversityDatabase();
|
|
|
|
|
var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-10-28 09:06:35 +04:00
|
|
|
|
{
|
2023-11-09 04:07:06 +04:00
|
|
|
|
var student = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (student == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Студент не найден");
|
|
|
|
|
}
|
|
|
|
|
CreateModel(model, student);
|
2023-10-28 09:06:35 +04:00
|
|
|
|
context.SaveChanges();
|
2023-11-09 04:07:06 +04:00
|
|
|
|
transaction.Commit();
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
2023-11-09 04:07:06 +04:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Student CreateModel(StudentBindingModel model, Student student)
|
|
|
|
|
{
|
|
|
|
|
student.FIO = model.FIO;
|
|
|
|
|
student.PhotoFilePath = model.PhotoFilePath;
|
|
|
|
|
student.Email = model.Email;
|
|
|
|
|
student.DirectionName = model.DirectionName;
|
|
|
|
|
|
|
|
|
|
return student;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private StudentViewModel CreateModel(Student student)
|
|
|
|
|
{
|
|
|
|
|
return new StudentViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = student.Id,
|
|
|
|
|
FIO = student.FIO,
|
|
|
|
|
PhotoFilePath = student.PhotoFilePath,
|
|
|
|
|
Email = student.Email,
|
|
|
|
|
DirectionName = student.DirectionName,
|
|
|
|
|
};
|
2023-10-28 09:06:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|