From f281f7a0095ccf067212bc7f4b05fa48b75eb60f Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Fri, 7 Apr 2023 17:44:00 +0400 Subject: [PATCH] add dictionaries and refactoring --- .../Implements/ActivityStorage.cs | 46 ++++++------- .../Implements/DisciplineStorage.cs | 49 +++++++------- .../Implements/ExaminationResultStorage.cs | 46 ++++++------- .../Implements/ReportTypeStorage.cs | 48 +++++++------- .../Implements/StatementStorage.cs | 65 ++++++++++--------- .../Implements/StudentStorage.cs | 55 +++++++++------- .../DatabaseImplement/Models/Discipline.cs | 4 +- .../Models/ExaminationResult.cs | 2 +- .../DatabaseImplement/Models/ReportType.cs | 2 +- .../DatabaseImplement/Models/Statement.cs | 18 ++++- .../DatabaseImplement/Models/Student.cs | 22 ++++++- 11 files changed, 191 insertions(+), 166 deletions(-) diff --git a/University/DatabaseImplement/Implements/ActivityStorage.cs b/University/DatabaseImplement/Implements/ActivityStorage.cs index d805983..b031bc4 100644 --- a/University/DatabaseImplement/Implements/ActivityStorage.cs +++ b/University/DatabaseImplement/Implements/ActivityStorage.cs @@ -8,30 +8,11 @@ namespace UniversityDatabaseImplement.Implements { internal class ActivityStorage : IActivityStorage { - public ActivityViewModel? Delete(ActivityBindingModel model) + public List GetFullList() { using var context = new UniversityDatabase(); - var element = context.Activities.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Activities.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return context.Activities.Select(x => x.GetViewModel).ToList(); } - - public ActivityViewModel? GetElement(ActivitySearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - using var context = new UniversityDatabase(); - return context.Activities - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFilteredList(ActivitySearchModel model) { using var context = new UniversityDatabase(); @@ -43,13 +24,16 @@ namespace UniversityDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - - public List GetFullList() + public ActivityViewModel? GetElement(ActivitySearchModel model) { + if (!model.Id.HasValue) + { + return null; + } using var context = new UniversityDatabase(); - return context.Activities.Select(x => x.GetViewModel).ToList(); + return context.Activities + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public ActivityViewModel? Insert(ActivityBindingModel model) { var newActivity = Activity.Create(model); @@ -75,5 +59,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return activity.GetViewModel; } + public ActivityViewModel? Delete(ActivityBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Activities.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Activities.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Implements/DisciplineStorage.cs b/University/DatabaseImplement/Implements/DisciplineStorage.cs index 8e3c4b5..d5a39ff 100644 --- a/University/DatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/DatabaseImplement/Implements/DisciplineStorage.cs @@ -8,31 +8,11 @@ namespace UniversityDatabaseImplement.Implements { internal class DisciplineStorage : IDisciplineStorage { - public DisciplineViewModel? Delete(DisciplineBindingModel model) + public List GetFullList() { using var context = new UniversityDatabase(); - var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.ExaminationResults.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return context.Disciplines.Select(x => x.GetViewModel).ToList(); } - - public DisciplineViewModel? GetElement(DisciplineSearchModel model) - { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) - { - return null; - } - using var context = new UniversityDatabase(); - return context.Disciplines - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFilteredList(DisciplineSearchModel model) { using var context = new UniversityDatabase(); @@ -45,13 +25,17 @@ namespace UniversityDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - - public List GetFullList() + public DisciplineViewModel? GetElement(DisciplineSearchModel model) { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } using var context = new UniversityDatabase(); - return context.Disciplines.Select(x => x.GetViewModel).ToList(); + return context.Disciplines + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public DisciplineViewModel? Insert(DisciplineBindingModel model) { var newDiscipline = Discipline.Create(model); @@ -64,7 +48,6 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return newDiscipline.GetViewModel; } - public DisciplineViewModel? Update(DisciplineBindingModel model) { using var context = new UniversityDatabase(); @@ -77,5 +60,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return discipline.GetViewModel; } + public DisciplineViewModel? Delete(DisciplineBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ExaminationResults.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Implements/ExaminationResultStorage.cs b/University/DatabaseImplement/Implements/ExaminationResultStorage.cs index 5882682..86f2495 100644 --- a/University/DatabaseImplement/Implements/ExaminationResultStorage.cs +++ b/University/DatabaseImplement/Implements/ExaminationResultStorage.cs @@ -8,30 +8,11 @@ namespace UniversityDatabaseImplement.Implements { internal class ExaminationResultStorage : IExaminationResultStorage { - public ExaminationResultViewModel? Delete(ExaminationResultBindingModel model) + public List GetFullList() { using var context = new UniversityDatabase(); - var element = context.ExaminationResults.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.ExaminationResults.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return context.ExaminationResults.Select(x => x.GetViewModel).ToList(); } - - public ExaminationResultViewModel? GetElement(ExaminationResultSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - using var context = new UniversityDatabase(); - return context.ExaminationResults - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFilteredList(ExaminationResultSearchModel model) { using var context = new UniversityDatabase(); @@ -43,13 +24,16 @@ namespace UniversityDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - - public List GetFullList() + public ExaminationResultViewModel? GetElement(ExaminationResultSearchModel model) { + if (!model.Id.HasValue) + { + return null; + } using var context = new UniversityDatabase(); - return context.ExaminationResults.Select(x => x.GetViewModel).ToList(); + return context.ExaminationResults + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public ExaminationResultViewModel? Insert(ExaminationResultBindingModel model) { var newExaminationResult = ExaminationResult.Create(model); @@ -75,5 +59,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return examinationResult.GetViewModel; } + public ExaminationResultViewModel? Delete(ExaminationResultBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.ExaminationResults.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ExaminationResults.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Implements/ReportTypeStorage.cs b/University/DatabaseImplement/Implements/ReportTypeStorage.cs index bcb57af..8f9b814 100644 --- a/University/DatabaseImplement/Implements/ReportTypeStorage.cs +++ b/University/DatabaseImplement/Implements/ReportTypeStorage.cs @@ -8,31 +8,11 @@ namespace UniversityDatabaseImplement.Implements { internal class ReportTypeStorage : IReportTypeStorage { - public ReportTypeViewModel? Delete(ReportTypeBindingModel model) + public List GetFullList() { using var context = new UniversityDatabase(); - var element = context.ReportTypes.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.ReportTypes.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return context.ReportTypes.Select(x => x.GetViewModel).ToList(); } - - public ReportTypeViewModel? GetElement(ReportTypeSearchModel model) - { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) - { - return null; - } - using var context = new UniversityDatabase(); - return context.ReportTypes - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFilteredList(ReportTypeSearchModel model) { using var context = new UniversityDatabase(); @@ -45,13 +25,17 @@ namespace UniversityDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - - public List GetFullList() + public ReportTypeViewModel? GetElement(ReportTypeSearchModel model) { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } using var context = new UniversityDatabase(); - return context.ReportTypes.Select(x => x.GetViewModel).ToList(); + return context.ReportTypes + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public ReportTypeViewModel? Insert(ReportTypeBindingModel model) { var newReportType = ReportType.Create(model); @@ -77,5 +61,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return reportType.GetViewModel; } + public ReportTypeViewModel? Delete(ReportTypeBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.ReportTypes.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ReportTypes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Implements/StatementStorage.cs b/University/DatabaseImplement/Implements/StatementStorage.cs index 72ed6da..13b5973 100644 --- a/University/DatabaseImplement/Implements/StatementStorage.cs +++ b/University/DatabaseImplement/Implements/StatementStorage.cs @@ -3,24 +3,34 @@ using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; using UniversityDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace UniversityDatabaseImplement.Implements { internal class StatementStorage : IStatementStorage { - public StatementViewModel? Delete(StatementBindingModel model) + public List GetFullList() { using var context = new UniversityDatabase(); - var element = context.Statements.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Statements.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; + return context.Statements + .Include(x => x.StatementStudents) + .ThenInclude(x => x.Student) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(StatementSearchModel model) + { + using var context = new UniversityDatabase(); + return context.Statements + .Include(x => x.StatementStudents) + .ThenInclude(x => x.Student) + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); } - public StatementViewModel? GetElement(StatementSearchModel model) { if (!model.Id.HasValue) @@ -29,27 +39,10 @@ namespace UniversityDatabaseImplement.Implements } using var context = new UniversityDatabase(); return context.Statements - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + .Include(x => x.StatementStudents) + .ThenInclude(x => x.Student) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - - public List GetFilteredList(StatementSearchModel model) - { - using var context = new UniversityDatabase(); - return context.Statements - .Where(x => ( - (!model.Id.HasValue || x.Id == model.Id) - ) - ) - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFullList() - { - using var context = new UniversityDatabase(); - return context.Statements.Select(x => x.GetViewModel).ToList(); - } - public StatementViewModel? Insert(StatementBindingModel model) { var newStatement = Statement.Create(model); @@ -75,5 +68,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return statement.GetViewModel; } + public StatementViewModel? Delete(StatementBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Statements.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Statements.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Implements/StudentStorage.cs b/University/DatabaseImplement/Implements/StudentStorage.cs index e474dfd..c22b540 100644 --- a/University/DatabaseImplement/Implements/StudentStorage.cs +++ b/University/DatabaseImplement/Implements/StudentStorage.cs @@ -3,40 +3,27 @@ using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; using UniversityDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace UniversityDatabaseImplement.Implements { internal class StudentStorage : IStudentStorage { - public StudentViewModel? Delete(StudentBindingModel model) + public List GetFullList() { - using var context = new UniversityDatabase(); - var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Students.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - - public StudentViewModel? GetElement(StudentSearchModel model) - { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) - { - return null; - } using var context = new UniversityDatabase(); return context.Students - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name== model.Name) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + .Include(x => x.StudentExaminationResults) + .ThenInclude(x => x.ExaminationResult) + .Select(x => x.GetViewModel) + .ToList(); } - public List GetFilteredList(StudentSearchModel model) { using var context = new UniversityDatabase(); return context.Students + .Include(x => x.StudentExaminationResults) + .ThenInclude(x => x.ExaminationResult) .Where(x => ( (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) @@ -45,13 +32,19 @@ namespace UniversityDatabaseImplement.Implements .Select(x => x.GetViewModel) .ToList(); } - - public List GetFullList() + public StudentViewModel? GetElement(StudentSearchModel model) { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } using var context = new UniversityDatabase(); - return context.Students.Select(x => x.GetViewModel).ToList(); + return context.Students + .Include(x => x.StudentExaminationResults) + .ThenInclude(x => x.ExaminationResult) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name== model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - public StudentViewModel? Insert(StudentBindingModel model) { var newStudent = Student.Create(model); @@ -77,5 +70,17 @@ namespace UniversityDatabaseImplement.Implements context.SaveChanges(); return student.GetViewModel; } + public StudentViewModel? Delete(StudentBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Students.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/DatabaseImplement/Models/Discipline.cs b/University/DatabaseImplement/Models/Discipline.cs index 26621db..6aa5679 100644 --- a/University/DatabaseImplement/Models/Discipline.cs +++ b/University/DatabaseImplement/Models/Discipline.cs @@ -10,9 +10,9 @@ namespace UniversityDatabaseImplement.Models { public int Id { get; set; } [Required] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [Required] - public string Department { get; set; } + public string Department { get; set; } = string.Empty; [ForeignKey("DisciplineId")] public virtual List Statements { get; set; } = new(); [ForeignKey("DisciplineId")] diff --git a/University/DatabaseImplement/Models/ExaminationResult.cs b/University/DatabaseImplement/Models/ExaminationResult.cs index fc29881..bd77bb9 100644 --- a/University/DatabaseImplement/Models/ExaminationResult.cs +++ b/University/DatabaseImplement/Models/ExaminationResult.cs @@ -10,7 +10,7 @@ namespace UniversityDatabaseImplement.Models public class ExaminationResult : IExaminationResultModel { public int Id { get; set; } - public string ExaminationForm { get; set; } + public string ExaminationForm { get; set; } = string.Empty; [Required] public MarkType Mark { get; set; } [Required] diff --git a/University/DatabaseImplement/Models/ReportType.cs b/University/DatabaseImplement/Models/ReportType.cs index acf60ba..50bb684 100644 --- a/University/DatabaseImplement/Models/ReportType.cs +++ b/University/DatabaseImplement/Models/ReportType.cs @@ -10,7 +10,7 @@ namespace UniversityDatabaseImplement.Models { public int Id { get; set; } [Required] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [ForeignKey("ReportTypeId")] public virtual List ReportTypeActivities { get; set; } = new(); [ForeignKey("ReportTypeId")] diff --git a/University/DatabaseImplement/Models/Statement.cs b/University/DatabaseImplement/Models/Statement.cs index 05655ed..629599d 100644 --- a/University/DatabaseImplement/Models/Statement.cs +++ b/University/DatabaseImplement/Models/Statement.cs @@ -17,6 +17,20 @@ namespace UniversityDatabaseImplement.Models public virtual List StatementStudents { get; set; } = new(); [ForeignKey("StatementId")] public virtual List ExaminationResults { get; set; } = new(); + private Dictionary? _students = null; + public Dictionary Students + { + get + { + if(_students == null) + { + _students = StatementStudents.ToDictionary( + x => x.Student.Id, x => x.Student as IStudentModel); + } + + return _students; + } + } public static Statement Create(StatementBindingModel model) { return new Statement @@ -39,7 +53,9 @@ namespace UniversityDatabaseImplement.Models { Id = Id, Date = Date, - HoursCount = HoursCount + HoursCount = HoursCount, + Students = Students }; + } } diff --git a/University/DatabaseImplement/Models/Student.cs b/University/DatabaseImplement/Models/Student.cs index d7bff7d..0637590 100644 --- a/University/DatabaseImplement/Models/Student.cs +++ b/University/DatabaseImplement/Models/Student.cs @@ -10,13 +10,27 @@ namespace UniversityDatabaseImplement.Models { public int Id { get; set; } [Required] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [Required] - public string RecordCardNumber { get; set; } + public string RecordCardNumber { get; set; } = string.Empty; [ForeignKey("StudentId")] public virtual List StatementStudents { get; set; } = new(); [ForeignKey("StudentId")] public virtual List StudentExaminationResults { get; set; } = new(); + + private Dictionary? _results; + [NotMapped] + public Dictionary Results { + get { + if(_results == null) + { + _results = StudentExaminationResults.ToDictionary( + x => x.ExaminationResult.Id, x => x.ExaminationResult as IExaminationResultModel); + } + + return _results; + } + } public static Student Create(StudentBindingModel model) { return new Student @@ -39,7 +53,9 @@ namespace UniversityDatabaseImplement.Models { Id = Id, Name = Name, - RecordCardNumber = RecordCardNumber + RecordCardNumber = RecordCardNumber, + Results = Results }; + } }