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

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

View File

@ -20,8 +20,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string Description { get; set; } = string.Empty;
public int Id { get; set; }
[ForeignKey("InterestId")]
public virtual List<StudentInterest> StudentInterests { get; set; } = new();
[Required]
public int StudentId { get; set; }
public virtual Student Student { get; set; }
[ForeignKey("InterestId")]
public virtual List<DiyInterest> DiyInterests { get; set; } = new();
[ForeignKey("InterestId")]
@ -37,19 +38,11 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{
Id = model.Id,
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)
{
@ -65,7 +58,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{
Id = Id,
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 virtual Product product { get; set; }
public virtual Product Product { get; set; }
[Required]
public int TeacherId { get; set; }
public string TeacherName { get; set; } = string.Empty;
public virtual Teacher Teacher { get; set; }
private Dictionary<int, IMaterial>? _LessonMaterials = null;
[NotMapped]
public Dictionary<int, IMaterial> LessonMaterials
@ -52,6 +55,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
ProductId = model.ProductId,
ProductName = model.ProductName,
TeacherId = model.TeacherId,
TeacherName=model.TeacherName,
Materials = model.LessonMaterials.Select(x => new LessonMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
@ -73,6 +77,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
ProductId = ProductId,
ProductName = ProductName,
TeacherId = TeacherId,
TeacherName=TeacherName,
LessonMaterials = LessonMaterials
};

View File

@ -17,15 +17,16 @@ namespace SchoolAgainStudyDataBaseImplements.Models
public string Title { get; set; } = string.Empty;
[Required]
public string SphereUse { get; set; } = string.Empty;
[Required]
public int TeacherId { get; set; }
public virtual Teacher Teacher { get; set; }
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)
{
@ -37,7 +38,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{
Id = model.Id,
Title = model.Title,
SphereUse = model.SphereUse
SphereUse = model.SphereUse,
TeacherId = model.TeacherId,
};
}
@ -47,7 +49,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{
Id = model.Id,
Title = model.Title,
SphereUse = model.SphereUse
SphereUse = model.SphereUse,
TeacherId=model.TeacherId
};
}
@ -65,7 +68,8 @@ namespace SchoolAgainStudyDataBaseImplements.Models
{
Id = Id,
Title = Title,
SphereUse = SphereUse
SphereUse = SphereUse,
TeacherId = TeacherId,
};
}
}

View File

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

View File

@ -26,25 +26,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models
[Required]
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; }
[ForeignKey("StudentId")]
public virtual List<StudentInterest> Interests { get; set; } = new();
public virtual List<Interest> Interests { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Diy> Diys { get; set; } = new();
[ForeignKey("StudentId")]
@ -59,10 +44,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
Email = model.Email,
Login = model.Login,
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,
Email = Email,
Login = Login,
Password = Password,
StudentInterests = StudentInterests
Password = Password
};
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]
public DateTime DateIssue { get; set; }
[Required]
public DateTime DateDelivery { get; set; }
public DateTime DateDelivery { get; set; }
[Required]
public int TeacherId { get; set; }
public string TeacherName { get; set; } = string.Empty;
public virtual Teacher Teacher { get; set; }
private Dictionary<int, IMaterial>? _TaskMaterials = null;
[NotMapped]
public Dictionary<int, IMaterial> TaskMaterials { get
@ -49,6 +51,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
DateIssue = model.DateIssue,
DateDelivery = model.DateDelivery,
TeacherId = model.TeacherId,
TeacherName = model.TeacherName,
Materials = model.TaskMaterials.Select(x => new TaskMaterial
{
Material = context.Materials.First(y => y.Id == x.Key),
@ -70,6 +73,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
DateIssue = DateIssue,
DateDelivery = DateDelivery,
TeacherId = TeacherId,
TeacherName= TeacherName,
TaskMaterials = TaskMaterials
};

View File

@ -24,23 +24,10 @@ namespace SchoolAgainStudyDataBaseImplements.Models
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();
public virtual List<Material> Materials { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<Task> Tasks { get; set; } = new();
[ForeignKey("TeacherId")]
@ -55,10 +42,7 @@ namespace SchoolAgainStudyDataBaseImplements.Models
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()
};
}
@ -79,34 +63,9 @@ namespace SchoolAgainStudyDataBaseImplements.Models
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

@ -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 SchoolAgainStudyDataBaseImplements.Models;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using Task = SchoolAgainStudyDataBaseImplements.Models.Task;
namespace SchoolAgainStudyDataBaseImplements
@ -21,11 +22,9 @@ namespace SchoolAgainStudyDataBaseImplements
}
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; }
@ -35,8 +34,6 @@ namespace SchoolAgainStudyDataBaseImplements
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; }
@ -45,4 +42,4 @@ namespace SchoolAgainStudyDataBaseImplements
public virtual DbSet<TaskMaterial> TaskMaterials { set; get; }
}
}
}