2024-04-19 19:30:16 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public class Discipline : IDisciplineModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
2024-04-29 22:15:15 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public int UserId { get; private set; }
|
|
|
|
|
|
2024-04-19 19:30:16 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public int TeacherId { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; private set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public string Description { get; private set; } = string.Empty;
|
2024-05-01 20:40:47 +04:00
|
|
|
|
public DateOnly Date { get; private set; }
|
2024-04-29 22:15:15 +04:00
|
|
|
|
public virtual User User { get; set; } = new();
|
2024-04-19 19:30:16 +04:00
|
|
|
|
public virtual Teacher Teacher { get; set; } = new();
|
2024-04-23 23:03:32 +04:00
|
|
|
|
private Dictionary<int, IStudentModel>? _studentDisciplines = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IStudentModel> StudentDisciplines
|
2024-04-19 19:30:16 +04:00
|
|
|
|
{
|
2024-04-23 23:03:32 +04:00
|
|
|
|
get
|
2024-04-19 19:30:16 +04:00
|
|
|
|
{
|
2024-04-23 23:03:32 +04:00
|
|
|
|
if (_studentDisciplines == null)
|
|
|
|
|
{
|
|
|
|
|
_studentDisciplines = Students
|
|
|
|
|
.ToDictionary(recPC => recPC.StudentId, recPC => recPC.Student as IStudentModel);
|
|
|
|
|
}
|
|
|
|
|
return _studentDisciplines;
|
2024-04-19 19:30:16 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-23 23:03:32 +04:00
|
|
|
|
[ForeignKey("DisciplineId")]
|
|
|
|
|
public virtual List<StudentDiscipline> Students { get; set; } = new();
|
|
|
|
|
public static Discipline Create(UniversityDatabase context, DisciplineBindingModel model)
|
2024-04-19 19:30:16 +04:00
|
|
|
|
{
|
|
|
|
|
return new Discipline()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
2024-04-29 22:15:15 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
User = context.Users.First(x => x.Id == model.UserId),
|
2024-04-19 19:30:16 +04:00
|
|
|
|
TeacherId = model.TeacherId,
|
2024-04-29 22:15:15 +04:00
|
|
|
|
Teacher = context.Teachers.First(x => x.Id == model.TeacherId),
|
2024-04-19 19:30:16 +04:00
|
|
|
|
Name = model.Name,
|
|
|
|
|
Description = model.Description,
|
2024-04-23 23:03:32 +04:00
|
|
|
|
Students = model.StudentDisciplines.Select(x => new
|
2024-04-29 15:37:19 +04:00
|
|
|
|
StudentDiscipline
|
2024-04-23 23:03:32 +04:00
|
|
|
|
{
|
|
|
|
|
Student = context.Students.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
2024-04-19 19:30:16 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(DisciplineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Id = model.Id;
|
2024-04-29 22:15:15 +04:00
|
|
|
|
UserId = model.UserId;
|
2024-04-19 19:30:16 +04:00
|
|
|
|
TeacherId = model.TeacherId;
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Description = model.Description;
|
|
|
|
|
}
|
2024-04-23 23:03:32 +04:00
|
|
|
|
public void UpdateStudents(UniversityDatabase context,
|
|
|
|
|
DisciplineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var studentDisciplines = context.StudentDisciplines.Where(rec => rec.DisciplineId == model.Id).ToList();
|
|
|
|
|
if (studentDisciplines != null && studentDisciplines.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.StudentDisciplines.RemoveRange(studentDisciplines.Where(rec
|
|
|
|
|
=> !model.StudentDisciplines.ContainsKey(rec.StudentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateStudent in studentDisciplines)
|
|
|
|
|
{
|
|
|
|
|
model.StudentDisciplines.Remove(updateStudent.StudentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var discipline = context.Disciplines.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.StudentDisciplines)
|
|
|
|
|
{
|
|
|
|
|
context.StudentDisciplines.Add(new StudentDiscipline
|
|
|
|
|
{
|
|
|
|
|
Discipline = discipline,
|
|
|
|
|
Student = context.Students.First(x => x.Id == pc.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_studentDisciplines = null;
|
|
|
|
|
}
|
2024-04-19 19:30:16 +04:00
|
|
|
|
public DisciplineViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2024-04-29 22:15:15 +04:00
|
|
|
|
UserId = UserId,
|
2024-04-19 19:30:16 +04:00
|
|
|
|
TeacherId = TeacherId,
|
2024-05-27 23:53:45 +04:00
|
|
|
|
TeacherName = Teacher.Name,
|
2024-04-19 19:30:16 +04:00
|
|
|
|
Name = Name,
|
|
|
|
|
Description = Description,
|
2024-04-23 23:03:32 +04:00
|
|
|
|
StudentDisciplines = StudentDisciplines,
|
2024-04-19 19:30:16 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|