Обновление моделей слоя хранения данных

This commit is contained in:
ityurner02@mail.ru 2023-04-05 21:11:51 +04:00
parent 283d1575c4
commit fd5b88d26e
11 changed files with 57 additions and 168 deletions

View File

@ -20,12 +20,15 @@ namespace SchoolAgainStudyDataBaseImplements.Models
[Required] [Required]
public string Description { get; set; } = string.Empty; public string Description { get; set; } = string.Empty;
[Required] [Required]
public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime DateCreate { get; set; }
[Required] [Required]
public int TaskId { get; set; } public int TaskId { get; set; }
public string TaskName { get; set; } = string.Empty; public string TaskName { get; set; } = string.Empty;
public virtual Task task { get; set; } public virtual Task Task { get; set; }
[Required]
public int StudentId { get; set; } public int StudentId { get; set; }
public string StudentName { get; set; } = string.Empty;
public virtual Student Student { get; set; }
private Dictionary<int, IInterest>? _DiyInterests = null; private Dictionary<int, IInterest>? _DiyInterests = null;
[NotMapped] [NotMapped]
public Dictionary<int, IInterest> DiyInterests { public Dictionary<int, IInterest> DiyInterests {
@ -54,6 +57,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
TaskId = model.TaskId, TaskId = model.TaskId,
TaskName = model.TaskName, TaskName = model.TaskName,
StudentId = model.StudentId, StudentId = model.StudentId,
StudentName = model.StudentName,
Interests = model.DiyInterests.Select(x => new DiyInterest Interests = model.DiyInterests.Select(x => new DiyInterest
{ {
Interest = context.Interests.First(y => y.Id == x.Key), Interest = context.Interests.First(y => y.Id == x.Key),
@ -77,6 +81,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
TaskId = TaskId, TaskId = TaskId,
TaskName = TaskName, TaskName = TaskName,
StudentId = StudentId, StudentId = StudentId,
StudentName = StudentName,
DiyInterests=DiyInterests DiyInterests=DiyInterests
}; };

View File

@ -20,8 +20,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string Description { get; set; } = string.Empty; public string Description { get; set; } = string.Empty;
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("InterestId")] [Required]
public virtual List<StudentInterest> StudentInterests { get; set; } = new(); public int StudentId { get; set; }
public virtual Student Student { get; set; }
[ForeignKey("InterestId")] [ForeignKey("InterestId")]
public virtual List<DiyInterest> DiyInterests { get; set; } = new(); public virtual List<DiyInterest> DiyInterests { get; set; } = new();
[ForeignKey("InterestId")] [ForeignKey("InterestId")]
@ -37,19 +38,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{ {
Id = model.Id, Id = model.Id,
Title = model.Title, Title = model.Title,
Description = model.Description Description = model.Description,
StudentId = model.StudentId,
}; };
} }
public static Interest Create(InterestViewModel model)
{
return new Interest
{
Id = model.Id,
Title = model.Title,
Description = model.Description
};
}
public void Update(InterestBindingModel model) public void Update(InterestBindingModel model)
{ {
@ -65,7 +58,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{ {
Id = Id, Id = Id,
Title = Title, Title = Title,
Description = Description Description = Description,
StudentId = StudentId,
}; };
} }
} }

View File

@ -22,8 +22,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string ProductName { get; set; } = string.Empty; public string ProductName { get; set; } = string.Empty;
public virtual Product product { get; set; } public virtual Product Product { get; set; }
[Required]
public int TeacherId { get; set; } public int TeacherId { get; set; }
public string TeacherName { get; set; } = string.Empty;
public virtual Teacher Teacher { get; set; }
private Dictionary<int, IMaterial>? _LessonMaterials = null; private Dictionary<int, IMaterial>? _LessonMaterials = null;
[NotMapped] [NotMapped]
public Dictionary<int, IMaterial> LessonMaterials public Dictionary<int, IMaterial> LessonMaterials
@ -52,6 +55,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
ProductId = model.ProductId, ProductId = model.ProductId,
ProductName = model.ProductName, ProductName = model.ProductName,
TeacherId = model.TeacherId, TeacherId = model.TeacherId,
TeacherName=model.TeacherName,
Materials = model.LessonMaterials.Select(x => new LessonMaterial Materials = model.LessonMaterials.Select(x => new LessonMaterial
{ {
Material = context.Materials.First(y => y.Id == x.Key), Material = context.Materials.First(y => y.Id == x.Key),
@ -73,6 +77,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
ProductId = ProductId, ProductId = ProductId,
ProductName = ProductName, ProductName = ProductName,
TeacherId = TeacherId, TeacherId = TeacherId,
TeacherName=TeacherName,
LessonMaterials = LessonMaterials LessonMaterials = LessonMaterials
}; };

View File

@ -17,15 +17,16 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string Title { get; set; } = string.Empty; public string Title { get; set; } = string.Empty;
[Required] [Required]
public string SphereUse { get; set; } = string.Empty; public string SphereUse { get; set; } = string.Empty;
[Required]
public int TeacherId { get; set; }
public virtual Teacher Teacher { get; set; }
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("MaterialId")] [ForeignKey("MaterialId")]
public virtual List<LessonMaterial> LessonMaterials { get; set; } = new(); public virtual List<LessonMaterial> LessonMaterials { get; set; } = new();
[ForeignKey("MaterialId")] [ForeignKey("MaterialId")]
public virtual List<TaskMaterial> TaskMaterial { get; set; } = new(); public virtual List<TaskMaterial> TaskMaterial { get; set; } = new();
[ForeignKey("MaterialId")]
public virtual List<TeacherMaterial> TeacherMaterial { get; set; } = new();
public static Material? Create(MaterialBindingModel model) public static Material? Create(MaterialBindingModel model)
{ {
@ -37,7 +38,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{ {
Id = model.Id, Id = model.Id,
Title = model.Title, Title = model.Title,
SphereUse = model.SphereUse SphereUse = model.SphereUse,
TeacherId = model.TeacherId,
}; };
} }
@ -47,7 +49,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{ {
Id = model.Id, Id = model.Id,
Title = model.Title, Title = model.Title,
SphereUse = model.SphereUse SphereUse = model.SphereUse,
TeacherId=model.TeacherId
}; };
} }
@ -65,7 +68,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{ {
Id = Id, Id = Id,
Title = Title, Title = Title,
SphereUse = SphereUse SphereUse = SphereUse,
TeacherId = TeacherId,
}; };
} }
} }

