From 98b2139237da29a2db0001ffae53692135706dbf Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Sun, 9 Apr 2023 23:34:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResultOfControlBindingModel.cs | 2 +- .../BindingModels/StatementBindingModel.cs | 2 +- .../BindingModels/TeacherBindingModel.cs | 2 +- .../ResultOfControlSearchModel.cs | 3 +- .../SearchModels/StatementSearchModel.cs | 3 +- .../ViewModels/ResultOfControlViewModel.cs | 2 +- .../ViewModels/StatementViewModel.cs | 2 +- .../ViewModels/TeacherViewModel.cs | 2 +- .../Models/IResultOfControl.cs | 2 +- .../Models/IStatement.cs | 2 +- .../Models/ITeacher.cs | 2 +- .../Discipline.cs | 23 ---- .../ElectronicJournalContext.cs | 1 + .../Group.cs | 29 ----- .../Implements/DisciplineStorage.cs | 84 +++++++++++++ .../Implements/GroupStorage.cs | 84 +++++++++++++ .../Implements/ResultOfControlStorage.cs | 105 +++++++++++++++++ .../Implements/StatementStorage.cs | 111 ++++++++++++++++++ .../Implements/StudentStorage.cs | 100 ++++++++++++++++ .../Implements/TeacherStorage.cs | 84 +++++++++++++ .../Models/Discipline.cs | 58 +++++++++ .../Models/Group.cs | 69 +++++++++++ .../{ => Models}/ResultOfControl.cs | 60 +++++++++- .../Models/Statement.cs | 94 +++++++++++++++ .../Models/Student.cs | 70 +++++++++++ .../Models/Teacher.cs | 67 +++++++++++ .../Statement.cs | 43 ------- .../Student.cs | 29 ----- .../Teacher.cs | 27 ----- 29 files changed, 997 insertions(+), 165 deletions(-) delete mode 100644 SUBD/ElectronicJournalDatabaseImplement/Discipline.cs delete mode 100644 SUBD/ElectronicJournalDatabaseImplement/Group.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/DisciplineStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/GroupStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/ResultOfControlStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/StatementStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/StudentStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Implements/TeacherStorage.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Models/Discipline.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Models/Group.cs rename SUBD/ElectronicJournalDatabaseImplement/{ => Models}/ResultOfControl.cs (50%) create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Models/Statement.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Models/Student.cs create mode 100644 SUBD/ElectronicJournalDatabaseImplement/Models/Teacher.cs delete mode 100644 SUBD/ElectronicJournalDatabaseImplement/Statement.cs delete mode 100644 SUBD/ElectronicJournalDatabaseImplement/Student.cs delete mode 100644 SUBD/ElectronicJournalDatabaseImplement/Teacher.cs diff --git a/SUBD/ElectronicJournalContracts/BindingModels/ResultOfControlBindingModel.cs b/SUBD/ElectronicJournalContracts/BindingModels/ResultOfControlBindingModel.cs index af2bd16..32befc5 100644 --- a/SUBD/ElectronicJournalContracts/BindingModels/ResultOfControlBindingModel.cs +++ b/SUBD/ElectronicJournalContracts/BindingModels/ResultOfControlBindingModel.cs @@ -6,7 +6,7 @@ namespace ElectronicJournalContracts.BindingModels { public string Form { get; set; } = string.Empty; - public DateTime Date { get; set; } + public DateOnly Date { get; set; } public string Mark { get; set; } = string.Empty; diff --git a/SUBD/ElectronicJournalContracts/BindingModels/StatementBindingModel.cs b/SUBD/ElectronicJournalContracts/BindingModels/StatementBindingModel.cs index 4bb9549..6a74981 100644 --- a/SUBD/ElectronicJournalContracts/BindingModels/StatementBindingModel.cs +++ b/SUBD/ElectronicJournalContracts/BindingModels/StatementBindingModel.cs @@ -4,7 +4,7 @@ namespace ElectronicJournalContracts.BindingModels { public class StatementBindingModel : IStatement { - public DateTime Date { get; set; } + public DateOnly Date { get; set; } public int DisciplineId { get; set; } diff --git a/SUBD/ElectronicJournalContracts/BindingModels/TeacherBindingModel.cs b/SUBD/ElectronicJournalContracts/BindingModels/TeacherBindingModel.cs index 08d616a..e49c7c3 100644 --- a/SUBD/ElectronicJournalContracts/BindingModels/TeacherBindingModel.cs +++ b/SUBD/ElectronicJournalContracts/BindingModels/TeacherBindingModel.cs @@ -6,7 +6,7 @@ namespace ElectronicJournalContracts.BindingModels { public string Name { get; set; } = string.Empty; - public string Academic_title { get; set; } = string.Empty; + public string AcademicTitle { get; set; } = string.Empty; public int Id { get; set; } } diff --git a/SUBD/ElectronicJournalContracts/SearchModels/ResultOfControlSearchModel.cs b/SUBD/ElectronicJournalContracts/SearchModels/ResultOfControlSearchModel.cs index 4da2b71..b0f226b 100644 --- a/SUBD/ElectronicJournalContracts/SearchModels/ResultOfControlSearchModel.cs +++ b/SUBD/ElectronicJournalContracts/SearchModels/ResultOfControlSearchModel.cs @@ -4,7 +4,8 @@ { public int? Id { get; set; } public string? Form { get; set; } - public DateTime? Date { get; set; } + public DateOnly? DateFrom { get; set; } + public DateOnly? DateTo { get; set; } public string? Mark { get; set; } public int? StudentId { get; set; } public string? StudentName { get; set; } diff --git a/SUBD/ElectronicJournalContracts/SearchModels/StatementSearchModel.cs b/SUBD/ElectronicJournalContracts/SearchModels/StatementSearchModel.cs index 23d335f..5b35627 100644 --- a/SUBD/ElectronicJournalContracts/SearchModels/StatementSearchModel.cs +++ b/SUBD/ElectronicJournalContracts/SearchModels/StatementSearchModel.cs @@ -3,7 +3,8 @@ public class StatementSearchModel { public int? Id { get; set; } - public DateTime? Date { get; set; } + public DateOnly? DateFrom { get; set; } + public DateOnly? DateTo { get; set; } public int? DisciplineId { get; set; } public string? DisciplineName { get; set; } public int? GroupId { get; set; } diff --git a/SUBD/ElectronicJournalContracts/ViewModels/ResultOfControlViewModel.cs b/SUBD/ElectronicJournalContracts/ViewModels/ResultOfControlViewModel.cs index a82ed9f..887da9a 100644 --- a/SUBD/ElectronicJournalContracts/ViewModels/ResultOfControlViewModel.cs +++ b/SUBD/ElectronicJournalContracts/ViewModels/ResultOfControlViewModel.cs @@ -10,7 +10,7 @@ namespace ElectronicJournalContracts.ViewModels [DisplayName("Форма")] public string Form { get; set; } = string.Empty; [DisplayName("Дата")] - public DateTime Date { get; set; } + public DateOnly Date { get; set; } [DisplayName("Оценка")] public string Mark { get; set; } = string.Empty; public int StudentId { get; set; } diff --git a/SUBD/ElectronicJournalContracts/ViewModels/StatementViewModel.cs b/SUBD/ElectronicJournalContracts/ViewModels/StatementViewModel.cs index 3acd4f3..2de73be 100644 --- a/SUBD/ElectronicJournalContracts/ViewModels/StatementViewModel.cs +++ b/SUBD/ElectronicJournalContracts/ViewModels/StatementViewModel.cs @@ -8,7 +8,7 @@ namespace ElectronicJournalContracts.ViewModels [DisplayName("Номер")] public int Id { get; set; } [DisplayName("Дата")] - public DateTime Date { get; set; } + public DateOnly Date { get; set; } public int DisciplineId { get; set; } [DisplayName("Дисциплина")] public string DisciplineName { get; set; } = string.Empty; diff --git a/SUBD/ElectronicJournalContracts/ViewModels/TeacherViewModel.cs b/SUBD/ElectronicJournalContracts/ViewModels/TeacherViewModel.cs index e5b2396..e6dfdf2 100644 --- a/SUBD/ElectronicJournalContracts/ViewModels/TeacherViewModel.cs +++ b/SUBD/ElectronicJournalContracts/ViewModels/TeacherViewModel.cs @@ -10,6 +10,6 @@ namespace ElectronicJournalContracts.ViewModels [DisplayName("Имя")] public string Name { get; set; } = string.Empty; [DisplayName("Ученое звание")] - public string Academic_title { get; set; } = string.Empty; + public string AcademicTitle { get; set; } = string.Empty; } } diff --git a/SUBD/ElectronicJournalDataModels/Models/IResultOfControl.cs b/SUBD/ElectronicJournalDataModels/Models/IResultOfControl.cs index c240b35..a6e43a8 100644 --- a/SUBD/ElectronicJournalDataModels/Models/IResultOfControl.cs +++ b/SUBD/ElectronicJournalDataModels/Models/IResultOfControl.cs @@ -3,7 +3,7 @@ public interface IResultOfControl : IId { string Form { get; } - DateTime Date { get; } + DateOnly Date { get; } string Mark { get; } int StudentId { get; } int StatementId { get; } diff --git a/SUBD/ElectronicJournalDataModels/Models/IStatement.cs b/SUBD/ElectronicJournalDataModels/Models/IStatement.cs index 3398eb7..ba7241f 100644 --- a/SUBD/ElectronicJournalDataModels/Models/IStatement.cs +++ b/SUBD/ElectronicJournalDataModels/Models/IStatement.cs @@ -2,7 +2,7 @@ { public interface IStatement : IId { - DateTime Date { get; } + DateOnly Date { get; } int DisciplineId { get; } int GroupId { get; } int TeacherId { get; } diff --git a/SUBD/ElectronicJournalDataModels/Models/ITeacher.cs b/SUBD/ElectronicJournalDataModels/Models/ITeacher.cs index d8decba..a53d570 100644 --- a/SUBD/ElectronicJournalDataModels/Models/ITeacher.cs +++ b/SUBD/ElectronicJournalDataModels/Models/ITeacher.cs @@ -3,6 +3,6 @@ public interface ITeacher : IId { string Name { get; } - string Academic_title { get; } + string AcademicTitle { get; } } } diff --git a/SUBD/ElectronicJournalDatabaseImplement/Discipline.cs b/SUBD/ElectronicJournalDatabaseImplement/Discipline.cs deleted file mode 100644 index b21ff78..0000000 --- a/SUBD/ElectronicJournalDatabaseImplement/Discipline.cs +++ /dev/null @@ -1,23 +0,0 @@ -using ElectronicJournalDataModels.Models; -using System; -using System.Collections.Generic; - -namespace ElectronicJournalDatabaseImplement; - -/// -/// Данная таблица отвечает за хранение данных по дисциплинам. Содержит два поля: id (тип integer), название (тип varchar). -/// -public partial class Discipline -{ - /// - /// Идентификатор дисциплины - /// - public int Id { get; set; } - - /// - /// Название - /// - public string Title { get; set; } = null!; - - public virtual ICollection Statements { get; } = new List(); -} diff --git a/SUBD/ElectronicJournalDatabaseImplement/ElectronicJournalContext.cs b/SUBD/ElectronicJournalDatabaseImplement/ElectronicJournalContext.cs index 4dd27c9..e1caa1b 100644 --- a/SUBD/ElectronicJournalDatabaseImplement/ElectronicJournalContext.cs +++ b/SUBD/ElectronicJournalDatabaseImplement/ElectronicJournalContext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using ElectronicJournalDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace ElectronicJournalDatabaseImplement; diff --git a/SUBD/ElectronicJournalDatabaseImplement/Group.cs b/SUBD/ElectronicJournalDatabaseImplement/Group.cs deleted file mode 100644 index 98cb843..0000000 --- a/SUBD/ElectronicJournalDatabaseImplement/Group.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ElectronicJournalDatabaseImplement; - -/// -/// Данная таблица отвечает за хранение данных по группам. Содержит три поля: id (тип integer), название (тип varchar), курс (тип integer). -/// -public partial class Group -{ - /// - /// Идентификатор группы - /// - public int Id { get; set; } - - /// - /// Название - /// - public string Title { get; set; } = null!; - - /// - /// Курс - /// - public int Course { get; set; } - - public virtual ICollection Statements { get; } = new List(); - - public virtual ICollection Students { get; } = new List(); -} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/DisciplineStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/DisciplineStorage.cs new file mode 100644 index 0000000..202843f --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/DisciplineStorage.cs @@ -0,0 +1,84 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class DisciplineStorage : IDisciplineStorage + { + public DisciplineViewModel? Delete(DisciplineBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Disciplines.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public DisciplineViewModel? GetElement(DisciplineSearchModel model) + { + if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Disciplines + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(DisciplineSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Disciplines + .OrderBy(x => x.Title) + .Where(x => x.Title.Contains(model.Title)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Disciplines + .OrderBy(x => x.Title) + .Select(x => x.GetViewModel) + .ToList(); + } + + public DisciplineViewModel? Insert(DisciplineBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.Disciplines.Count() > 0 ? context.Disciplines.Max(x => x.Id) + 1 : 1; + var newDiscipline = Discipline.Create(model); + if (newDiscipline == null) + { + return null; + } + context.Disciplines.Add(newDiscipline); + context.SaveChanges(); + return newDiscipline.GetViewModel; + } + + public DisciplineViewModel? Update(DisciplineBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id); + if (discipline == null) + { + return null; + } + discipline.Update(model); + context.SaveChanges(); + return discipline.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/GroupStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/GroupStorage.cs new file mode 100644 index 0000000..3dff339 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/GroupStorage.cs @@ -0,0 +1,84 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class GroupStorage : IGroupStorage + { + public GroupViewModel? Delete(GroupBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.Groups.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Groups.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public GroupViewModel? GetElement(GroupSearchModel model) + { + if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Groups + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Title) && x.Title == model.Title && x.Course == model.Course) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(GroupSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Groups + .OrderBy(x => x.Title) + .Where(x => x.Title.Contains(model.Title) || x.Course == model.Course) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Groups + .OrderBy(x => x.Title) + .Select(x => x.GetViewModel) + .ToList(); + } + + public GroupViewModel? Insert(GroupBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.Groups.Count() > 0 ? context.Groups.Max(x => x.Id) + 1 : 1; + var newGroup = Group.Create(model); + if (newGroup == null) + { + return null; + } + context.Groups.Add(newGroup); + context.SaveChanges(); + return newGroup.GetViewModel; + } + + public GroupViewModel? Update(GroupBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var group = context.Groups.FirstOrDefault(x => x.Id == model.Id); + if (group == null) + { + return null; + } + group.Update(model); + context.SaveChanges(); + return group.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/ResultOfControlStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/ResultOfControlStorage.cs new file mode 100644 index 0000000..b0205b7 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/ResultOfControlStorage.cs @@ -0,0 +1,105 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; +using ElectronicJournalDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class ResultOfControlStorage : IResultOfControlStorage + { + public ResultOfControlViewModel? Delete(ResultOfControlBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.ResultOfControls.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.ResultOfControls + .Include(x => x.Student) + .Include(x => x.Statement) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + context.ResultOfControls.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + + public ResultOfControlViewModel? GetElement(ResultOfControlSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.ResultOfControls + .Include(x => x.Student) + .Include(x => x.Statement) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ResultOfControlSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.ResultOfControls + .OrderBy(x => x.Date) + .Include(x => x.Student) + .Include(x => x.Statement) + .Where(x => + (x.Date >= model.DateFrom && x.Date <= model.DateTo) + || x.Student.Name.Contains(model.StudentName) + || x.StatementId == model.StatementId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.ResultOfControls + .OrderBy(x => x.Date) + .Include(x => x.Student) + .Include(x => x.Statement) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ResultOfControlViewModel? Insert(ResultOfControlBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.ResultOfControls.Count() > 0 ? context.ResultOfControls.Max(x => x.Id) + 1 : 1; + var newResultOfControl = ResultOfControl.Create(model); + if (newResultOfControl == null) + { + return null; + } + context.ResultOfControls.Add(newResultOfControl); + context.SaveChanges(); + return context.ResultOfControls + .Include(x => x.Student) + .Include(x => x.Statement) + .FirstOrDefault(x => x.Id == newResultOfControl.Id) + ?.GetViewModel; + } + + public ResultOfControlViewModel? Update(ResultOfControlBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var result = context.ResultOfControls.FirstOrDefault(x => x.Id == model.Id); + if (result == null) + { + return null; + } + result.Update(model); + context.SaveChanges(); + return context.ResultOfControls + .Include(x => x.Student) + .Include(x => x.Statement) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/StatementStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/StatementStorage.cs new file mode 100644 index 0000000..413eca0 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/StatementStorage.cs @@ -0,0 +1,111 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class StatementStorage : IStatementStorage + { + public StatementViewModel? Delete(StatementBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.Statements.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Statements + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + context.Statements.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + + public StatementViewModel? GetElement(StatementSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Statements + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(StatementSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Statements + .OrderBy(x => x.Date) + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .Where(x => + (x.Date >= model.DateFrom && x.Date <= model.DateTo) + || x.Discipline.Title.Contains(model.DisciplineName) + || x.Group.Title.Contains(model.GroupName) + || x.Teacher.Name.Contains(model.TeacherName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Statements + .OrderBy(x => x.Date) + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StatementViewModel? Insert(StatementBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.Statements.Count() > 0 ? context.Statements.Max(x => x.Id) + 1 : 1; + var newStatement = Statement.Create(model); + if (newStatement == null) + { + return null; + } + context.Statements.Add(newStatement); + context.SaveChanges(); + return context.Statements + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .FirstOrDefault(x => x.Id == newStatement.Id) + ?.GetViewModel; + } + + public StatementViewModel? Update(StatementBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var statement = context.Statements.FirstOrDefault(x => x.Id == model.Id); + if (statement == null) + { + return null; + } + statement.Update(model); + context.SaveChanges(); + return context.Statements + .Include(x => x.Discipline) + .Include(x => x.Group) + .Include(x => x.Teacher) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/StudentStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/StudentStorage.cs new file mode 100644 index 0000000..51169c1 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/StudentStorage.cs @@ -0,0 +1,100 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class StudentStorage : IStudentStorage + { + public StudentViewModel? Delete(StudentBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Students + .Include(x => x.Group) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + context.Students.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + + public StudentViewModel? GetElement(StudentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Students + .Include(x => x.Group) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StudentSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Students + .OrderBy(x => x.Name) + .Include(x => x.Group) + .Where(x => + x.Name.Contains(model.Name) + || x.Group.Title.Contains(model.GroupName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Students + .OrderBy(x => x.Name) + .Include(x => x.Group) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StudentViewModel? Insert(StudentBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.Students.Count() > 0 ? context.Students.Max(x => x.Id) + 1 : 1; + var newStudent = Student.Create(model); + if (newStudent == null) + { + return null; + } + context.Students.Add(newStudent); + context.SaveChanges(); + return context.Students + .Include(x => x.Group) + .FirstOrDefault(x => x.Id == newStudent.Id) + ?.GetViewModel; + } + + public StudentViewModel? Update(StudentBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var student = context.Students.FirstOrDefault(x => x.Id == + model.Id); + if (student == null) + { + return null; + } + student.Update(model); + context.SaveChanges(); + return context.Students + .Include(x => x.Group) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Implements/TeacherStorage.cs b/SUBD/ElectronicJournalDatabaseImplement/Implements/TeacherStorage.cs new file mode 100644 index 0000000..2a83a38 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Implements/TeacherStorage.cs @@ -0,0 +1,84 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.SearchModels; +using ElectronicJournalContracts.StorageContracts; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDatabaseImplement.Models; + +namespace ElectronicJournalDatabaseImplement.Implements +{ + public class TeacherStorage : ITeacherStorage + { + public TeacherViewModel? Delete(TeacherBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var element = context.Teachers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Teachers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public TeacherViewModel? GetElement(TeacherSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Teachers + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name && x.AcademicTitle == model.Academic_title) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(TeacherSearchModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Teachers + .OrderBy(x => x.Name) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + ElectronicJournalContext context = new ElectronicJournalContext(); + return context.Teachers + .OrderBy(x => x.Name) + .Select(x => x.GetViewModel) + .ToList(); + } + + public TeacherViewModel? Insert(TeacherBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + model.Id = context.Teachers.Count() > 0 ? context.Teachers.Max(x => x.Id) + 1 : 1; + var newTeacher = Teacher.Create(model); + if (newTeacher == null) + { + return null; + } + context.Teachers.Add(newTeacher); + context.SaveChanges(); + return newTeacher.GetViewModel; + } + + public TeacherViewModel? Update(TeacherBindingModel model) + { + ElectronicJournalContext context = new ElectronicJournalContext(); + var teacher = context.Teachers.FirstOrDefault(x => x.Id == model.Id); + if (teacher == null) + { + return null; + } + teacher.Update(model); + context.SaveChanges(); + return teacher.GetViewModel; + } + } +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Models/Discipline.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/Discipline.cs new file mode 100644 index 0000000..930d499 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/Discipline.cs @@ -0,0 +1,58 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; +using System.Collections.Generic; + +namespace ElectronicJournalDatabaseImplement.Models; + +/// +/// Данная таблица отвечает за хранение данных по дисциплинам. Содержит два поля: id (тип integer), название (тип varchar). +/// +public partial class Discipline : IDiscipline +{ + /// + /// Идентификатор дисциплины + /// + public int Id { get; set; } + + /// + /// Название + /// + public string Title { get; set; } = null!; + + public virtual List Statements { get; } = new List(); + public static Discipline? Create(DisciplineBindingModel model) + { + if (model == null) + { + return null; + } + return new Discipline() + { + Id = model.Id, + Title = model.Title + }; + } + public static Discipline Create(DisciplineViewModel model) + { + return new Discipline + { + Id = model.Id, + Title = model.Title + }; + } + public void Update(DisciplineBindingModel model) + { + if (model == null) + { + return; + } + Title = model.Title; + } + public DisciplineViewModel GetViewModel => new() + { + Id = Id, + Title = Title + }; +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Models/Group.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/Group.cs new file mode 100644 index 0000000..48b61b9 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/Group.cs @@ -0,0 +1,69 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; +using System.Collections.Generic; + +namespace ElectronicJournalDatabaseImplement.Models; + +/// +/// Данная таблица отвечает за хранение данных по группам. Содержит три поля: id (тип integer), название (тип varchar), курс (тип integer). +/// +public partial class Group : IGroup +{ + /// + /// Идентификатор группы + /// + public int Id { get; set; } + + /// + /// Название + /// + public string Title { get; set; } = null!; + + /// + /// Курс + /// + public int Course { get; set; } + + public virtual List Statements { get; } = new List(); + + public virtual List Students { get; } = new List(); + public static Group? Create(GroupBindingModel model) + { + if (model == null) + { + return null; + } + return new Group() + { + Id = model.Id, + Title = model.Title, + Course = model.Course + }; + } + public static Group Create(GroupViewModel model) + { + return new Group + { + Id = model.Id, + Title = model.Title, + Course = model.Course + }; + } + public void Update(GroupBindingModel model) + { + if (model == null) + { + return; + } + Title = model.Title; + Course = model.Course; + } + public GroupViewModel GetViewModel => new() + { + Id = Id, + Title = Title, + Course = Course + }; +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/ResultOfControl.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/ResultOfControl.cs similarity index 50% rename from SUBD/ElectronicJournalDatabaseImplement/ResultOfControl.cs rename to SUBD/ElectronicJournalDatabaseImplement/Models/ResultOfControl.cs index e80502c..613fe5c 100644 --- a/SUBD/ElectronicJournalDatabaseImplement/ResultOfControl.cs +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/ResultOfControl.cs @@ -1,12 +1,16 @@ -using System; +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; using System.Collections.Generic; +using System.Text.RegularExpressions; -namespace ElectronicJournalDatabaseImplement; +namespace ElectronicJournalDatabaseImplement.Models; /// /// Данная таблица отвечает за хранение данных по результатам контроля. Содержит шесть полей: id (тип integer), форма контроля (тип varchar), дата (тип date), оценка (тип varchar), внешний ключ на студента (тип integer), внешний ключ на ведомость (тип integer). /// -public partial class ResultOfControl +public partial class ResultOfControl : IResultOfControl { /// /// Идентификатор результата контроля @@ -41,4 +45,54 @@ public partial class ResultOfControl public virtual Statement Statement { get; set; } = null!; public virtual Student Student { get; set; } = null!; + public static ResultOfControl? Create(ResultOfControlBindingModel model) + { + if (model == null) + { + return null; + } + return new ResultOfControl() + { + Id = model.Id, + Form = model.Form, + Date = model.Date, + Mark = model.Mark, + StudentId = model.StudentId, + StatementId = model.StatementId + }; + } + public static ResultOfControl Create(ResultOfControlViewModel model) + { + return new ResultOfControl + { + Id = model.Id, + Form = model.Form, + Date = model.Date, + Mark = model.Mark, + StudentId = model.StudentId, + StatementId = model.StatementId + }; + } + public void Update(ResultOfControlBindingModel model) + { + if (model == null) + { + return; + } + Form = model.Form; + Date = model.Date; + Mark = model.Mark; + StudentId = model.StudentId; + StatementId = model.StatementId; + } + public ResultOfControlViewModel GetViewModel => new() + { + Id = Id, + Form = Form, + Date = Date, + Mark = Mark, + StudentId = StudentId, + StatementId = StatementId, + StudentName = Student.Name + }; } diff --git a/SUBD/ElectronicJournalDatabaseImplement/Models/Statement.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/Statement.cs new file mode 100644 index 0000000..42f784d --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/Statement.cs @@ -0,0 +1,94 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; +using System.Collections.Generic; + +namespace ElectronicJournalDatabaseImplement.Models; + +/// +/// Данная таблица отвечает за хранение данных по ведомостям. Содержит пять полей: id (тип integer), дата (тип date), внешний ключ на дисциплину (тип integer), внешний ключ на группу (тип integer), внешний ключ на преподавателя (тип integer). +/// +public partial class Statement : IStatement +{ + /// + /// Идентификатор ведомости + /// + public int Id { get; set; } + + /// + /// Дата + /// + public DateOnly Date { get; set; } + + /// + /// Внешний ключ на дисциплину + /// + public int DisciplineId { get; set; } + + /// + /// Внешний ключ на группу + /// + public int GroupId { get; set; } + + /// + /// Внешний ключ на преподавателя + /// + public int TeacherId { get; set; } + + public virtual Discipline Discipline { get; set; } = null!; + + public virtual Group Group { get; set; } = null!; + + public virtual List ResultOfControls { get; } = new List(); + + public virtual Teacher Teacher { get; set; } = null!; + public static Statement? Create(StatementBindingModel model) + { + if (model == null) + { + return null; + } + return new Statement() + { + Id = model.Id, + Date = model.Date, + DisciplineId = model.DisciplineId, + TeacherId = model.TeacherId, + GroupId = model.GroupId + }; + } + public static Statement Create(StatementViewModel model) + { + return new Statement + { + Id = model.Id, + Date = model.Date, + DisciplineId = model.DisciplineId, + TeacherId = model.TeacherId, + GroupId = model.GroupId + }; + } + public void Update(StatementBindingModel model) + { + if (model == null) + { + return; + } + Date = model.Date; + DisciplineId = model.DisciplineId; + TeacherId = model.TeacherId; + GroupId = model.GroupId; + } + public StatementViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + DisciplineId = DisciplineId, + GroupId = GroupId, + TeacherId = TeacherId, + DisciplineName = Discipline.Title, + GroupName = Group.Title, + TeacherName = Teacher.Name + }; +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Models/Student.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/Student.cs new file mode 100644 index 0000000..dd1b1db --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/Student.cs @@ -0,0 +1,70 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; +using System.Collections.Generic; + +namespace ElectronicJournalDatabaseImplement.Models; + +/// +/// Данная таблица отвечает за хранение данных по студентам. Содержит три поля: id (тип integer), имя (тип varchar), внешний ключ на группу (тип integer). +/// +public partial class Student : IStudent +{ + /// + /// Идентификатор студента + /// + public int Id { get; set; } + + /// + /// Имя + /// + public string Name { get; set; } = null!; + + /// + /// Внешний ключ на группу + /// + public int GroupId { get; set; } + + public virtual Group Group { get; set; } = null!; + + public virtual List ResultOfControls { get; } = new List(); + public static Student? Create(StudentBindingModel model) + { + if (model == null) + { + return null; + } + return new Student() + { + Id = model.Id, + Name = model.Name, + GroupId = model.GroupId + }; + } + public static Student Create(StudentViewModel model) + { + return new Student + { + Id = model.Id, + Name = model.Name, + GroupId = model.GroupId + }; + } + public void Update(StudentBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + GroupId = model.GroupId; + } + public StudentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + GroupId = GroupId, + GroupName = Group.Title + }; +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Models/Teacher.cs b/SUBD/ElectronicJournalDatabaseImplement/Models/Teacher.cs new file mode 100644 index 0000000..ad2d5f0 --- /dev/null +++ b/SUBD/ElectronicJournalDatabaseImplement/Models/Teacher.cs @@ -0,0 +1,67 @@ +using ElectronicJournalContracts.BindingModels; +using ElectronicJournalContracts.ViewModels; +using ElectronicJournalDataModels.Models; +using System; +using System.Collections.Generic; + +namespace ElectronicJournalDatabaseImplement.Models; + +/// +/// Данная таблица отвечает за хранение данных по преподавателям. Содержит три поля: id (тип integer), имя (тип varchar), ученое звание (тип integer). +/// +public partial class Teacher : ITeacher +{ + /// + /// Идентификатор преподавателя + /// + public int Id { get; set; } + + /// + /// Имя + /// + public string Name { get; set; } = null!; + + /// + /// Ученое звание + /// + public string? AcademicTitle { get; set; } + + public virtual List Statements { get; } = new List(); + public static Teacher? Create(TeacherBindingModel model) + { + if (model == null) + { + return null; + } + return new Teacher() + { + Id = model.Id, + Name = model.Name, + AcademicTitle = model.AcademicTitle + }; + } + public static Teacher Create(TeacherViewModel model) + { + return new Teacher + { + Id = model.Id, + Name = model.Name, + AcademicTitle = model.AcademicTitle + }; + } + public void Update(TeacherBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + AcademicTitle = model.AcademicTitle; + } + public TeacherViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + AcademicTitle = AcademicTitle + }; +} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Statement.cs b/SUBD/ElectronicJournalDatabaseImplement/Statement.cs deleted file mode 100644 index 82574d8..0000000 --- a/SUBD/ElectronicJournalDatabaseImplement/Statement.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ElectronicJournalDatabaseImplement; - -/// -/// Данная таблица отвечает за хранение данных по ведомостям. Содержит пять полей: id (тип integer), дата (тип date), внешний ключ на дисциплину (тип integer), внешний ключ на группу (тип integer), внешний ключ на преподавателя (тип integer). -/// -public partial class Statement -{ - /// - /// Идентификатор ведомости - /// - public int Id { get; set; } - - /// - /// Дата - /// - public DateOnly Date { get; set; } - - /// - /// Внешний ключ на дисциплину - /// - public int DisciplineId { get; set; } - - /// - /// Внешний ключ на группу - /// - public int GroupId { get; set; } - - /// - /// Внешний ключ на преподавателя - /// - public int TeacherId { get; set; } - - public virtual Discipline Discipline { get; set; } = null!; - - public virtual Group Group { get; set; } = null!; - - public virtual ICollection ResultOfControls { get; } = new List(); - - public virtual Teacher Teacher { get; set; } = null!; -} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Student.cs b/SUBD/ElectronicJournalDatabaseImplement/Student.cs deleted file mode 100644 index 84baded..0000000 --- a/SUBD/ElectronicJournalDatabaseImplement/Student.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ElectronicJournalDatabaseImplement; - -/// -/// Данная таблица отвечает за хранение данных по студентам. Содержит три поля: id (тип integer), имя (тип varchar), внешний ключ на группу (тип integer). -/// -public partial class Student -{ - /// - /// Идентификатор студента - /// - public int Id { get; set; } - - /// - /// Имя - /// - public string Name { get; set; } = null!; - - /// - /// Внешний ключ на группу - /// - public int GroupId { get; set; } - - public virtual Group Group { get; set; } = null!; - - public virtual ICollection ResultOfControls { get; } = new List(); -} diff --git a/SUBD/ElectronicJournalDatabaseImplement/Teacher.cs b/SUBD/ElectronicJournalDatabaseImplement/Teacher.cs deleted file mode 100644 index 2240804..0000000 --- a/SUBD/ElectronicJournalDatabaseImplement/Teacher.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ElectronicJournalDatabaseImplement; - -/// -/// Данная таблица отвечает за хранение данных по преподавателям. Содержит три поля: id (тип integer), имя (тип varchar), ученое звание (тип integer). -/// -public partial class Teacher -{ - /// - /// Идентификатор преподавателя - /// - public int Id { get; set; } - - /// - /// Имя - /// - public string Name { get; set; } = null!; - - /// - /// Ученое звание - /// - public string? AcademicTitle { get; set; } - - public virtual ICollection Statements { get; } = new List(); -}