From e1482243ed7b5e1e1ce32f4d9499afab5e524931 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:57:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D1=82=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD=D0=B0=D0=B4=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B9=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/AttestationSearchModel.cs | 2 +- .../Implements/AttestationStorage.cs | 88 ++++++++++++- .../Implements/PlanOfStudyStorage.cs | 88 ++++++++++++- .../Models/Attestation.cs | 117 +++++++++--------- .../Models/PlanOfStudy.cs | 5 +- .../Models/PlanOfStudyTeacher.cs | 4 +- .../Models/Student.cs | 1 + .../Models/Worker.cs | 14 +++ 8 files changed, 255 insertions(+), 64 deletions(-) create mode 100644 University/UniversityDatabaseImplement/Models/Worker.cs diff --git a/University/UniversityContracts/SearchModels/AttestationSearchModel.cs b/University/UniversityContracts/SearchModels/AttestationSearchModel.cs index 91c4141..cb3d1b8 100644 --- a/University/UniversityContracts/SearchModels/AttestationSearchModel.cs +++ b/University/UniversityContracts/SearchModels/AttestationSearchModel.cs @@ -9,6 +9,6 @@ namespace UniversityContracts.SearchModels public class AttestationSearchModel { public int? Id { get; set; } - public string? FormOfEvaluation { get; set; } + public int? StudentId { get; set; } } } diff --git a/University/UniversityDatabaseImplement/Implements/AttestationStorage.cs b/University/UniversityDatabaseImplement/Implements/AttestationStorage.cs index 8685292..a49307e 100644 --- a/University/UniversityDatabaseImplement/Implements/AttestationStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/AttestationStorage.cs @@ -1,13 +1,99 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; using UniversityContracts.StorageContracts; +using UniversityContracts.ViewModels; +using UniversityDatabaseImplement.Models; namespace UniversityDatabaseImplement.Implements { public class AttestationStorage : IAttestationStorage { + public List GetFullList() + { + using var context = new UniversityDatabase(); + + return context.Attestations.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(AttestationSearchModel model) + { + using var context = new UniversityDatabase(); + if (model.Id.HasValue) + { + return context.Attestations + .Include(x => x.Student) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.StudentId.HasValue) + { + return context.Attestations + .Include(x => x.Student) + .Where(x => x.StudentId == model.StudentId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return new(); + } + + public AttestationViewModel? GetElement(AttestationSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new UniversityDatabase(); + return context.Attestations.Include(x => x.Student).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public AttestationViewModel? Insert(AttestationBindingModel model) + { + var newAttestation = Attestation.Create(model); + + if (newAttestation == null) + { + return null; + } + + using var context = new UniversityDatabase(); + + context.Attestations.Add(newAttestation); + context.SaveChanges(); + + return newAttestation.GetViewModel; + } + + public AttestationViewModel? Update(AttestationBindingModel model) + { + using var context = new UniversityDatabase(); + var order = context.Attestations.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Attestations.Include(x => x.Student).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public AttestationViewModel? Delete(AttestationBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Attestations.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Attestations.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/UniversityDatabaseImplement/Implements/PlanOfStudyStorage.cs b/University/UniversityDatabaseImplement/Implements/PlanOfStudyStorage.cs index cad2218..a44d46d 100644 --- a/University/UniversityDatabaseImplement/Implements/PlanOfStudyStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/PlanOfStudyStorage.cs @@ -1,13 +1,99 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; using UniversityContracts.StorageContracts; +using UniversityContracts.ViewModels; +using UniversityDatabaseImplement.Models; namespace UniversityDatabaseImplement.Implements { public class PlanOfStudyStorage : IPlanOfStudyStorage { + public List GetFullList() + { + using var context = new UniversityDatabase(); + + return context.PlanOfStudys.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(PlanOfStudySearchModel model) + { + using var context = new UniversityDatabase(); + if (model.Id.HasValue) + { + return context.PlanOfStudys + .Include(x => x.Student) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.StudentId.HasValue) + { + return context.Attestations + .Include(x => x.Student) + .Where(x => x.StudentId == model.StudentId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return new(); + } + + public PlanOfStudyViewModel? GetElement(PlanOfStudySearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new UniversityDatabase(); + return context.Attestations.Include(x => x.Student).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public PlanOfStudyViewModel? Insert(PlanOfStudyBindingModel model) + { + var newAttestation = Attestation.Create(model); + + if (newAttestation == null) + { + return null; + } + + using var context = new UniversityDatabase(); + + context.Attestations.Add(newAttestation); + context.SaveChanges(); + + return newAttestation.GetViewModel; + } + + public PlanOfStudyViewModel? Update(PlanOfStudyBindingModel model) + { + using var context = new UniversityDatabase(); + var order = context.Attestations.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Attestations.Include(x => x.Student).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public PlanOfStudyViewModel? Delete(PlanOfStudyBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Attestations.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Attestations.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/University/UniversityDatabaseImplement/Models/Attestation.cs b/University/UniversityDatabaseImplement/Models/Attestation.cs index dcd5f09..e89222a 100644 --- a/University/UniversityDatabaseImplement/Models/Attestation.cs +++ b/University/UniversityDatabaseImplement/Models/Attestation.cs @@ -1,63 +1,66 @@ -using System; -using System.Collections.Generic; -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 -{ - public class Attestation : IAttestationModel - { - public int Id { get; set; } - public int StudentId { get; set; } - [Required] - public string FormOfEvaluation { get; private set; } = string.Empty; - [Required] - public string Score { get; private set; } = string.Empty; - } - public static Student? Create(StudentBindingModel model) +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; +using UniversityDataModels.Enums; +using UniversityDataModels.Models; +using static System.Formats.Asn1.AsnWriter; + +namespace UniversityDatabaseImplement.Models +{ + public class Attestation : IAttestationModel { - if (model == null) + public int Id { get; private set; } + public int StudentId { get; private set; } + [Required] + public string FormOfEvaluation { get; private set; } = string.Empty; + [Required] + public AttestationScore Score { get; private set; } = AttestationScore.Неявка; + public virtual Student Student { get; set; } = new(); + public static Attestation? Create(AttestationBindingModel model) { - return null; + if (model == null) + { + return null; + } + return new Attestation() + { + Id = model.Id, + StudentId = model.StudentId, + FormOfEvaluation = model.FormOfEvaluation, + Score = model.Score + }; } - return new Student() + public static Attestation Create(AttestationViewModel model) { - Id = model.Id, - PlanOfStudyId = model.PlanOfStudyId, - Name = model.Name, - PhoneNumber = model.PhoneNumber + return new Attestation + { + Id = model.Id, + StudentId = model.StudentId, + FormOfEvaluation = model.FormOfEvaluation, + Score = model.Score + }; + } + public void Update(AttestationBindingModel model) + { + if (model == null) + { + return; + } + StudentId = model.StudentId; + FormOfEvaluation = model.FormOfEvaluation; + Score = model.Score; + } + public AttestationViewModel GetViewModel => new() + { + Id = Id, + StudentId = StudentId, + FormOfEvaluation = FormOfEvaluation, + Score = Score }; } - 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 - }; -} +} diff --git a/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs b/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs index 24ecae4..8bee4d8 100644 --- a/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs +++ b/University/UniversityDatabaseImplement/Models/PlanOfStudy.cs @@ -13,8 +13,8 @@ namespace UniversityDatabaseImplement.Models { public class PlanOfStudy : IPlanOfStudyModel { - public int Id { get; set; } - public int WorkerId { get; set; } + public int Id { get; private set; } + public int WorkerId { get; private set; } [Required] public string Profile { get; private set; } = string.Empty; [Required] @@ -35,6 +35,7 @@ namespace UniversityDatabaseImplement.Models } [ForeignKey("PlanOfStudyId")] public virtual List Teachers { get; set; } = new(); + public virtual Worker Worker { get; set; } = new(); public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model) { return new PlanOfStudy() diff --git a/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs b/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs index fbd8a59..884511a 100644 --- a/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs +++ b/University/UniversityDatabaseImplement/Models/PlanOfStudyTeacher.cs @@ -14,7 +14,7 @@ namespace UniversityDatabaseImplement.Models public int PlanOfStudyId { get; set; } [Required] public int TeacherId { get; set; } - public virtual PlanOfStudy PlanOfStudy { get; set; } - public virtual Teacher Teacher { get; set; } + public virtual PlanOfStudy PlanOfStudy { get; set; } = new(); + public virtual Teacher Teacher { get; set; } = new(); } } diff --git a/University/UniversityDatabaseImplement/Models/Student.cs b/University/UniversityDatabaseImplement/Models/Student.cs index 4753193..0e2b4c1 100644 --- a/University/UniversityDatabaseImplement/Models/Student.cs +++ b/University/UniversityDatabaseImplement/Models/Student.cs @@ -22,6 +22,7 @@ namespace UniversityDatabaseImplement.Models public string PhoneNumber { get; private set; } = string.Empty; [ForeignKey("StudentId")] public virtual List StudentDiscipline { get; set; } = new(); + public virtual PlanOfStudy PlanOfStudy { get; set; } = new(); public static Student? Create(StudentBindingModel model) { if (model == null) diff --git a/University/UniversityDatabaseImplement/Models/Worker.cs b/University/UniversityDatabaseImplement/Models/Worker.cs new file mode 100644 index 0000000..8fbdecc --- /dev/null +++ b/University/UniversityDatabaseImplement/Models/Worker.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityDataModels.Models; + +namespace UniversityDatabaseImplement.Models +{ + public class Worker : IWorkerModel + { + + } +}