Начало бд

This commit is contained in:
GokaPek 2024-05-14 20:26:30 +04:00
parent 01b2bc9960
commit 9f9e4e92f9
26 changed files with 967 additions and 17 deletions

View File

@ -10,13 +10,14 @@ namespace TaskTrackerContracts.BindingModels
public class DirectionBindingModel : IDirectionModel public class DirectionBindingModel : IDirectionModel
{ {
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, (IStudentModel, int)> DirectionStudents public string Name { get; set; } = string.Empty;
public Dictionary<int, IStudentModel> DirectionStudents
{ {
get; get;
set; set;
} = new(); } = new();
public Dictionary<int, (ISubjectModel, int)> DirectionSubjects public Dictionary<int, ISubjectModel> DirectionSubjects
{ {
get; get;
set; set;

View File

@ -9,5 +9,6 @@ namespace TaskTrackerContracts.SearchModels
public class DirectionSearchModel public class DirectionSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string Name { get; set; } = string.Empty;
} }
} }

View File

@ -9,5 +9,6 @@ namespace TaskTrackerContracts.SearchModels
public class ExamSearchModel public class ExamSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public int? SubjectId { get; set; }
} }
} }

View File

@ -9,5 +9,7 @@ namespace TaskTrackerContracts.SearchModels
public class ResultSearchModel public class ResultSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public int? SubjectId { get; set; }
public int? StudentId { get; set; }
} }
} }

View File

@ -9,5 +9,6 @@ namespace TaskTrackerContracts.SearchModels
public class StudentSearchModel public class StudentSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string Name { get; set; } = string.Empty;
} }
} }

View File

@ -9,5 +9,6 @@ namespace TaskTrackerContracts.SearchModels
public class SubjectSearchModel public class SubjectSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string Name { get; set; }
} }
} }

View File

@ -11,11 +11,11 @@ namespace TaskTrackerContracts.StoragesContracts
{ {
public interface IDirectionStorage public interface IDirectionStorage
{ {
List<DirectionBindingModel> GetFullList(); /*List<DirectionBindingModel> GetFullList();
List<DirectionBindingModel> GetFilteredList(DirectionSearchModel model); List<DirectionBindingModel> GetFilteredList(DirectionSearchModel model);
DirectionBindingModel? GetElement(DirectionSearchModel model); DirectionBindingModel? GetElement(DirectionSearchModel model);
DirectionBindingModel? Insert(DirectionBindingModel model); DirectionBindingModel? Insert(DirectionBindingModel model);
DirectionBindingModel? Update(DirectionBindingModel model); DirectionBindingModel? Update(DirectionBindingModel model);
DirectionBindingModel? Delete(DirectionBindingModel model); DirectionBindingModel? Delete(DirectionBindingModel model);*/
} }
} }

View File

@ -11,11 +11,11 @@ namespace TaskTrackerContracts.StoragesContracts
{ {
public interface IExamStorage public interface IExamStorage
{ {
List<ExamBindingModel> GetFullList(); /*List<ExamBindingModel> GetFullList();
List<ExamBindingModel> GetFilteredList(ExamSearchModel model); List<ExamBindingModel> GetFilteredList(ExamSearchModel model);
ExamBindingModel? GetElement(ExamSearchModel model); ExamBindingModel? GetElement(ExamSearchModel model);
ExamBindingModel? Insert(ExamBindingModel model); ExamBindingModel? Insert(ExamBindingModel model);
ExamBindingModel? Update(ExamBindingModel model); ExamBindingModel? Update(ExamBindingModel model);
ExamBindingModel? Delete(ExamBindingModel model); ExamBindingModel? Delete(ExamBindingModel model);*/
} }
} }

View File

@ -11,11 +11,11 @@ namespace TaskTrackerContracts.StoragesContracts
{ {
public interface IResultStorage public interface IResultStorage
{ {
List<ResultBindingModel> GetFullList(); /*List<ResultBindingModel> GetFullList();
List<ResultBindingModel> GetFilteredList(ResultSearchModel model); List<ResultBindingModel> GetFilteredList(ResultSearchModel model);
ResultBindingModel? GetElement(ResultSearchModel model); ResultBindingModel? GetElement(ResultSearchModel model);
ResultBindingModel? Insert(ResultBindingModel model); ResultBindingModel? Insert(ResultBindingModel model);
ResultBindingModel? Update(ResultBindingModel model); ResultBindingModel? Update(ResultBindingModel model);
ResultBindingModel? Delete(ResultBindingModel model); ResultBindingModel? Delete(ResultBindingModel model);*/
} }
} }

