Add post, fix delete forum, fix null pointer exceptions, min max limist
on database strings
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BL.DTOs;
|
||||
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;
|
||||
public FoorumService(IFoorumRepository foorumRepository)
|
||||
private readonly PostFactory _postFactory;
|
||||
public FoorumService(IFoorumRepository foorumRepository, IPostRepository postRepository)
|
||||
{
|
||||
_foorumRepository = foorumRepository;
|
||||
_factory = new FoorumFactory();
|
||||
_postRepository = postRepository;
|
||||
_factory = new FoorumFactory(this);
|
||||
_postFactory = new PostFactory(this);
|
||||
}
|
||||
public FoorumDTO Add(FoorumDTO foorum)
|
||||
{
|
||||
@@ -25,9 +30,20 @@ namespace BL.Services
|
||||
return _factory.Create(f);
|
||||
}
|
||||
|
||||
public void AddPost(int id, PostDTO post)
|
||||
public PostDTO AddPost(int id, PostDTO post)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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)
|
||||
@@ -37,23 +53,43 @@ namespace BL.Services
|
||||
{
|
||||
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)
|
||||
public FoorumDTO Get(int id, bool withPosts=true)
|
||||
{
|
||||
var f =_foorumRepository.GetById(id);
|
||||
return _factory.Create(f, withPosts:true);
|
||||
if (f == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _factory.Create(f, withPosts);
|
||||
}
|
||||
|
||||
public IEnumerable<FoorumDTO> GetAll()
|
||||
public IEnumerable<FoorumDTO> GetAll(bool withHidden = false, bool withPosts = false)
|
||||
{
|
||||
foreach (var f in _foorumRepository.GetAll())
|
||||
IEnumerable<Foorum> results;
|
||||
if (withHidden)
|
||||
{
|
||||
yield return _factory.Create(f);
|
||||
results = _foorumRepository.GetAll();
|
||||
}
|
||||
else
|
||||
{
|
||||
results = _foorumRepository.GetMany(f => f.Visible);
|
||||
}
|
||||
|
||||
foreach (var f in results)
|
||||
{
|
||||
yield return _factory.Create(f, withPosts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +126,10 @@ namespace BL.Services
|
||||
|
||||
public IEnumerable<PostDTO> SearchPostAuthor(string query)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user