2017-06-12 17:22:58 +03:00
|
|
|
|
using BL.DTOs;
|
2017-06-12 14:28:07 +03:00
|
|
|
|
using BL.Factories;
|
2017-06-12 13:37:48 +03:00
|
|
|
|
using BL.Interfaces;
|
|
|
|
|
using DAL.Interfaces;
|
2017-06-12 17:22:58 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using DAL.Domain;
|
2017-06-12 13:37:48 +03:00
|
|
|
|
|
|
|
|
|
namespace BL.Services
|
|
|
|
|
{
|
|
|
|
|
public class FoorumService : IFoorumService
|
|
|
|
|
{
|
2017-06-12 14:28:07 +03:00
|
|
|
|
private readonly IFoorumRepository _foorumRepository;
|
2017-06-12 17:22:58 +03:00
|
|
|
|
private readonly IPostRepository _postRepository;
|
2017-06-12 14:28:07 +03:00
|
|
|
|
private readonly FoorumFactory _factory;
|
2017-06-12 17:22:58 +03:00
|
|
|
|
private readonly PostFactory _postFactory;
|
|
|
|
|
public FoorumService(IFoorumRepository foorumRepository, IPostRepository postRepository)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
|
|
|
|
_foorumRepository = foorumRepository;
|
2017-06-12 17:22:58 +03:00
|
|
|
|
_postRepository = postRepository;
|
|
|
|
|
_factory = new FoorumFactory(this);
|
|
|
|
|
_postFactory = new PostFactory(this);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
2017-06-12 14:16:38 +03:00
|
|
|
|
public FoorumDTO Add(FoorumDTO foorum)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 14:28:07 +03:00
|
|
|
|
var foorumDomain = _factory.Create(foorum);
|
|
|
|
|
foorumDomain.CreationTime = DateTime.Now;
|
2017-06-12 14:16:38 +03:00
|
|
|
|
var f = _foorumRepository.Add(foorumDomain);
|
2017-06-12 14:28:07 +03:00
|
|
|
|
return _factory.Create(f);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 17:22:58 +03:00
|
|
|
|
public PostDTO AddPost(int id, PostDTO post)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 17:22:58 +03:00
|
|
|
|
var f =_foorumRepository.GetById(id);
|
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var domainPost = _postFactory.Create(post);
|
|
|
|
|
domainPost.CreationTime = DateTime.Now;
|
|
|
|
|
domainPost.ForumId = f.FoorumId;
|
|
|
|
|
f.Posts.Add(domainPost);
|
|
|
|
|
_postRepository.Add(domainPost);
|
|
|
|
|
return _postFactory.Create(domainPost);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 16:23:33 +03:00
|
|
|
|
public FoorumDTO Delete(int id)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 16:15:34 +03:00
|
|
|
|
var f = _foorumRepository.GetById(id);
|
2017-06-12 16:23:33 +03:00
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-06-12 17:22:58 +03:00
|
|
|
|
|
|
|
|
|
foreach (var post in f.Posts.ToArray())
|
|
|
|
|
{
|
|
|
|
|
_postRepository.Delete(post);
|
|
|
|
|
}
|
|
|
|
|
_postRepository.SaveChanges();
|
2017-06-12 16:15:34 +03:00
|
|
|
|
_foorumRepository.Delete(f);
|
|
|
|
|
_foorumRepository.SaveChanges();
|
2017-06-12 16:23:33 +03:00
|
|
|
|
|
|
|
|
|
return _factory.Create(f);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 17:22:58 +03:00
|
|
|
|
public FoorumDTO Get(int id, bool withPosts=true)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 14:43:31 +03:00
|
|
|
|
var f =_foorumRepository.GetById(id);
|
2017-06-12 17:22:58 +03:00
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _factory.Create(f, withPosts);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 17:22:58 +03:00
|
|
|
|
public IEnumerable<FoorumDTO> GetAll(bool withHidden = false, bool withPosts = false)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 17:22:58 +03:00
|
|
|
|
IEnumerable<Foorum> results;
|
|
|
|
|
if (withHidden)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 17:22:58 +03:00
|
|
|
|
results = _foorumRepository.GetAll();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
results = _foorumRepository.GetMany(f => f.Visible);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var f in results)
|
|
|
|
|
{
|
|
|
|
|
yield return _factory.Create(f, withPosts);
|
2017-06-12 16:23:33 +03:00
|
|
|
|
}
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 16:23:33 +03:00
|
|
|
|
public FoorumDTO Hide(int id, bool hide=true)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 16:15:34 +03:00
|
|
|
|
var f = _foorumRepository.GetById(id);
|
2017-06-12 16:23:33 +03:00
|
|
|
|
if (f == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 16:15:34 +03:00
|
|
|
|
f.Visible = !hide;
|
|
|
|
|
_foorumRepository.Update(f);
|
|
|
|
|
_foorumRepository.SaveChanges();
|
2017-06-12 16:23:33 +03:00
|
|
|
|
|
|
|
|
|
return _factory.Create(f);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<FoorumDTO> SearchFoorumAuthor(string query)
|
|
|
|
|
{
|
2017-06-12 15:26:34 +03:00
|
|
|
|
foreach (var f in _foorumRepository.GetMany(f => f.Author.ToLower().Contains(query.ToLower())))
|
|
|
|
|
{
|
|
|
|
|
yield return _factory.Create(f);
|
|
|
|
|
}
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<FoorumDTO> SearchFoorumTitle(string query)
|
|
|
|
|
{
|
2017-06-12 15:53:50 +03:00
|
|
|
|
foreach (var f in _foorumRepository.GetMany(f => f.Title.ToLower().Contains(query.ToLower())).OrderBy(f => f.CreationTime))
|
|
|
|
|
{
|
|
|
|
|
yield return _factory.Create(f);
|
|
|
|
|
}
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PostDTO> SearchPostAuthor(string query)
|
|
|
|
|
{
|
2017-06-12 17:22:58 +03:00
|
|
|
|
foreach (var post in _postRepository.GetMany(p => p.Author.ToLower().Contains(query.ToLower())))
|
|
|
|
|
{
|
|
|
|
|
yield return _postFactory.Create(post, withForum: true);
|
|
|
|
|
}
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 15:46:53 +03:00
|
|
|
|
public FoorumDTO Update(FoorumDTO foorum)
|
2017-06-12 13:37:48 +03:00
|
|
|
|
{
|
2017-06-12 15:46:53 +03:00
|
|
|
|
var domain = _foorumRepository.GetById(foorum.FoorumId);
|
|
|
|
|
domain = _factory.Update(domain, foorum);
|
|
|
|
|
_foorumRepository.Update(domain);
|
|
|
|
|
_foorumRepository.SaveChanges();
|
|
|
|
|
return _factory.Create(domain);
|
2017-06-12 13:37:48 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|