View File

@ -11,11 +11,11 @@ namespace TaskTrackerContracts.StoragesContracts
{ {
public interface IStudentStorage public interface IStudentStorage
{ {
List<StudentBindingModel> GetFullList(); /* List<StudentBindingModel> GetFullList();
List<StudentBindingModel> GetFilteredList(StudentSearchModel model); List<StudentBindingModel> GetFilteredList(StudentSearchModel model);
StudentBindingModel? GetElement(StudentSearchModel model); StudentBindingModel? GetElement(StudentSearchModel model);
StudentBindingModel? Insert(StudentBindingModel model); StudentBindingModel? Insert(StudentBindingModel model);
StudentBindingModel? Update(StudentBindingModel model); StudentBindingModel? Update(StudentBindingModel model);
StudentBindingModel? Delete(StudentBindingModel model); StudentBindingModel? Delete(StudentBindingModel model);*/
} }
} }

View File

@ -11,11 +11,11 @@ namespace TaskTrackerContracts.StoragesContracts
{ {
public interface ISubjectStorage public interface ISubjectStorage
{ {
List<SubjectBindingModel> GetFullList(); /*List<SubjectBindingModel> GetFullList();
List<SubjectBindingModel> GetFilteredList(SubjectSearchModel model); List<SubjectBindingModel> GetFilteredList(SubjectSearchModel model);
SubjectBindingModel? GetElement(SubjectSearchModel model); SubjectBindingModel? GetElement(SubjectSearchModel model);
SubjectBindingModel? Insert(SubjectBindingModel model); SubjectBindingModel? Insert(SubjectBindingModel model);
SubjectBindingModel? Update(SubjectBindingModel model); SubjectBindingModel? Update(SubjectBindingModel model);
SubjectBindingModel? Delete(SubjectBindingModel model); SubjectBindingModel? Delete(SubjectBindingModel model);*/
} }
} }

View File

