Переделал implements чутка

This commit is contained in:
Kirill 2024-05-02 20:14:11 +04:00
parent 8292e1a4ae
commit 7e1670bf0c
5 changed files with 277 additions and 6 deletions

View File

@ -0,0 +1,111 @@
using Microsoft.EntityFrameworkCore;
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class ClubStorage : IClubStorage
{
public List<ClubViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Clubs
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClubViewModel> GetFilteredList(ClubSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDatabase();
return context.Clubs
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ClubViewModel? GetElement(ClubSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) &&
!model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Clubs
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) &&
x.Name == model.Name) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public ClubViewModel? Insert(ClubBindingModel model)
{
using var context = new SchoolDatabase();
var newComputer = Club.Create(context, model);
if (newComputer == null)
{
return null;
}
context.Clubs.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
}
public ClubViewModel? Update(ClubBindingModel model)
{
using var context = new SchoolDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Computer = context.Clubs.FirstOrDefault(rec =>
rec.Id == model.Id);
if (Computer == null)
{
return null;
}
Computer.Update(model);
context.SaveChanges();
Computer.UpdateLessons(context, model);
transaction.Commit();
return Computer.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ClubViewModel? Delete(ClubBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Clubs
.Include(x => x.Lessons)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clubs.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -20,6 +20,8 @@ namespace SchoolDatabaseImplement.Implements
return context.Interests
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
@ -34,6 +36,8 @@ namespace SchoolDatabaseImplement.Implements
return context.Interests
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
@ -50,6 +54,8 @@ namespace SchoolDatabaseImplement.Implements
return context.Interests
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.Include(x => x.Materials)
.ThenInclude(x => x.Material)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) &&
x.Name == model.Name) ||
(model.Id.HasValue && x.Id ==
@ -83,6 +89,7 @@ namespace SchoolDatabaseImplement.Implements
Computer.Update(model);
context.SaveChanges();
Computer.UpdateLessons(context, model);
Computer.UpdateMaterials(context, model);
transaction.Commit();
return Computer.GetViewModel;
}
@ -97,6 +104,7 @@ namespace SchoolDatabaseImplement.Implements
using var context = new SchoolDatabase();
var element = context.Interests
.Include(x => x.Lessons)
.Include(x => x.Materials)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{

View File

@ -0,0 +1,90 @@
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class MaterialStorage : IMaterialStorage
{
public List<MaterialViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Materials
.Select(x => x.GetViewModel)
.ToList();
}
public List<MaterialViewModel> GetFilteredList(MaterialSearchModel
model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDatabase();
return context.Materials
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public MaterialViewModel? GetElement(MaterialSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Materials
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name ==
model.Name) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MaterialViewModel? Insert(MaterialBindingModel model)
{
var newMaterial = Material.Create(model);
if (newMaterial == null)
{
return null;
}
using var context = new SchoolDatabase();
context.Materials.Add(newMaterial);
context.SaveChanges();
return newMaterial.GetViewModel;
}
public MaterialViewModel? Update(MaterialBindingModel model)
{
using var context = new SchoolDatabase();
var Material = context.Materials.FirstOrDefault(x => x.Id ==
model.Id);
if (Material == null)
{
return null;
}
Material.Update(model);
context.SaveChanges();
return Material.GetViewModel;
}
public MaterialViewModel? Delete(MaterialBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Materials.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Materials.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -32,16 +32,16 @@ namespace SchoolDatabaseImplement.Models
}
[ForeignKey("ClubId")]
public virtual List<ClubLesson> Lessons { get; set; } = new();
public static Club? Create(ClubBindingModel model)
public static Club Create(SchoolDatabase context, ClubBindingModel model)
{
if (model == null)
{
return null;
}
return new Club()
{
Id = model.Id,
Name = model.Name
Name = model.Name,
Lessons = model.ClubLessons.Select(x => new ClubLesson
{
Lesson = context.Lessons.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(ClubBindingModel model)
@ -57,5 +57,34 @@ namespace SchoolDatabaseImplement.Models
Id = Id,
Name = Name
};
public void UpdateLessons(SchoolDatabase context,
ClubBindingModel model)
{
var ClubLessons = context.ClubLessons.Where(rec => rec.ClubId == model.Id).ToList();
if (ClubLessons != null)
{ // удалили те, которых нет в модели
context.ClubLessons.RemoveRange(ClubLessons.Where(rec
=> !model.ClubLessons.ContainsKey(rec.LessonId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateLesson in ClubLessons)
{
model.ClubLessons.Remove(updateLesson.LessonId);
}
context.SaveChanges();
}
var Club = context.Clubs.First(x => x.Id == Id);
foreach (var pc in model.ClubLessons)
{
context.ClubLessons.Add(new ClubLesson
{
Club = Club,
Lesson = context.Lessons.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_ClubLessons = null;
}
}
}

View File

@ -66,6 +66,10 @@ namespace SchoolDatabaseImplement.Models
Lessons = model.InterestLessons.Select(x => new InterestLesson
{
Lesson = context.Lessons.First(y => y.Id == x.Key)
}).ToList(),
Materials = model.InterestMaterials.Select(x => new InterestMaterial
{
Material = context.Materials.First(y => y.Id == x.Key)
}).ToList()
};
}
@ -114,5 +118,34 @@ namespace SchoolDatabaseImplement.Models
}
_InterestLessons = null;
}
public void UpdateMaterials(SchoolDatabase context,
InterestBindingModel model)
{
var InterestMaterials = context.InterestMaterials.Where(rec => rec.InterestId == model.Id).ToList();
if (InterestMaterials != null)
{ // удалили те, которых нет в модели
context.InterestMaterials.RemoveRange(InterestMaterials.Where(rec
=> !model.InterestMaterials.ContainsKey(rec.MaterialId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateMaterial in InterestMaterials)
{
model.InterestMaterials.Remove(updateMaterial.MaterialId);
}
context.SaveChanges();
}
var Interest = context.Interests.First(x => x.Id == Id);
foreach (var pc in model.InterestMaterials)
{
context.InterestMaterials.Add(new InterestMaterial
{
Interest = Interest,
Material = context.Materials.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_InterestMaterials = null;
}
}
}