Implements для 4ех сущностей закончил

This commit is contained in:
Kirill 2024-05-01 01:14:47 +04:00
parent 78516eef25
commit 9a74447419
2 changed files with 201 additions and 0 deletions

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 AchievementStorage : IAchievementStorage
{
public List<AchievementViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Achievements
.Select(x => x.GetViewModel)
.ToList();
}
public List<AchievementViewModel> GetFilteredList(AchievementSearchModel
model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDatabase();
return context.Achievements
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public AchievementViewModel? GetElement(AchievementSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Achievements
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name ==
model.Name) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public AchievementViewModel? Insert(AchievementBindingModel model)
{
var newAchievement = Achievement.Create(model);
if (newAchievement == null)
{
return null;
}
using var context = new SchoolDatabase();
context.Achievements.Add(newAchievement);
context.SaveChanges();
return newAchievement.GetViewModel;
}
public AchievementViewModel? Update(AchievementBindingModel model)
{
using var context = new SchoolDatabase();
var Achievement = context.Achievements.FirstOrDefault(x => x.Id ==
model.Id);
if (Achievement == null)
{
return null;
}
Achievement.Update(model);
context.SaveChanges();
return Achievement.GetViewModel;
}
public AchievementViewModel? Delete(AchievementBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Achievements.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Achievements.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

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 InterestStorage : IInterestStorage
{
public List<InterestViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Interests
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<InterestViewModel> GetFilteredList(InterestSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDatabase();
return context.Interests
.Include(x => x.Lessons)
.ThenInclude(x => x.Lesson)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public InterestViewModel? GetElement(InterestSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) &&
!model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Interests
.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 InterestViewModel? Insert(InterestBindingModel model)
{
using var context = new SchoolDatabase();
var newComputer = Interest.Create(context, model);
if (newComputer == null)
{
return null;
}
context.Interests.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
}
public InterestViewModel? Update(InterestBindingModel model)
{
using var context = new SchoolDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Computer = context.Interests.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 InterestViewModel? Delete(InterestBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Interests
.Include(x => x.Lessons)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Interests.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}