@ -10,13 +10,14 @@ namespace TaskTrackerContracts.ViewModels
public class DirectionViewModel : IDirectionModel public class DirectionViewModel : IDirectionModel
{ {
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, (IStudentModel, int)> DirectionStudents public string Name { get; set; } = string.Empty;
public Dictionary<int, IStudentModel> DirectionStudents
{ {
get; get;
set; set;
} = new(); } = new();
public Dictionary<int, (ISubjectModel, int)> DirectionSubjects public Dictionary<int, ISubjectModel> DirectionSubjects
{ {
get; get;
set; set;

View File

@ -8,7 +8,8 @@ namespace TaskTrackerDataModels.Models
{ {
public interface IDirectionModel : IId public interface IDirectionModel : IId
{ {
Dictionary<int, (IStudentModel, int)> DirectionStudents { get; } string Name { get; }
Dictionary<int, (ISubjectModel, int)> DirectionSubjects { get; } Dictionary<int, IStudentModel> DirectionStudents { get; }
Dictionary<int, ISubjectModel> DirectionSubjects { get; }
} }
} }

View File

@ -0,0 +1,114 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDatabase.Models;
namespace TaskTrackerDatabase.Implements
{
public class DirectionStorage : IDirectionStorage
{
public List<DirectionViewModel> GetFullList()
{
using var context = new TaskTrackerDatabase();
return context.Directions
.Include(x => x.Students)
.ThenInclude(x => x.Student)
//.ToList()
.Include(x => x.Subjects)
.ThenInclude(x => x.Subject)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<DirectionViewModel> GetFilteredList(DirectionSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new TaskTrackerDatabase();
return context.Directions
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.Include(x => x.Subjects)
.ThenInclude(x => x.Subject)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public DirectionViewModel? GetElement(DirectionSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new TaskTrackerDatabase();
return context.Directions
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.Include(x => x.Subjects)
.ThenInclude(x => x.Subject)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public DirectionViewModel? Insert(DirectionBindingModel model)
{
using var context = new TaskTrackerDatabase();
var newReinforced = Direction.Create(context, model);
if (newReinforced == null)
{
return null;
}
context.Directions.Add(newReinforced);
context.SaveChanges();
return newReinforced.GetViewModel;
}
public DirectionViewModel? Update(DirectionBindingModel model)
{
using var context = new TaskTrackerDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var reinforced = context.Directions.FirstOrDefault(rec => rec.Id == model.Id);
if (reinforced == null)
{
return null;
}
reinforced.Update(model);
context.SaveChanges();
reinforced.UpdateStudents(context, model);
reinforced.UpdateSubjects(context, model);
transaction.Commit();
return reinforced.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DirectionViewModel? Delete(DirectionBindingModel model)
{
using var context = new TaskTrackerDatabase();
var element = context.Directions
.Include(x => x.Students)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Directions.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,110 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDatabase.Models;
namespace TaskTrackerDatabase.Implements
{
public class ExamStorage : IExamStorage
{
public List<ExamViewModel> GetFullList()
{
using var context = new TaskTrackerDatabase();
return context.Exams
.Include(x => x.Subject)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ExamViewModel> GetFilteredList(ExamSearchModel model)
{
using var context = new TaskTrackerDatabase();
if (model.Id.HasValue)
{
return context.Exams
.Include(x => x.Subject)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (!model.SubjectId.HasValue)
{
return context.Exams
.Include(x => x.Subject)
.Where(x => x.SubjectId == model.SubjectId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public ExamViewModel? GetElement(ExamSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new TaskTrackerDatabase();
return context.Exams
.Include(x => x.Subject)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ExamViewModel? Insert(ExamBindingModel model)
{
var newExam = Exam.Create(model);
if (newExam == null)
{
return null;
}
using var context = new TaskTrackerDatabase();
context.Exams.Add(newExam);
context.SaveChanges();
return context.Exams
.Include(x => x.Subject)
.FirstOrDefault(x => x.Id == newExam.Id)
?.GetViewModel;
}
public ExamViewModel? Update(ExamBindingModel model)
{
using var context = new TaskTrackerDatabase();
var order = context.Exams.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Exams
.Include(x => x.Subject)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ExamViewModel? Delete(ExamBindingModel model)
{
using var context = new TaskTrackerDatabase();
var element = context.Exams.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Exams
.Include(x => x.Subject)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Exams.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
}
}

View File

@ -0,0 +1,115 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDatabase.Models;
namespace TaskTrackerDatabase.Implements
{
public class ResultStorage : IResultStorage
{
public List<ResultViewModel> GetFullList()
{
using var context = new TaskTrackerDatabase();
return context.Results
.Include(x => x.Subject)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ResultViewModel> GetFilteredList(ResultSearchModel model)
{
using var context = new TaskTrackerDatabase();
if (model.Id.HasValue)
{
return context.Results
.Include(x => x.Subject)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (!model.SubjectId.HasValue)
{
return context.Results
.Include(x => x.Subject)
.Include(x => x.Student)
.Where(x => x.SubjectId == model.SubjectId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public ResultViewModel? GetElement(ResultSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new TaskTrackerDatabase();
return context.Results
.Include(x => x.Subject)
.Include(x => x.Student)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ResultViewModel? Insert(ResultBindingModel model)
{
var newResult = Result.Create(model);
if (newResult == null)
{
return null;
}
using var context = new TaskTrackerDatabase();
context.Results.Add(newResult);
context.SaveChanges();
return context.Results
.Include(x => x.Subject)
.Include(x => x.Student)
.FirstOrDefault(x => x.Id == newResult.Id)
?.GetViewModel;
}
public ResultViewModel? Update(ResultBindingModel model)
{
using var context = new TaskTrackerDatabase();
var order = context.Results.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Results
.Include(x => x.Subject)
.Include(x => x.Student)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ResultViewModel? Delete(ResultBindingModel model)
{
using var context = new TaskTrackerDatabase();
var element = context.Results.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Results
.Include(x => x.Subject)
.Include(x => x.Student)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Results.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDatabase.Models;
namespace TaskTrackerDatabase.Implements
{
public class StudentStorage : IStudentStorage
{
public List<StudentViewModel> GetFullList()
{
using var context = new TaskTrackerDatabase();
return context.Students
.Select(x => x.GetViewModel)
.ToList();
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new TaskTrackerDatabase();
return context.Students
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public StudentViewModel? GetElement(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new TaskTrackerDatabase();
return context.Students.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name || (model.Id.HasValue && x.Id == model.Id)))?.GetViewModel;
}
public StudentViewModel? Insert(StudentBindingModel model)
{
var newStudent = Student.Create(model);
if (newStudent == null)
{
return null;
}
using var context = new TaskTrackerDatabase();
context.Students.Add(newStudent);
context.SaveChanges();
return newStudent.GetViewModel;
}
public StudentViewModel? Update(StudentBindingModel model)
{
using var context = new TaskTrackerDatabase();
var user = context.Students.FirstOrDefault(x => x.Id == model.Id);
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetViewModel;
}
public StudentViewModel? Delete(StudentBindingModel model)
{
using var context = new TaskTrackerDatabase();
var element = context.Students.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Students.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDatabase.Models;
namespace TaskTrackerDatabase.Implements
{
public class SubjectStorage : ISubjectStorage
{
public List<SubjectViewModel> GetFullList()
{
using var context = new TaskTrackerDatabase();
return context.Subjects
.Select(x => x.GetViewModel)
.ToList();
}
public List<SubjectViewModel> GetFilteredList(SubjectSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new TaskTrackerDatabase();
return context.Subjects
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public SubjectViewModel? GetElement(SubjectSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new TaskTrackerDatabase();
return context.Subjects.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name || (model.Id.HasValue && x.Id == model.Id)))?.GetViewModel;
}
public SubjectViewModel? Insert(SubjectBindingModel model)
{
var newSubject = Subject.Create(model);
if (newSubject == null)
{
return null;
}
using var context = new TaskTrackerDatabase();
context.Subjects.Add(newSubject);
context.SaveChanges();
return newSubject.GetViewModel;
}
public SubjectViewModel? Update(SubjectBindingModel model)
{
using var context = new TaskTrackerDatabase();
var user = context.Subjects.FirstOrDefault(x => x.Id == model.Id);
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetViewModel;
}
public SubjectViewModel? Delete(SubjectBindingModel model)
{
using var context = new TaskTrackerDatabase();
var element = context.Subjects.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Subjects.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Models;
namespace TaskTrackerDatabase.Models
{
public class Direction : IDirectionModel
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
private Dictionary<int, IStudentModel>? _directionStudents = null;
[NotMapped]
public Dictionary<int, IStudentModel> DirectionStudents
{
get
{
if (_directionStudents == null)
{
_directionStudents = Students
.ToDictionary(recPC => recPC.StudentId, recPC =>
(recPC.Student as IStudentModel));
}
return _directionStudents;
}
}
[ForeignKey("DirectionId")]
public virtual List<DirectionStudent> Students { get; set; } = new();
private Dictionary<int, ISubjectModel>? _directionSubjects = null;
[NotMapped]
public Dictionary<int, ISubjectModel> DirectionSubjects
{
get
{
if (_directionSubjects == null)
{
_directionSubjects = Subjects
.ToDictionary(recPC => recPC.SubjectId, recPC =>
(recPC.Subject as ISubjectModel));
}
return _directionSubjects;
}
}
[ForeignKey("DirectionId")]
public virtual List<DirectionSubject> Subjects { get; set; } = new();
public static Direction Create(TaskTrackerDatabase context, DirectionBindingModel model)
{
return new Direction()
{
Id = model.Id,
Name = model.Name,
Students = model.DirectionStudents.Select(x => new DirectionStudent
{
Student = context.Students.First(y => y.Id == x.Key)
}).ToList(),
Subjects = model.DirectionSubjects.Select(x => new DirectionSubject
{
Subject = context.Subjects.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(DirectionBindingModel model)
{
Name = model.Name;
}
public DirectionViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
DirectionStudents = DirectionStudents,
DirectionSubjects = DirectionSubjects
};
public void UpdateStudents(TaskTrackerDatabase context, DirectionBindingModel model)
{
var directionStudents = context.DirectionStudents.Where(rec => rec.DirectionId == model.Id).ToList();
if (directionStudents != null && DirectionStudents.Count > 0)
{ // удалили те, которых нет в модели
context.DirectionStudents.RemoveRange(directionStudents.Where(rec => !model.DirectionStudents.ContainsKey(rec.StudentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateStudents in directionStudents)
{
model.DirectionStudents.Remove(updateStudents.StudentId);
}
context.SaveChanges();
}
var reinforced = context.Directions.First(x => x.Id == Id);
foreach (var rp in model.DirectionStudents)
{
context.DirectionStudents.Add(new DirectionStudent
{
Direction = reinforced,
Student = context.Students.First(x => x.Id == rp.Key)
});
context.SaveChanges();
}
_directionStudents = null;
}
public void UpdateSubjects(TaskTrackerDatabase context, DirectionBindingModel model)
{
var directionSubjects = context.DirectionSubjects.Where(rec => rec.DirectionId == model.Id).ToList();
if (directionSubjects != null && DirectionSubjects.Count > 0)
{ // удалили те, которых нет в модели
context.DirectionSubjects.RemoveRange(directionSubjects.Where(rec => !model.DirectionSubjects.ContainsKey(rec.SubjectId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateSubjects in directionSubjects)
{
model.DirectionSubjects.Remove(updateSubjects.SubjectId);
}
context.SaveChanges();
}
var reinforced = context.Directions.First(x => x.Id == Id);
foreach (var rp in model.DirectionSubjects)
{
context.DirectionSubjects.Add(new DirectionSubject
{
Direction = reinforced,
Subject = context.Subjects.First(x => x.Id == rp.Key)
});
context.SaveChanges();
}
_directionSubjects = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaskTrackerDatabase.Models
{
public class DirectionSubject
{
public int Id { get; set; }
[Required]
public int DirectionId { get; set; }
[Required]
public int SubjectId { get; set; }
[Required]
public virtual Direction Direction{ get; set; } = new();
public virtual Subject Subject { get; set; } = new();
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaskTrackerDatabase.Models
{
public class DirectionStudent
{
public int Id { get; set; }
[Required]
public int DirectionId { get; set; }
[Required]
public int StudentId { get; set; }
[Required]
public virtual Direction Direction{ get; set; } = new();
public virtual Student Student { get; set; } = new();
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Models;
namespace TaskTrackerDatabase.Models
{
public class Exam : IExamModel
{
public int Id { get; set; }
[Required]
public int SubjectId { get; set; }
[Required]
public DateTime Date { get; set; }
public virtual Subject Subject { get; set; }
public static Exam? Create(ExamBindingModel? model)
{
if (model == null)
{
return null;
}
return new Exam()
{
Id = model.Id,
SubjectId = model.SubjectId,
Date = model.Date,
};
}
public void Update(ExamBindingModel? model)
{
if (model == null)
{
return;
}
Date = model.Date;
}
public ExamViewModel GetViewModel => new()
{
Id = Id,
SubjectId = SubjectId,
Date = Date,
};
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Models;
namespace TaskTrackerDatabase.Models
{
public class Result : IResultModel
{
public int Id { get; set; }
[Required]
public string Grade { get; set; } = string.Empty;
[Required]
public int StudentId { get; set; }
[Required]
public int SubjectId { get; set; }
public Student Student { get; set; }
public Subject Subject { get; set; }
public static Result? Create(ResultBindingModel? model)
{
if (model == null)
{
return null;
}
return new Result()
{
Id = model.Id,
Grade = model.Grade,
StudentId = model.StudentId,
SubjectId = model.SubjectId,
};
}
public void Update(ResultBindingModel? model)
{
if (model == null)
{
return;
}
Grade = model.Grade;
StudentId = model.StudentId;
SubjectId = model.SubjectId;
}
public ResultViewModel GetViewModel => new()
{
Id = Id,
Grade = Grade,
StudentId = StudentId,
SubjectId = SubjectId,
};
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicsContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Models;
namespace TaskTrackerDatabase.Models
{
public class Student : IStudentModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime DateBirth { get; set; }
[ForeignKey("StudentId")]
public virtual List<Result> Results { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Exam> Exams { get; set; } = new();
public static Student? Create(StudentBindingModel? model)
{
if (model == null)
{
return null;
}
return new Student()
{
Id = model.Id,
Name = model.Name,
DateBirth = model.DateBirth,
};
}
public void Update(StudentBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
DateBirth = model.DateBirth;
}
public StudentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
DateBirth = DateBirth,
};
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Models;
namespace TaskTrackerDatabase.Models
{
public class Subject : ISubjectModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[ForeignKey("SubjectId")]
public virtual List<Exam> Exams { get; set; } = new();
[ForeignKey("SubjectId")]
public virtual List<Result> Results { get; set; } = new();
public static Subject? Create(SubjectBindingModel? model)
{
if (model == null)
{
return null;
}
return new Subject()
{
Id = model.Id,
Name = model.Name,
};
}
public void Update(SubjectBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
public SubjectViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
};
}
}

View File

@ -19,5 +19,14 @@ namespace TaskTrackerDatabase
public virtual DbSet<OrganizationProject> OrganizationProjects { set; get; } public virtual DbSet<OrganizationProject> OrganizationProjects { set; get; }
public virtual DbSet<Task> Tasks { set; get; } public virtual DbSet<Task> Tasks { set; get; }
public virtual DbSet<User> Users { set; get; } public virtual DbSet<User> Users { set; get; }
public virtual DbSet<Direction> Directions { set; get; }
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<Subject> Subjects { set; get; }
public virtual DbSet<DirectionStudent> DirectionStudents { set; get; }
public virtual DbSet<DirectionSubject> DirectionSubjects { set; get; }
public virtual DbSet<Result> Results { set; get; }
public virtual DbSet<Exam> Exams { set; get; }
} }
} }