Поручитель: слой хранилища (модели)

This commit is contained in:
ityurner02@mail.ru 2023-04-03 20:19:36 +04:00
parent 4f82ccbe6e
commit 0e0a605915
11 changed files with 527 additions and 0 deletions

View File

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyDataModels"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyContracts", "SchoolAgainStudyContracts\SchoolAgainStudyContracts.csproj", "{5D678B52-4EDB-439A-BF15-E18280D39585}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SchoolAgainStudyDataBaseImplements", "SchoolAgainStudyDataBaseImplements\SchoolAgainStudyDataBaseImplements.csproj", "{7B3598B3-8AE0-4353-B967-0D9141F2798F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{5D678B52-4EDB-439A-BF15-E18280D39585}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D678B52-4EDB-439A-BF15-E18280D39585}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D678B52-4EDB-439A-BF15-E18280D39585}.Release|Any CPU.Build.0 = Release|Any CPU
{7B3598B3-8AE0-4353-B967-0D9141F2798F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B3598B3-8AE0-4353-B967-0D9141F2798F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B3598B3-8AE0-4353-B967-0D9141F2798F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B3598B3-8AE0-4353-B967-0D9141F2798F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -6,4 +6,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SchoolAgainStudyDataModels\SchoolAgainStudyDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="BindingModel\" />
<Folder Include="BusinessLogicContracts\" />
<Folder Include="SearchModel\" />
<Folder Include="StorageContracts\" />
<Folder Include="ViewModel\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,106 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Lesson : ILesson
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public DateTime DateEvent { get; set; }
[Required]
public int ProductId { get; set; }
public string ProductName { get; set; } = string.Empty;
public virtual Product product { get; set; }
public int TeacherId { get; set; }
private Dictionary<int, IMaterial>? _LessonMaterials = null;
[NotMapped]
public Dictionary<int, IMaterial> LessonMaterials
{ get
{
if (_LessonMaterials == null)
{
_LessonMaterials = Materials
.ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial));
}
return _LessonMaterials;
}
}
public int Id { get; set; }
[ForeignKey("LessonId")]
public virtual List<LessonMaterial> Materials { get; set; } = new();
public static Lesson Create(SchoolDataBase context, LessonBindingModel model)
{
return new Lesson()
{
Id = model.Id,
Title = model.Title,
DateEvent = model.DateEvent,
ProductId = model.ProductId,
ProductName = model.ProductName,
TeacherId = model.TeacherId,
Materials = model.LessonMaterials.Select(x => new LessonMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(LessonBindingModel model)
{
Title = model.Title;
DateEvent = model.DateEvent;
}
public LessonViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
DateEvent = DateEvent,
ProductId = ProductId,
ProductName = ProductName,
TeacherId = TeacherId,
LessonMaterials = LessonMaterials
};
public void UpdateMaterials(SchoolDataBase context, LessonBindingModel model)
{
var lessonMaterials = context.LessonMaterials.Where(rec => rec.LessonId == model.Id).ToList();
if (lessonMaterials != null && lessonMaterials.Count > 0)
{
context.LessonMaterials.RemoveRange(lessonMaterials.Where(rec => !model.LessonMaterials.ContainsKey(rec.MaterialId)));
context.SaveChanges();
foreach (var updateMaterial in lessonMaterials)
{
model.LessonMaterials.Remove(updateMaterial.MaterialId);
}
context.SaveChanges();
}
var lesson = context.Lessons.First(x => x.Id == Id);
foreach (var pc in model.LessonMaterials)
{
context.LessonMaterials.Add(new LessonMaterial
{
Lesson = lesson,
Material = context.Materials.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_LessonMaterials = null;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class LessonMaterial
{
public int Id { get; set; }
[Required]
public int LessonId { get; set; }
[Required]
public int MaterialId { get; set; }
public virtual Lesson Lesson { get; set; } = new();
public virtual Material Material { get; set; } = new();
}
}

View File

@ -0,0 +1,71 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Material : IMaterial
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string SphereUse { get; set; } = string.Empty;
public int Id { get; set; }
[ForeignKey("MaterialId")]
public virtual List<LessonMaterial> LessonMaterials { get; set; } = new();
[ForeignKey("MaterialId")]
public virtual List<TaskMaterial> TaskMaterial { get; set; } = new();
[ForeignKey("MaterialId")]
public virtual List<TeacherMaterial> TeacherMaterial { get; set; } = new();
public static Material? Create(MaterialBindingModel model)
{
if (model == null)
{
return null;
}
return new Material()
{
Id = model.Id,
Title = model.Title,
SphereUse = model.SphereUse
};
}
public static Material Create(MaterialViewModel model)
{
return new Material
{
Id = model.Id,
Title = model.Title,
SphereUse = model.SphereUse
};
}
public void Update(MaterialBindingModel model)
{
if (model == null)
{
return;
}
Title = model.Title;
SphereUse = model.SphereUse;
}
public MaterialViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
SphereUse = SphereUse
};
}
}

View File

@ -0,0 +1,103 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Task : ITask
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public DateTime DateIssue { get; set; }
[Required]
public DateTime DateDelivery { get; set; }
public int TeacherId { get; set; }
private Dictionary<int, IMaterial>? _TaskMaterials = null;
[NotMapped]
public Dictionary<int, IMaterial> TaskMaterials { get
{
if (_TaskMaterials == null)
{
_TaskMaterials = Materials
.ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial));
}
return _TaskMaterials;
}
}
public int Id { get; set; }
[ForeignKey("TaskId")]
public virtual List<TaskMaterial> Materials { get; set; } = new();
[ForeignKey("TaskId")]
public virtual List<Diy> Diys { get; set; } = new();
public static Task Create(SchoolDataBase context, TaskBindingModel model)
{
return new Task()
{
Id = model.Id,
Title = model.Title,
DateIssue = model.DateIssue,
DateDelivery = model.DateDelivery,
TeacherId = model.TeacherId,
Materials = model.TaskMaterials.Select(x => new TaskMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(TaskBindingModel model)
{
Title = model.Title;
DateIssue = model.DateIssue;
DateDelivery = model.DateDelivery;
}
public TaskViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
DateIssue = DateIssue,
DateDelivery = DateDelivery,
TeacherId = TeacherId,
TaskMaterials = TaskMaterials
};
public void UpdateMaterials(SchoolDataBase context, TaskBindingModel model)
{
var taskMaterials = context.TaskMaterials.Where(rec => rec.TaskId == model.Id).ToList();
if (taskMaterials != null && taskMaterials.Count > 0)
{
context.TaskMaterials.RemoveRange(taskMaterials.Where(rec => !model.TaskMaterials.ContainsKey(rec.MaterialId)));
context.SaveChanges();
foreach (var updateMaterial in taskMaterials)
{
model.TaskMaterials.Remove(updateMaterial.MaterialId);
}
context.SaveChanges();
}
var task = context.Tasks.First(x => x.Id == Id);
foreach (var pc in model.TaskMaterials)
{
context.TaskMaterials.Add(new TaskMaterial
{
Task = task,
Material = context.Materials.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_TaskMaterials = null;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class TaskMaterial
{
public int Id { get; set; }
[Required]
public int TaskId { get; set; }
[Required]
public int MaterialId { get; set; }
public virtual Task Task { get; set; } = new();
public virtual Material Material { get; set; } = new();
}
}

View File

@ -0,0 +1,112 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Teacher : ITeacher
{
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Post { get; set; } = string.Empty;
[Required]
public string Phone { get; set; } = string.Empty;
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
private Dictionary<int, IMaterial>? _TeacherMaterials = null;
[NotMapped]
public Dictionary<int, IMaterial> TeacherMaterials {
get
{
if (_TeacherMaterials == null)
{
_TeacherMaterials = Materials
.ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial));
}
return _TeacherMaterials;
}
}
public int Id { get; set; }
[ForeignKey("TeacherId")]
public virtual List<TeacherMaterial> Materials { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<Task> Tasks { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<Lesson> Lessons { get; set; } = new();
public static Teacher Create(SchoolDataBase context, TeacherBindingModel model)
{
return new Teacher()
{
Id = model.Id,
Name = model.Name,
Post = model.Post,
Phone = model.Phone,
Login = model.Login,
Password = model.Password,
Materials = model.TeacherMaterials.Select(x => new TeacherMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(TeacherBindingModel model)
{
Name = model.Name;
Post = model.Post;
Phone = model.Phone;
Login = model.Login;
Password = model.Password;
}
public TeacherViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Post = Post,
Phone = Phone,
Login = Login,
Password = Password,
TeacherMaterials = TeacherMaterials
};
public void UpdateMaterials(SchoolDataBase context, TeacherBindingModel model)
{
var teacherMaterials = context.TeacherMaterials.Where(rec => rec.TeacherId == model.Id).ToList();
if (teacherMaterials != null && teacherMaterials.Count > 0)
{
context.TeacherMaterials.RemoveRange(teacherMaterials.Where(rec => !model.TeacherMaterials.ContainsKey(rec.MaterialId)));
context.SaveChanges();
foreach (var updateMaterial in teacherMaterials)
{
model.TeacherMaterials.Remove(updateMaterial.MaterialId);
}
context.SaveChanges();
}
var teacher = context.Teachers.First(x => x.Id == Id);
foreach (var pc in model.TeacherMaterials)
{
context.TeacherMaterials.Add(new TeacherMaterial
{
Teacher = teacher,
Material = context.Materials.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_TeacherMaterials = null;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class TeacherMaterial
{
public int Id { get; set; }
[Required]
public int TeacherId { get; set; }
[Required]
public int MaterialId { get; set; }
public virtual Teacher Teacher { get; set; } = new();
public virtual Material Material { get; set; } = new();
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,48 @@
using Microsoft.EntityFrameworkCore;
using SchoolAgainStudyDataBaseImplements.Models;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using Task = SchoolAgainStudyDataBaseImplements.Models.Task;
namespace SchoolAgainStudyDataBaseImplements
{
public class SchoolDataBase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql("Server=PostgreSQL;Host=localhost;Port=5432;Database=SchoolAgainStudyDataBase;Username=postgres;Password=postgres");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<StudentInterest> StudentInterests { set; get; }
public virtual DbSet<Interest> Interests { set; get; }
public virtual DbSet<Diy> Diys { set; get; }
public virtual DbSet<DiyInterest> DiyInterests { set; get; }
public virtual DbSet<Product> Products { set; get; }
public virtual DbSet<ProductInterest> ProductInterests { set; get; }
public virtual DbSet<Teacher> Teachers { set; get; }
public virtual DbSet<TeacherMaterial> TeacherMaterials { set; get; }
public virtual DbSet<Material> Materials { set; get; }
public virtual DbSet<Lesson> Lessons { set; get; }
public virtual DbSet<LessonMaterial> LessonMaterials { set; get; }
public virtual DbSet<Task> Tasks { set; get; }
public virtual DbSet<TaskMaterial> TaskMaterials { set; get; }
}
}