Add post, fix delete forum, fix null pointer exceptions, min max limist

on database strings
This commit is contained in:
2017-06-12 17:22:58 +03:00
parent 1249950059
commit 430d02f59e
10 changed files with 148 additions and 22 deletions

View File

@@ -2,13 +2,19 @@
using System;
using System.Collections.Generic;
using BL.DTOs;
using BL.Interfaces;
using DAL.Domain;
namespace BL.Factories
{
public class FoorumFactory
{
private readonly PostFactory _postFactory = new PostFactory();
private readonly PostFactory _postFactory;
public FoorumFactory(IFoorumService forumService)
{
_postFactory = new PostFactory(forumService);
}
public FoorumDTO Create(Foorum f, bool withPosts = false)
{
var dto = new FoorumDTO()

View File

@@ -4,12 +4,18 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BL.DTOs;
using BL.Interfaces;
using DAL.Domain;
namespace BL.Factories
{
public class PostFactory
{
private readonly IFoorumService _foorumService;
public PostFactory(IFoorumService foorumService)
{
_foorumService = foorumService;
}
public PostDTO Create(Post p, bool withForum=false)
{
@@ -24,10 +30,23 @@ namespace BL.Factories
if (withForum)
{
throw new NotImplementedException();
dto.Foorum = _foorumService.Get(p.ForumId, withPosts:false);
}
return dto;
}
public Post Create(PostDTO p)
{
var domain = new Post()
{
PostId = p.PostId,
Title = p.Title,
Author = p.Author,
Body = p.Body,
CreationTime = p.CreationTime,
};
return domain;
}
}
}

View File

@@ -9,13 +9,13 @@ namespace BL.Interfaces
{
public interface IFoorumService
{
IEnumerable<FoorumDTO> GetAll();
FoorumDTO Get(int id);
IEnumerable<FoorumDTO> GetAll(bool withHidden = false, bool withPosts = false);
FoorumDTO Get(int id, bool withPosts = true);
FoorumDTO Add(FoorumDTO foorum);
FoorumDTO Update(FoorumDTO foorum);
void Hide(int id, bool hide=true);
FoorumDTO Hide(int id, bool hide = true);
FoorumDTO Delete(int id);
void AddPost(int id, PostDTO post);
PostDTO AddPost(int id, PostDTO post);
IEnumerable<FoorumDTO> SearchFoorumTitle(string query);
IEnumerable<FoorumDTO> SearchFoorumAuthor(string query);

View File

@@ -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)