diff --git a/University/DatabaseImplement/Implements/ActivityStorage.cs b/University/DatabaseImplement/Implements/ActivityStorage.cs index 8ccd6ea..d805983 100644 --- a/University/DatabaseImplement/Implements/ActivityStorage.cs +++ b/University/DatabaseImplement/Implements/ActivityStorage.cs @@ -8,5 +8,72 @@ namespace UniversityDatabaseImplement.Implements { internal class ActivityStorage : IActivityStorage { + 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; + } + + 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(); + return context.Activities + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.Activities.Select(x => x.GetViewModel).ToList(); + } + + public ActivityViewModel? Insert(ActivityBindingModel model) + { + var newActivity = Activity.Create(model); + if (newActivity == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.Activities.Add(newActivity); + context.SaveChanges(); + return newActivity.GetViewModel; + } + + public ActivityViewModel? Update(ActivityBindingModel model) + { + using var context = new UniversityDatabase(); + var activity = context.Activities.FirstOrDefault(x => x.Id == model.Id); + if (activity == null) + { + return null; + } + activity.Update(model); + context.SaveChanges(); + return activity.GetViewModel; + } } } diff --git a/University/DatabaseImplement/Implements/DisciplineStorage.cs b/University/DatabaseImplement/Implements/DisciplineStorage.cs index e55cbf7..8e3c4b5 100644 --- a/University/DatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/DatabaseImplement/Implements/DisciplineStorage.cs @@ -8,5 +8,74 @@ namespace UniversityDatabaseImplement.Implements { internal class DisciplineStorage : IDisciplineStorage { + 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; + } + + 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(); + return context.Disciplines + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.Disciplines.Select(x => x.GetViewModel).ToList(); + } + + public DisciplineViewModel? Insert(DisciplineBindingModel model) + { + var newDiscipline = Discipline.Create(model); + if (newDiscipline == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.Disciplines.Add(newDiscipline); + context.SaveChanges(); + return newDiscipline.GetViewModel; + } + + public DisciplineViewModel? Update(DisciplineBindingModel model) + { + using var context = new UniversityDatabase(); + 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/University/DatabaseImplement/Implements/ExaminationResultStorage.cs b/University/DatabaseImplement/Implements/ExaminationResultStorage.cs index e8c8343..5882682 100644 --- a/University/DatabaseImplement/Implements/ExaminationResultStorage.cs +++ b/University/DatabaseImplement/Implements/ExaminationResultStorage.cs @@ -8,5 +8,72 @@ namespace UniversityDatabaseImplement.Implements { internal class ExaminationResultStorage : IExaminationResultStorage { + 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; + } + + 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(); + return context.ExaminationResults + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.ExaminationResults.Select(x => x.GetViewModel).ToList(); + } + + public ExaminationResultViewModel? Insert(ExaminationResultBindingModel model) + { + var newExaminationResult = ExaminationResult.Create(model); + if (newExaminationResult == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.ExaminationResults.Add(newExaminationResult); + context.SaveChanges(); + return newExaminationResult.GetViewModel; + } + + public ExaminationResultViewModel? Update(ExaminationResultBindingModel model) + { + using var context = new UniversityDatabase(); + var examinationResult = context.ExaminationResults.FirstOrDefault(x => x.Id == model.Id); + if (examinationResult == null) + { + return null; + } + examinationResult.Update(model); + context.SaveChanges(); + return examinationResult.GetViewModel; + } } } diff --git a/University/DatabaseImplement/Implements/ReportTypeStorage.cs b/University/DatabaseImplement/Implements/ReportTypeStorage.cs index 4dbf87d..bcb57af 100644 --- a/University/DatabaseImplement/Implements/ReportTypeStorage.cs +++ b/University/DatabaseImplement/Implements/ReportTypeStorage.cs @@ -8,5 +8,74 @@ namespace UniversityDatabaseImplement.Implements { internal class ReportTypeStorage : IReportTypeStorage { + 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; + } + + 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(); + return context.ReportTypes + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.ReportTypes.Select(x => x.GetViewModel).ToList(); + } + + public ReportTypeViewModel? Insert(ReportTypeBindingModel model) + { + var newReportType = ReportType.Create(model); + if (newReportType == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.ReportTypes.Add(newReportType); + context.SaveChanges(); + return newReportType.GetViewModel; + } + + public ReportTypeViewModel? Update(ReportTypeBindingModel model) + { + using var context = new UniversityDatabase(); + var reportType = context.ReportTypes.FirstOrDefault(x => x.Id == model.Id); + if (reportType == null) + { + return null; + } + reportType.Update(model); + context.SaveChanges(); + return reportType.GetViewModel; + } } } diff --git a/University/DatabaseImplement/Implements/StatementStorage.cs b/University/DatabaseImplement/Implements/StatementStorage.cs index fdce169..72ed6da 100644 --- a/University/DatabaseImplement/Implements/StatementStorage.cs +++ b/University/DatabaseImplement/Implements/StatementStorage.cs @@ -8,5 +8,72 @@ namespace UniversityDatabaseImplement.Implements { internal class StatementStorage : IStatementStorage { + 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; + } + + public StatementViewModel? GetElement(StatementSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new UniversityDatabase(); + return context.Statements + .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); + if (newStatement == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.Statements.Add(newStatement); + context.SaveChanges(); + return newStatement.GetViewModel; + } + + public StatementViewModel? Update(StatementBindingModel model) + { + using var context = new UniversityDatabase(); + var statement = context.Statements.FirstOrDefault(x => x.Id == model.Id); + if (statement == null) + { + return null; + } + statement.Update(model); + context.SaveChanges(); + return statement.GetViewModel; + } } } diff --git a/University/DatabaseImplement/Implements/StudentStorage.cs b/University/DatabaseImplement/Implements/StudentStorage.cs index 8943ff4..e474dfd 100644 --- a/University/DatabaseImplement/Implements/StudentStorage.cs +++ b/University/DatabaseImplement/Implements/StudentStorage.cs @@ -8,5 +8,74 @@ namespace UniversityDatabaseImplement.Implements { internal class StudentStorage : IStudentStorage { + 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; + } + + 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; + } + + public List GetFilteredList(StudentSearchModel model) + { + using var context = new UniversityDatabase(); + return context.Students + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Name) || x.Name.Contains(model.Name)) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.Students.Select(x => x.GetViewModel).ToList(); + } + + public StudentViewModel? Insert(StudentBindingModel model) + { + var newStudent = Student.Create(model); + if (newStudent == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.Students.Add(newStudent); + context.SaveChanges(); + return newStudent.GetViewModel; + } + + public StudentViewModel? Update(StudentBindingModel model) + { + using var context = new UniversityDatabase(); + var student = context.Students.FirstOrDefault(x => x.Id == model.Id); + if (student == null) + { + return null; + } + student.Update(model); + context.SaveChanges(); + return student.GetViewModel; + } } } diff --git a/University/DatabaseImplement/Models/Activity.cs b/University/DatabaseImplement/Models/Activity.cs index c630895..3afad2d 100644 --- a/University/DatabaseImplement/Models/Activity.cs +++ b/University/DatabaseImplement/Models/Activity.cs @@ -15,5 +15,31 @@ namespace UniversityDatabaseImplement.Models public int Number { get; set; } [ForeignKey("ActivityId")] public virtual List ReportTypeActivities { get; set; } = new(); + + public static Activity Create(ActivityBindingModel model) + { + return new Activity + { + Id = model.Id, + Date = model.Date, + Number = model.Number + }; + } + + public void Update(ActivityBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Date = model.Date; + Number = model.Number; + } + + public ActivityViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + Number = Number + }; } } diff --git a/University/DatabaseImplement/Models/Discipline.cs b/University/DatabaseImplement/Models/Discipline.cs index 9d0eb80..26621db 100644 --- a/University/DatabaseImplement/Models/Discipline.cs +++ b/University/DatabaseImplement/Models/Discipline.cs @@ -17,5 +17,31 @@ namespace UniversityDatabaseImplement.Models public virtual List Statements { get; set; } = new(); [ForeignKey("DisciplineId")] public virtual List ReportTypeDisciplines { get; set; } = new(); + + public static Discipline Create(DisciplineBindingModel model) + { + return new Discipline + { + Id = model.Id, + Name = model.Name, + Department = model.Department + }; + } + + public void Update(DisciplineBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Name = model.Name; + Department = model.Department; + } + + public DisciplineViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Department = Department + }; } } diff --git a/University/DatabaseImplement/Models/ExaminationResult.cs b/University/DatabaseImplement/Models/ExaminationResult.cs index 15bdf1d..fc29881 100644 --- a/University/DatabaseImplement/Models/ExaminationResult.cs +++ b/University/DatabaseImplement/Models/ExaminationResult.cs @@ -19,5 +19,33 @@ namespace UniversityDatabaseImplement.Models public virtual List Activities { get; set; } = new(); [ForeignKey("ExaminationResultId")] public virtual List StudentExaminationResults { get; set; } = new(); + public static ExaminationResult Create(ExaminationResultBindingModel model) + { + return new ExaminationResult + { + Id = model.Id, + ExaminationForm = model.ExaminationForm, + Mark = model.Mark, + Date = model.Date + }; + } + + public void Update(ExaminationResultBindingModel model) + { + if (model == null) return; + + Id = model.Id; + ExaminationForm = model.ExaminationForm; + Mark = model.Mark; + Date = model.Date; + } + + public ExaminationResultViewModel GetViewModel => new() + { + Id = Id, + ExaminationForm = ExaminationForm, + Mark = Mark, + Date = Date + }; } } diff --git a/University/DatabaseImplement/Models/ReportType.cs b/University/DatabaseImplement/Models/ReportType.cs index c51f37e..acf60ba 100644 --- a/University/DatabaseImplement/Models/ReportType.cs +++ b/University/DatabaseImplement/Models/ReportType.cs @@ -15,5 +15,26 @@ namespace UniversityDatabaseImplement.Models public virtual List ReportTypeActivities { get; set; } = new(); [ForeignKey("ReportTypeId")] public virtual List ReportTypeDisciplines { get; set; } = new(); + public static ReportType Create(ReportTypeBindingModel model) + { + return new ReportType + { + Id = model.Id, + Name = model.Name + }; + } + public void Update(ReportTypeBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Name = model.Name; + } + + public ReportTypeViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; } } diff --git a/University/DatabaseImplement/Models/ReportTypeActivity.cs b/University/DatabaseImplement/Models/ReportTypeActivity.cs index 16d3223..c1ca5f5 100644 --- a/University/DatabaseImplement/Models/ReportTypeActivity.cs +++ b/University/DatabaseImplement/Models/ReportTypeActivity.cs @@ -11,6 +11,5 @@ namespace UniversityDatabaseImplement.Models public int ActivityId { get; set; } public virtual ReportType ReportType { get; set; } = new(); public virtual Activity Activity { get; set; } = new(); - } } diff --git a/University/DatabaseImplement/Models/Statement.cs b/University/DatabaseImplement/Models/Statement.cs index 4ebfa57..05655ed 100644 --- a/University/DatabaseImplement/Models/Statement.cs +++ b/University/DatabaseImplement/Models/Statement.cs @@ -17,5 +17,29 @@ namespace UniversityDatabaseImplement.Models public virtual List StatementStudents { get; set; } = new(); [ForeignKey("StatementId")] public virtual List ExaminationResults { get; set; } = new(); + public static Statement Create(StatementBindingModel model) + { + return new Statement + { + Id = model.Id, + Date = model.Date, + HoursCount = model.HoursCount + }; + } + public void Update(StatementBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Date = model.Date; + HoursCount = model.HoursCount; + } + + public StatementViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + HoursCount = HoursCount + }; } } diff --git a/University/DatabaseImplement/Models/Student.cs b/University/DatabaseImplement/Models/Student.cs index 80860a9..d7bff7d 100644 --- a/University/DatabaseImplement/Models/Student.cs +++ b/University/DatabaseImplement/Models/Student.cs @@ -17,5 +17,29 @@ namespace UniversityDatabaseImplement.Models public virtual List StatementStudents { get; set; } = new(); [ForeignKey("StudentId")] public virtual List StudentExaminationResults { get; set; } = new(); + public static Student Create(StudentBindingModel model) + { + return new Student + { + Id = model.Id, + Name = model.Name, + RecordCardNumber = model.RecordCardNumber + }; + } + public void Update(StudentBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Name = model.Name; + RecordCardNumber = model.RecordCardNumber; + } + + public StudentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + RecordCardNumber = RecordCardNumber + }; } } diff --git a/University/DatabaseImplement/UniversityDatabase.cs b/University/DatabaseImplement/UniversityDatabase.cs index c429b98..3c78a0b 100644 --- a/University/DatabaseImplement/UniversityDatabase.cs +++ b/University/DatabaseImplement/UniversityDatabase.cs @@ -1,9 +1,9 @@ using UniversityDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; -namespace PizzeriaDatabaseImplement +namespace UniversityDatabaseImplement { - public class PizzeriaDatabase : DbContext + public class UniversityDatabase : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -13,15 +13,15 @@ namespace PizzeriaDatabaseImplement } base.OnConfiguring(optionsBuilder); } - public virtual DbSet Activity { set; get; } - public virtual DbSet Discipline { set; get; } - public virtual DbSet ExaminationResult { set; get; } - public virtual DbSet ReportType { set; get; } - public virtual DbSet ReportTypeActivity { set; get; } - public virtual DbSet ReportTypeDiscipline { set; get; } - public virtual DbSet Statement { set; get; } - public virtual DbSet StatementStudent { set; get; } - public virtual DbSet Student { set; get; } - public virtual DbSet StudentExaminationResult { set; get; } + public virtual DbSet Activities { set; get; } + public virtual DbSet Disciplines { set; get; } + public virtual DbSet ExaminationResults { set; get; } + public virtual DbSet ReportTypes { set; get; } + public virtual DbSet ReportTypeActivities { set; get; } + public virtual DbSet ReportTypeDisciplines { set; get; } + public virtual DbSet Statements { set; get; } + public virtual DbSet StatementStudents { set; get; } + public virtual DbSet Students { set; get; } + public virtual DbSet StudentExaminationResults { set; get; } } } \ No newline at end of file