vreksam/BL/Services/FoorumService.cs

145 lines
4.3 KiB
C#

using BL.DTOs;
using BL.Factories;
using BL.Interfaces;
using DAL.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using DAL.Domain;
namespace BL.Services
{
public class FoorumService : IFoorumService
{
private readonly IFoorumRepository _foorumRepository;
private readonly IPostRepository _postRepository;
private readonly FoorumFactory _factory;
private readonly PostFactory _postFactory;
public FoorumService(IFoorumRepository foorumRepository, IPostRepository postRepository)
{
_foorumRepository = foorumRepository;
_postRepository = postRepository;
_factory = new FoorumFactory(this);
_postFactory = new PostFactory(this);
}
public FoorumDTO Add(FoorumDTO foorum)
{
var foorumDomain = _factory.Create(foorum);
foorumDomain.CreationTime = DateTime.Now;
var f = _foorumRepository.Add(foorumDomain);
return _factory.Create(f);
}
public PostDTO AddPost(int id, PostDTO post)
{
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);
}
public FoorumDTO Delete(int id)
{
var f = _foorumRepository.GetById(id);
if (f == null)
{
return null;
}
foreach (var post in f.Posts.ToArray())
{
_postRepository.Delete(post);
}
_postRepository.SaveChanges();
_foorumRepository.Delete(f);
_foorumRepository.SaveChanges();
return _factory.Create(f);
}
public FoorumDTO Get(int id, bool withPosts=true)
{
var f =_foorumRepository.GetById(id);
if (f == null)
{
return null;
}
return _factory.Create(f, withPosts);
}
public IEnumerable<FoorumDTO> GetAll(bool withHidden = false, bool withPosts = false)
{
IEnumerable<Foorum> results;
if (withHidden)
{
results = _foorumRepository.GetAll();
}
else
{
results = _foorumRepository.GetMany(f => f.Visible);
}
foreach (var f in results)
{
yield return _factory.Create(f, withPosts);
}
}
public FoorumDTO Hide(int id, bool hide=true)
{
var f = _foorumRepository.GetById(id);
if (f == null)
{
return null;
}
f.Visible = !hide;
_foorumRepository.Update(f);
_foorumRepository.SaveChanges();
return _factory.Create(f);
}
public IEnumerable<FoorumDTO> SearchFoorumAuthor(string query)
{
foreach (var f in _foorumRepository.GetMany(f => f.Author.ToLower().Contains(query.ToLower())))
{
yield return _factory.Create(f);
}
}
public IEnumerable<FoorumDTO> SearchFoorumTitle(string query)
{
foreach (var f in _foorumRepository.GetMany(f => f.Title.ToLower().Contains(query.ToLower())).OrderBy(f => f.CreationTime))
{
yield return _factory.Create(f);
}
}
public IEnumerable<PostDTO> SearchPostAuthor(string query)
{
foreach (var post in _postRepository.GetMany(p => p.Author.ToLower().Contains(query.ToLower())))
{
yield return _postFactory.Create(post, withForum: true);
}
}
public FoorumDTO Update(FoorumDTO foorum)
{
var domain = _foorumRepository.GetById(foorum.FoorumId);
domain = _factory.Update(domain, foorum);
_foorumRepository.Update(domain);
_foorumRepository.SaveChanges();
return _factory.Create(domain);
}
}
}