CourseWork_SchoolStudyAgain/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Implements/InterestStorage.cs

91 lines
3.0 KiB
C#
Raw Normal View History

using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Implements
{
public class InterestStorage : IInterestStorage
{
public List<InterestViewModel> GetFullList()
{
using var context = new SchoolDataBase();
return context.Interests
.Select(x => x.GetViewModel)
.ToList();
}
public List<InterestViewModel> GetFilteredList(InterestSearchModel model)
{
if (!model.StudentId.HasValue)
{
return new();
}
using var context = new SchoolDataBase();
return context.Interests
.Where(x => x.StudentId == model.StudentId)
.Select(x => x.GetViewModel)
.ToList();
}
public InterestViewModel? GetElement(InterestSearchModel model)
{
if ((string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) || !model.StudentId.HasValue)
{
return null;
}
using var context = new SchoolDataBase();
return context.Interests
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
(model.Id.HasValue && x.Id == model.Id)) && x.StudentId==model.StudentId)
?.GetViewModel;
}
public InterestViewModel? Insert(InterestBindingModel model)
{
var newInterest = Interest.Create(model);
if (newInterest == null)
{
return null;
}
using var context = new SchoolDataBase();
context.Interests.Add(newInterest);
context.SaveChanges();
return newInterest.GetViewModel;
}
public InterestViewModel? Update(InterestBindingModel model)
{
using var context = new SchoolDataBase();
var interest = context.Interests.FirstOrDefault(x => x.Id == model.Id);
if (interest == null)
{
return null;
}
interest.Update(model);
context.SaveChanges();
return interest.GetViewModel;
}
public InterestViewModel? Delete(InterestBindingModel model)
{
using var context = new SchoolDataBase();
var element = context.Interests.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null )
{
context.Interests.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}