149 lines
4.3 KiB
C#
149 lines
4.3 KiB
C#
using ForumContracts.BindingModels;
|
|
using ForumContracts.SearchModels;
|
|
using ForumContracts.StorageContracts;
|
|
using ForumContracts.ViewModels;
|
|
using ForumDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ForumDatabaseImplement.Implements
|
|
{
|
|
public class QuestionStorage : IQuestionStorage
|
|
{
|
|
public QuestionViewModel? Delete(QuestionBindingModel model)
|
|
{
|
|
using var context = new ForumDataBase();
|
|
|
|
var element = context.Questions
|
|
.Include(x => x.User)
|
|
.Include(x => x.Category)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
context.Questions.Remove(element);
|
|
context.SaveChanges();
|
|
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public QuestionViewModel? GetElement(QuestionSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new ForumDataBase();
|
|
|
|
return context.Questions
|
|
.Include(x => x.User)
|
|
.Include(x => x.Category)
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<QuestionViewModel> GetFilteredList(QuestionSearchModel model)
|
|
{
|
|
if (model.Id.HasValue)
|
|
{
|
|
var result = GetElement(model);
|
|
return result != null ? new() { result } : new();
|
|
}
|
|
|
|
using var context = new ForumDataBase();
|
|
IQueryable<Question>? queryWhere = null;
|
|
|
|
if (model.CategoryId.HasValue)
|
|
{
|
|
queryWhere = context.Questions.Where(x => x.CategoryId == model.CategoryId);
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
queryWhere = context.Questions.Where(x => x.UserId == model.UserId);
|
|
}
|
|
|
|
else
|
|
{
|
|
return new();
|
|
}
|
|
|
|
return queryWhere
|
|
.Include(x => x.Category)
|
|
.Include(x => x.User)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<QuestionViewModel> HardRequest(QuestionSearchModel model)
|
|
{
|
|
using var context = new ForumDataBase();
|
|
return context.Questions.
|
|
OrderBy(x => x.CreateDate)
|
|
.Include(x => x.Category)
|
|
.Include(x => x.User)
|
|
.Where(x => x.CreateDate.Date >= model.DateFrom.Value.Date && x.CreateDate.Date <= model.DateTo.Value.Date)
|
|
.Select(x=>x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<QuestionViewModel> GetFullList()
|
|
{
|
|
using var context = new ForumDataBase();
|
|
|
|
return context.Questions
|
|
.Include(x => x.User)
|
|
.Include(x => x.Category)
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public QuestionViewModel? Insert(QuestionBindingModel model)
|
|
{
|
|
var newQuestion = Question.Create(model);
|
|
|
|
if (newQuestion == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new ForumDataBase();
|
|
|
|
context.Questions.Add(newQuestion);
|
|
context.SaveChanges();
|
|
|
|
return context.Questions
|
|
.Include(x => x.Category)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == newQuestion.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public QuestionViewModel? Update(QuestionBindingModel model)
|
|
{
|
|
using var context = new ForumDataBase();
|
|
|
|
var question = context.Questions
|
|
.Include(x => x.Category)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (question == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
question.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return question.GetViewModel;
|
|
}
|
|
}
|
|
}
|