Сделанный planOfStudyTeachers

This commit is contained in:
DyCTaTOR 2024-04-24 14:08:27 +04:00
parent 51ed3d3af4
commit f93bc290e3
8 changed files with 156 additions and 3 deletions

View File

@ -14,6 +14,6 @@ namespace UniversityContracts.BindingModels
public int TeacherId { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public Dictionary<int, (IStudentModel, int)> StudentDisciplines { get; set; } = new();
public Dictionary<int, IStudentModel> StudentDisciplines { get; set; } = new();
}
}

View File

@ -13,5 +13,6 @@ namespace UniversityContracts.BindingModels
public string Profile { get; set; } = string.Empty;
public string FormOfStudy { get; set; } = string.Empty;
public int WorkerId { get; set; }
public Dictionary<int, ITeacherModel> PlanOfStudyTeachers { get; set; } = new();
}
}

View File

@ -16,5 +16,10 @@ namespace UniversityContracts.ViewModels
public string Profile { get; set; } = string.Empty;
[DisplayName("Форма обучения")]
public string FormOfStudy { get; set; } = string.Empty;
public Dictionary<int, ITeacherModel> PlanOfStudyTeachers
{
get;
set;
} = new();
}
}

View File

@ -4,6 +4,8 @@ using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
@ -17,4 +19,45 @@ namespace UniversityDatabaseImplement.Models
[Required]
public string Score { get; private set; } = string.Empty;
}
public static Student? Create(StudentBindingModel model)
{
if (model == null)
{
return null;
}
return new Student()
{
Id = model.Id,
PlanOfStudyId = model.PlanOfStudyId,
Name = model.Name,
PhoneNumber = model.PhoneNumber
};
}
public static Student Create(StudentViewModel model)
{
return new Student
{
Id = model.Id,
PlanOfStudyId = model.PlanOfStudyId,
Name = model.Name,
PhoneNumber = model.PhoneNumber
};
}
public void Update(StudentBindingModel model)
{
if (model == null)
{
return;
}
PhoneNumber = model.PhoneNumber;
Name = model.Name;
PlanOfStudyId = model.PlanOfStudyId;
}
public StudentViewModel GetViewModel => new()
{
Id = Id,
PlanOfStudyId = PlanOfStudyId,
Name = Name,
PhoneNumber = PhoneNumber
};
}

View File

@ -1,9 +1,12 @@
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;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
@ -16,5 +19,84 @@ namespace UniversityDatabaseImplement.Models
public string Profile { get; private set; } = string.Empty;
[Required]
public string FormOfStudy { get; private set; } = string.Empty;
private Dictionary<int, ITeacherModel>? _planOfStudyTeachers = null;
[NotMapped]
public Dictionary<int, ITeacherModel> PlanOfStudyTeachers
{
get
{
if (_planOfStudyTeachers == null)
{
_planOfStudyTeachers = Teachers
.ToDictionary(recPC => recPC.TeacherId, recPC => recPC.Teacher as ITeacherModel);
}
return _planOfStudyTeachers;
}
}
[ForeignKey("PlanOfStudyId")]
public virtual List<PlanOfStudyTeacher> Teachers { get; set; } = new();
public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model)
{
return new PlanOfStudy()
{
Id = model.Id,
WorkerId = model.WorkerId,
Profile = model.Profile,
FormOfStudy = model.FormOfStudy,
Teachers = model.PlanOfStudyTeachers.Select(x => new
PlanOfStudyTeacher
{
Teacher = context.Teachers.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(PlanOfStudyBindingModel model)
{
if (model == null)
{
return;
}
Id = model.Id;
WorkerId = model.WorkerId;
Profile = model.Profile;
FormOfStudy = model.FormOfStudy;
}
public void UpdateTeachers(UniversityDatabase context,
PlanOfStudyBindingModel model)
{
var planOfStudyTeachers = context.PlanOfStudyTeachers
.Where(rec => rec.PlanOfStudyId == model.Id).ToList();
if (planOfStudyTeachers != null && planOfStudyTeachers.Count > 0)
{ // удалили те, которых нет в модели
context.PlanOfStudyTeachers.RemoveRange(planOfStudyTeachers.Where(rec
=> !model.PlanOfStudyTeachers.ContainsKey(rec.TeacherId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateTeacher in planOfStudyTeachers)
{
model.PlanOfStudyTeachers.Remove(updateTeacher.TeacherId);
}
context.SaveChanges();
}
var planOfStudy = context.PlanOfStudys.First(x => x.Id == Id);
foreach (var pc in model.PlanOfStudyTeachers)
{
context.PlanOfStudyTeachers.Add(new PlanOfStudyTeacher
{
PlanOfStudy = planOfStudy,
Teacher = context.Teachers.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_planOfStudyTeachers = null;
}
public PlanOfStudyViewModel GetViewModel => new()
{
Id = Id,
WorkerId = WorkerId,
Profile = Profile,
FormOfStudy = FormOfStudy,
PlanOfStudyTeachers = PlanOfStudyTeachers,
};
}
}

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 UniversityDatabaseImplement.Models
{
public class PlanOfStudyTeacher
{
public int Id { get; set; }
[Required]
public int PlanOfStudyId { get; set; }
[Required]
public int TeacherId { get; set; }
public virtual PlanOfStudy PlanOfStudy { get; set; }
public virtual Teacher Teacher { get; set; }
}
}

View File

@ -28,6 +28,7 @@ namespace UniversityDatabaseImplement.Models
public virtual List<Statement> Statements { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<Discipline> Disciplines { get; set; } = new();
public virtual List<PlanOfStudyTeacher> PlanOfStudyTeachers { get; set; } = new();
public static Teacher? Create(TeacherBindingModel model)
{
if (model == null)

View File

@ -11,13 +11,13 @@ namespace UniversityDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
//Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR
//Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR; other-name: LAPTOP-DYCTATOR
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<PlanOfStudy> PlansOfStudy { set; get; }
public virtual DbSet<PlanOfStudy> PlanOfStudys { set; get; }
public virtual DbSet<Attestation> Attestations { set; get; }
// public virtual DbSet<Worker> Workers { set; get; }
public virtual DbSet<Storekeeper> Storekeepers { set; get; }
@ -25,5 +25,6 @@ namespace UniversityDatabaseImplement
public virtual DbSet<Discipline> Disciplines { set; get; }
public virtual DbSet<Statement> Statements { set; get; }
public virtual DbSet<StudentDiscipline> StudentDisciplines { set; get; }
public virtual DbSet<PlanOfStudyTeacher> PlanOfStudyTeachers { set; get; }
}
}