Data storage v1

This commit is contained in:
2023-04-03 21:59:15 +03:00
parent 081e8ce3ec
commit 8041ad6179
5 changed files with 415 additions and 4 deletions

View File

@@ -0,0 +1,96 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Implements
{
public class InterestStorage : IInterestStorage
{
public List<InterestViewModel> GetFullList()
{
using var context = new SchoolDataBase();
return context.Interests
.Select(x => x.GetViewModel)
.ToList();
}
public List<InterestViewModel> GetFilteredList(InterestSearchModel model)
{
if (string.IsNullOrEmpty(model.Title))
{
return new();
}
using var context = new SchoolDataBase();
return context.Interests
.Where(x => x.Title.Contains(model.Title))
.Select(x => x.GetViewModel)
.ToList();
}
public InterestViewModel? GetElement(InterestSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDataBase();
return context.Interests
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public InterestViewModel? Insert(InterestBindingModel model)
{
var newInterest = Interest.Create(model);
if (newInterest == null)
{
return null;
}
using var context = new SchoolDataBase();
context.Interests.Add(newInterest);
context.SaveChanges();
return newInterest.GetViewModel;
}
public InterestViewModel? Update(InterestBindingModel model)
{
using var context = new SchoolDataBase();
var checkStudent = context.StudentInterests.FirstOrDefault(x => x.InterestId == model.Id);
var checkDiy = context.DiyInterests.FirstOrDefault(x => x.InterestId == model.Id);
var checkProduct = context.ProductInterests.FirstOrDefault(x => x.InterestId == model.Id);
var interest = context.Interests.FirstOrDefault(x => x.Id == model.Id);
if (interest == null || checkStudent != null || checkProduct != null || checkDiy != null)
{
return null;
}
interest.Update(model);
context.SaveChanges();
return interest.GetViewModel;
}
public InterestViewModel? Delete(InterestBindingModel model)
{
using var context = new SchoolDataBase();
var checkStudent = context.StudentInterests.FirstOrDefault(x => x.InterestId == model.Id);
var checkDiy = context.DiyInterests.FirstOrDefault(x => x.InterestId == model.Id);
var checkProduct = context.ProductInterests.FirstOrDefault(x => x.InterestId == model.Id);
var element = context.Interests.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null &&(checkStudent == null && checkDiy == null && checkProduct == null))
{
context.Interests.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@@ -0,0 +1,95 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Implements
{
public class MaterialStorage : IMaterialStorage
{
public List<MaterialViewModel> GetFullList()
{
using var context = new SchoolDataBase();
return context.Materials
.Select(x => x.GetViewModel)
.ToList();
}
public List<MaterialViewModel> GetFilteredList(MaterialSearchModel model)
{
if (string.IsNullOrEmpty(model.Title))
{
return new();
}
using var context = new SchoolDataBase();
return context.Materials
.Where(x => x.Title.Contains(model.Title))
.Select(x => x.GetViewModel)
.ToList();
}
public MaterialViewModel? GetElement(MaterialSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDataBase();
return context.Materials
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MaterialViewModel? Insert(MaterialBindingModel model)
{
var newMaterial = Material.Create(model);
if (newMaterial == null)
{
return null;
}
using var context = new SchoolDataBase();
context.Materials.Add(newMaterial);
context.SaveChanges();
return newMaterial.GetViewModel;
}
public MaterialViewModel? Update(MaterialBindingModel model)
{
using var context = new SchoolDataBase();
var checkTeacher = context.TeacherMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var checkLesson = context.LessonMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var checkTask = context.TaskMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var material = context.Materials.FirstOrDefault(x => x.Id == model.Id);
if (material == null || checkTeacher != null || checkTask != null || checkLesson != null)
{
return null;
}
material.Update(model);
context.SaveChanges();
return material.GetViewModel;
}
public MaterialViewModel? Delete(MaterialBindingModel model)
{
using var context = new SchoolDataBase();
var checkTeacher = context.TeacherMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var checkLesson = context.LessonMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var checkTask = context.TaskMaterials.FirstOrDefault(x => x.MaterialId == model.Id);
var element = context.Materials.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null && (checkTeacher == null && checkLesson == null && checkTask == null))
{
context.Materials.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@@ -0,0 +1,113 @@
using Microsoft.EntityFrameworkCore;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Implements
{
public class StudentStorage : IStudentStorage
{
public StudentViewModel? Delete(StudentBindingModel model)
{
using var context = new SchoolDataBase();
var element = context.Students
.Include(x => x.Interests)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Students.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public StudentViewModel? GetElement(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDataBase();
return context.Students
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDataBase();
return context.Students
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<StudentViewModel> GetFullList()
{
using var context = new SchoolDataBase();
return context.Students
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public StudentViewModel? Insert(StudentBindingModel model)
{
using var context = new SchoolDataBase();
var newStudent = Student.Create(context, model);
if (newStudent == null)
{
return null;
}
context.Students.Add(newStudent);
context.SaveChanges();
return newStudent.GetViewModel;
}
public StudentViewModel? Update(StudentBindingModel model)
{
using var context = new SchoolDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var student = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
if (student == null)
{
return null;
}
student.Update(model);
context.SaveChanges();
student.UpdateInterests(context, model);
transaction.Commit();
return student.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@@ -0,0 +1,111 @@
using Microsoft.EntityFrameworkCore;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Implements
{
public class TeacherStorage : ITeacherStorage
{
public TeacherViewModel? Delete(TeacherBindingModel model)
{
using var context = new SchoolDataBase();
var element = context.Teachers
.Include(x => x.Materials)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Teachers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TeacherViewModel? GetElement(TeacherSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDataBase();
return context.Teachers
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<TeacherViewModel> GetFilteredList(TeacherSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDataBase();
return context.Teachers
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<TeacherViewModel> GetFullList()
{
using var context = new SchoolDataBase();
return context.Teachers
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public TeacherViewModel? Insert(TeacherBindingModel model)
{
using var context = new SchoolDataBase();
var newTeacher = Teacher.Create(context, model);
if (newTeacher == null)
{
return null;
}
context.Teachers.Add(newTeacher);
context.SaveChanges();
return newTeacher.GetViewModel;
}
public TeacherViewModel? Update(TeacherBindingModel model)
{
using var context = new SchoolDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var teacher = context.Teachers.FirstOrDefault(rec => rec.Id == model.Id);
if (teacher == null)
{
return null;
}
teacher.Update(model);
context.SaveChanges();
teacher.UpdateMaterials(context, model);
transaction.Commit();
return teacher.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@@ -15,10 +15,6 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataModels\SchoolAgainStudyDataModels.csproj" />
<ProjectReference Include="..\SchoolAgainStudyContracts\SchoolAgainStudyContracts.csproj" />