add databaseImplement

This commit is contained in:
MaxKarme 2023-04-04 22:36:39 +04:00
parent 4a787d1931
commit 1503a5c132
14 changed files with 569 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,5 +15,31 @@ namespace UniversityDatabaseImplement.Models
public int Number { get; set; }
[ForeignKey("ActivityId")]
public virtual List<ReportTypeActivity> 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
};
}
}

View File

@ -17,5 +17,31 @@ namespace UniversityDatabaseImplement.Models
public virtual List<Statement> Statements { get; set; } = new();
[ForeignKey("DisciplineId")]
public virtual List<ReportTypeDiscipline> 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
};
}
}

View File

@ -19,5 +19,33 @@ namespace UniversityDatabaseImplement.Models
public virtual List<Activity> Activities { get; set; } = new();
[ForeignKey("ExaminationResultId")]
public virtual List<StudentExaminationResult> 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
};
}
}

View File

@ -15,5 +15,26 @@ namespace UniversityDatabaseImplement.Models
public virtual List<ReportTypeActivity> ReportTypeActivities { get; set; } = new();
[ForeignKey("ReportTypeId")]
public virtual List<ReportTypeDiscipline> 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
};
}
}

View File

@ -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();
}
}

View File

@ -17,5 +17,29 @@ namespace UniversityDatabaseImplement.Models
public virtual List<StatementStudent> StatementStudents { get; set; } = new();
[ForeignKey("StatementId")]
public virtual List<ExaminationResult> 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
};
}
}

View File

@ -17,5 +17,29 @@ namespace UniversityDatabaseImplement.Models
public virtual List<StatementStudent> StatementStudents { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<StudentExaminationResult> 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
};
}
}

View File

@ -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> Activity { set; get; }
public virtual DbSet<Discipline> Discipline { set; get; }
public virtual DbSet<ExaminationResult> ExaminationResult { set; get; }
public virtual DbSet<ReportType> ReportType { set; get; }
public virtual DbSet<ReportTypeActivity> ReportTypeActivity { set; get; }
public virtual DbSet<ReportTypeDiscipline> ReportTypeDiscipline { set; get; }
public virtual DbSet<Statement> Statement { set; get; }
public virtual DbSet<StatementStudent> StatementStudent { set; get; }
public virtual DbSet<Student> Student { set; get; }
public virtual DbSet<StudentExaminationResult> StudentExaminationResult { set; get; }
public virtual DbSet<Activity> Activities { set; get; }
public virtual DbSet<Discipline> Disciplines { set; get; }
public virtual DbSet<ExaminationResult> ExaminationResults { set; get; }
public virtual DbSet<ReportType> ReportTypes { set; get; }
public virtual DbSet<ReportTypeActivity> ReportTypeActivities { set; get; }
public virtual DbSet<ReportTypeDiscipline> ReportTypeDisciplines { set; get; }
public virtual DbSet<Statement> Statements { set; get; }
public virtual DbSet<StatementStudent> StatementStudents { set; get; }
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<StudentExaminationResult> StudentExaminationResults { set; get; }
}
}