vreksam/BL/Services/FoorumService.cs

106 lines
3.0 KiB
C#
Raw Normal View History

2017-06-12 13:37:48 +03:00
using System;
using System.Collections.Generic;
2017-06-12 15:53:50 +03:00
using System.Linq;
2017-06-12 13:37:48 +03:00
using BL.DTOs;
using BL.Factories;
2017-06-12 13:37:48 +03:00
using BL.Interfaces;
using DAL.Interfaces;
namespace BL.Services
{
public class FoorumService : IFoorumService
{
private readonly IFoorumRepository _foorumRepository;
private readonly FoorumFactory _factory;
2017-06-12 13:37:48 +03:00
public FoorumService(IFoorumRepository foorumRepository)
{
_foorumRepository = foorumRepository;
_factory = new FoorumFactory();
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
{
var foorumDomain = _factory.Create(foorum);
foorumDomain.CreationTime = DateTime.Now;
2017-06-12 14:16:38 +03:00
var f = _foorumRepository.Add(foorumDomain);
return _factory.Create(f);
2017-06-12 13:37:48 +03:00
}
public void AddPost(int id, PostDTO post)
{
throw new NotImplementedException();
}
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);
if (f == null)
{
return null;
}
2017-06-12 16:15:34 +03:00
_foorumRepository.Delete(f);
_foorumRepository.SaveChanges();
return _factory.Create(f);
2017-06-12 13:37:48 +03:00
}
public FoorumDTO Get(int id)
{
2017-06-12 14:43:31 +03:00
var f =_foorumRepository.GetById(id);
return _factory.Create(f, withPosts:true);
2017-06-12 13:37:48 +03:00
}
public IEnumerable<FoorumDTO> GetAll()
{
foreach (var f in _foorumRepository.GetAll())
{
yield return _factory.Create(f);
}
2017-06-12 13:37:48 +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);
if (f == null)
{
return null;
}
2017-06-12 16:15:34 +03:00
f.Visible = !hide;
_foorumRepository.Update(f);
_foorumRepository.SaveChanges();
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)
{
throw new NotImplementedException();
}
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
}
}
}