View File

@ -18,8 +18,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models
[Required] [Required]
public string Description { get; set; } = string.Empty; public string Description { get; set; } = string.Empty;
[Required] [Required]
public DateTime DateCreate { get; set; } public DateTime DateCreate { get; set; }
[Required]
public int StudentId { get; set; } public int StudentId { get; set; }
public string StudentName { get; set; } = string.Empty;
public virtual Student Student { get; set; }
private Dictionary<int, IInterest>? _ProductInterests = null; private Dictionary<int, IInterest>? _ProductInterests = null;
[NotMapped] [NotMapped]
public Dictionary<int, IInterest> ProductInterests public Dictionary<int, IInterest> ProductInterests
@ -34,7 +37,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
return _ProductInterests; return _ProductInterests;
} }
} }
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("ProductId")] [ForeignKey("ProductId")]
public virtual List<ProductInterest> Interests { get; set; } = new(); public virtual List<ProductInterest> Interests { get; set; } = new();
@ -49,6 +52,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Description = model.Description, Description = model.Description,
DateCreate = model.DateCreate, DateCreate = model.DateCreate,
StudentId = model.StudentId, StudentId = model.StudentId,
StudentName=model.StudentName,
Interests = model.ProductInterests.Select(x => new ProductInterest Interests = model.ProductInterests.Select(x => new ProductInterest
{ {
Interest = context.Interests.First(y => y.Id == x.Key), Interest = context.Interests.First(y => y.Id == x.Key),
@ -70,6 +74,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Description = Description, Description = Description,
DateCreate = DateCreate, DateCreate = DateCreate,
StudentId = StudentId, StudentId = StudentId,
StudentName = StudentName,
ProductInterests = ProductInterests ProductInterests = ProductInterests
}; };

View File

@ -26,25 +26,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models
[Required] [Required]
public string Password { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;
private Dictionary<int, IInterest>? _StudentInterests = null;
[NotMapped]
public Dictionary<int, IInterest> StudentInterests
{
get
{
if (_StudentInterests == null)
{
_StudentInterests = Interests
.ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest));
}
return _StudentInterests;
}
}
public int Id { get; set; } public int Id { get; set; }
[ForeignKey("StudentId")] [ForeignKey("StudentId")]
public virtual List<StudentInterest> Interests { get; set; } = new(); public virtual List<Interest> Interests { get; set; } = new();
[ForeignKey("StudentId")] [ForeignKey("StudentId")]
public virtual List<Diy> Diys { get; set; } = new(); public virtual List<Diy> Diys { get; set; } = new();
[ForeignKey("StudentId")] [ForeignKey("StudentId")]
@ -59,10 +44,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Email = model.Email, Email = model.Email,
Login = model.Login, Login = model.Login,
Password = model.Password, Password = model.Password,
Interests = model.StudentInterests.Select(x => new StudentInterest
{
Interest = context.Interests.First(y => y.Id == x.Key),
}).ToList()
}; };
} }
@ -82,35 +64,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Class = Class, Class = Class,
Email = Email, Email = Email,
Login = Login, Login = Login,
Password = Password, Password = Password
StudentInterests = StudentInterests
}; };
public void UpdateInterests(SchoolDataBase context, StudentBindingModel model)
{
var studentInterests = context.StudentInterests.Where(rec => rec.StudentId == model.Id).ToList();
if (studentInterests != null && studentInterests.Count > 0)
{
context.StudentInterests.RemoveRange(studentInterests.Where(rec => !model.StudentInterests.ContainsKey(rec.InterestId)));
context.SaveChanges();
foreach (var updateInterest in studentInterests)
{
model.StudentInterests.Remove(updateInterest.InterestId);
}
context.SaveChanges();
}
var student = context.Students.First(x => x.Id == Id);
foreach (var pc in model.StudentInterests)
{
context.StudentInterests.Add(new StudentInterest
{
Student = student,
Interest = context.Interests.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_StudentInterests = null;
}
} }
} }

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class StudentInterest
{
public int Id { get; set; }
[Required]
public int StudentId { get; set; }
[Required]
public int InterestId { get; set; }
public virtual Student Student { get; set; } = new();
public virtual Interest Interest { get; set; } = new();
}
}

