add dictionaries and refactoring

This commit is contained in:
MaxKarme 2023-04-07 17:44:00 +04:00
parent f1c38528b2
commit f281f7a009
11 changed files with 191 additions and 166 deletions

View File

@ -8,30 +8,11 @@ namespace UniversityDatabaseImplement.Implements
{
internal class ActivityStorage : IActivityStorage
{
public ActivityViewModel? Delete(ActivityBindingModel model)
public List<ActivityViewModel> 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<ActivityViewModel> GetFilteredList(ActivitySearchModel model)
{
using var context = new UniversityDatabase();
@ -43,13 +24,16 @@ namespace UniversityDatabaseImplement.Implements
.Select(x => x.GetViewModel)
.ToList();
}
public List<ActivityViewModel> 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;
}
}
}

View File

@ -8,31 +8,11 @@ namespace UniversityDatabaseImplement.Implements
{
internal class DisciplineStorage : IDisciplineStorage
{
public DisciplineViewModel? Delete(DisciplineBindingModel model)
public List<DisciplineViewModel> 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<DisciplineViewModel> GetFilteredList(DisciplineSearchModel model)
{
using var context = new UniversityDatabase();
@ -45,13 +25,17 @@ namespace UniversityDatabaseImplement.Implements
.Select(x => x.GetViewModel)
.ToList();
}
public List<DisciplineViewModel> 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;
}
}
}

View File

@ -8,30 +8,11 @@ namespace UniversityDatabaseImplement.Implements
{
internal class ExaminationResultStorage : IExaminationResultStorage
{
public ExaminationResultViewModel? Delete(ExaminationResultBindingModel model)
public List<ExaminationResultViewModel> 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<ExaminationResultViewModel> GetFilteredList(ExaminationResultSearchModel model)
{
using var context = new UniversityDatabase();
@ -43,13 +24,16 @@ namespace UniversityDatabaseImplement.Implements
.Select(x => x.GetViewModel)
.ToList();
}
public List<ExaminationResultViewModel> 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;
}
}
}

View File

@ -8,31 +8,11 @@ namespace UniversityDatabaseImplement.Implements
{
internal class ReportTypeStorage : IReportTypeStorage
{
public ReportTypeViewModel? Delete(ReportTypeBindingModel model)
public List<ReportTypeViewModel> 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<ReportTypeViewModel> GetFilteredList(ReportTypeSearchModel model)
{
using var context = new UniversityDatabase();
@ -45,13 +25,17 @@ namespace UniversityDatabaseImplement.Implements
.Select(x => x.GetViewModel)
.ToList();
}
public List<ReportTypeViewModel> 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;
}
}
}

View File

@ -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<StatementViewModel> 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<StatementViewModel> 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<StatementViewModel> 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<StatementViewModel> 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;
}
}
}

View File

@ -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<StudentViewModel> 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<StudentViewModel> 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<StudentViewModel> 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;
}
}
}

View File

@ -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<Statement> Statements { get; set; } = new();
[ForeignKey("DisciplineId")]

View File

@ -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]

View File

@ -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<ReportTypeActivity> ReportTypeActivities { get; set; } = new();
[ForeignKey("ReportTypeId")]

View File

@ -17,6 +17,20 @@ namespace UniversityDatabaseImplement.Models
public virtual List<StatementStudent> StatementStudents { get; set; } = new();
[ForeignKey("StatementId")]
public virtual List<ExaminationResult> ExaminationResults { get; set; } = new();
private Dictionary<int, IStudentModel>? _students = null;
public Dictionary<int, IStudentModel> 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
};
}
}

View File

@ -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<StatementStudent> StatementStudents { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<StudentExaminationResult> StudentExaminationResults { get; set; } = new();
private Dictionary<int, IExaminationResultModel>? _results;
[NotMapped]
public Dictionary<int, IExaminationResultModel> 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
};
}
}