2023-04-05 22:23:31 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-05-17 15:31:34 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue && !model.StudentId.HasValue)
|
2023-04-05 22:23:31 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SchoolDataBase();
|
2023-05-17 15:31:34 +04:00
|
|
|
|
if(!string.IsNullOrEmpty(model.Title) && model.StudentId.HasValue)
|
2023-04-05 22:23:31 +04:00
|
|
|
|
return context.Interests
|
2023-05-17 15:31:34 +04:00
|
|
|
|
.FirstOrDefault(x => x.Title.Equals(model.Title)
|
|
|
|
|
&& x.StudentId==model.StudentId)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
return context.Interests
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
2023-04-05 22:23:31 +04:00
|
|
|
|
?.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|