View File

@ -18,9 +18,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models
[Required] [Required]
public DateTime DateIssue { get; set; } public DateTime DateIssue { get; set; }
[Required] [Required]
public DateTime DateDelivery { get; set; } public DateTime DateDelivery { get; set; }
[Required]
public int TeacherId { get; set; } public int TeacherId { get; set; }
public string TeacherName { get; set; } = string.Empty;
public virtual Teacher Teacher { get; set; }
private Dictionary<int, IMaterial>? _TaskMaterials = null; private Dictionary<int, IMaterial>? _TaskMaterials = null;
[NotMapped] [NotMapped]
public Dictionary<int, IMaterial> TaskMaterials { get public Dictionary<int, IMaterial> TaskMaterials { get
@ -49,6 +51,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
DateIssue = model.DateIssue, DateIssue = model.DateIssue,
DateDelivery = model.DateDelivery, DateDelivery = model.DateDelivery,
TeacherId = model.TeacherId, TeacherId = model.TeacherId,
TeacherName = model.TeacherName,
Materials = model.TaskMaterials.Select(x => new TaskMaterial Materials = model.TaskMaterials.Select(x => new TaskMaterial
{ {
Material = context.Materials.First(y => y.Id == x.Key), Material = context.Materials.First(y => y.Id == x.Key),
@ -70,6 +73,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
DateIssue = DateIssue, DateIssue = DateIssue,
DateDelivery = DateDelivery, DateDelivery = DateDelivery,
TeacherId = TeacherId, TeacherId = TeacherId,
TeacherName= TeacherName,
TaskMaterials = TaskMaterials TaskMaterials = TaskMaterials
}; };

View File

@ -24,23 +24,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string Login { get; set; } = string.Empty; public string Login { get; set; } = string.Empty;
[Required] [Required]
public string Password { get; set; } = string.Empty; 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; } public int Id { get; set; }
[ForeignKey("TeacherId")] [ForeignKey("TeacherId")]
public virtual List<TeacherMaterial> Materials { get; set; } = new(); public virtual List<Material> Materials { get; set; } = new();
[ForeignKey("TeacherId")] [ForeignKey("TeacherId")]
public virtual List<Task> Tasks { get; set; } = new(); public virtual List<Task> Tasks { get; set; } = new();
[ForeignKey("TeacherId")] [ForeignKey("TeacherId")]
@ -55,10 +42,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Phone = model.Phone, Phone = model.Phone,
Login = model.Login, Login = model.Login,
Password = model.Password, Password = model.Password,
Materials = model.TeacherMaterials.Select(x => new TeacherMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
}).ToList()
}; };
} }
@ -79,34 +63,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Phone = Phone, Phone = Phone,
Login = Login, Login = Login,
Password = Password, 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

@ -1,20 +0,0 @@
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

@ -1,12 +1,13 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using SchoolAgainStudyDataBaseImplements.Models; using SchoolAgainStudyDataBaseImplements.Models;
using SchoolAgainStudyDataModels.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.ConstrainedExecution; using System.Runtime.ConstrainedExecution;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Task = SchoolAgainStudyDataBaseImplements.Models.Task; using Task = SchoolAgainStudyDataBaseImplements.Models.Task;
namespace SchoolAgainStudyDataBaseImplements namespace SchoolAgainStudyDataBaseImplements
@ -21,11 +22,9 @@ namespace SchoolAgainStudyDataBaseImplements
} }
base.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder);
} }
public virtual DbSet<Student> Students { set; get; } public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<StudentInterest> StudentInterests { set; get; }
public virtual DbSet<Interest> Interests { set; get; } public virtual DbSet<Interest> Interests { set; get; }
public virtual DbSet<Diy> Diys { set; get; } public virtual DbSet<Diy> Diys { set; get; }
@ -35,8 +34,6 @@ namespace SchoolAgainStudyDataBaseImplements
public virtual DbSet<Teacher> Teachers { 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<Material> Materials { set; get; }
public virtual DbSet<Lesson> Lessons { set; get; } public virtual DbSet<Lesson> Lessons { set; get; }
@ -45,4 +42,4 @@ namespace SchoolAgainStudyDataBaseImplements
public virtual DbSet<TaskMaterial> TaskMaterials { set; get; } public virtual DbSet<TaskMaterial> TaskMaterials { set; get; }
} }
} }