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

on database strings
This commit is contained in:
Arti Zirk 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;
using System.Collections.Generic; using System.Collections.Generic;
using BL.DTOs; using BL.DTOs;
using BL.Interfaces;
using DAL.Domain; using DAL.Domain;
namespace BL.Factories namespace BL.Factories
{ {
public class FoorumFactory 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) public FoorumDTO Create(Foorum f, bool withPosts = false)
{ {
var dto = new FoorumDTO() var dto = new FoorumDTO()

View File

@ -4,12 +4,18 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BL.DTOs; using BL.DTOs;
using BL.Interfaces;
using DAL.Domain; using DAL.Domain;
namespace BL.Factories namespace BL.Factories
{ {
public class PostFactory public class PostFactory
{ {
private readonly IFoorumService _foorumService;
public PostFactory(IFoorumService foorumService)
{
_foorumService = foorumService;
}
public PostDTO Create(Post p, bool withForum=false) public PostDTO Create(Post p, bool withForum=false)
{ {
@ -24,10 +30,23 @@ namespace BL.Factories
if (withForum) if (withForum)
{ {
throw new NotImplementedException(); dto.Foorum = _foorumService.Get(p.ForumId, withPosts:false);
} }
return dto; 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 public interface IFoorumService
{ {
IEnumerable<FoorumDTO> GetAll(); IEnumerable<FoorumDTO> GetAll(bool withHidden = false, bool withPosts = false);
FoorumDTO Get(int id); FoorumDTO Get(int id, bool withPosts = true);
FoorumDTO Add(FoorumDTO foorum); FoorumDTO Add(FoorumDTO foorum);
FoorumDTO Update(FoorumDTO foorum); FoorumDTO Update(FoorumDTO foorum);
void Hide(int id, bool hide=true); FoorumDTO Hide(int id, bool hide = true);
FoorumDTO Delete(int id); FoorumDTO Delete(int id);
void AddPost(int id, PostDTO post); PostDTO AddPost(int id, PostDTO post);
IEnumerable<FoorumDTO> SearchFoorumTitle(string query); IEnumerable<FoorumDTO> SearchFoorumTitle(string query);
IEnumerable<FoorumDTO> SearchFoorumAuthor(string query); IEnumerable<FoorumDTO> SearchFoorumAuthor(string query);

View File

@ -1,21 +1,26 @@
using System; using BL.DTOs;
using System.Collections.Generic;
using System.Linq;
using BL.DTOs;
using BL.Factories; using BL.Factories;
using BL.Interfaces; using BL.Interfaces;
using DAL.Interfaces; using DAL.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using DAL.Domain;
namespace BL.Services namespace BL.Services
{ {
public class FoorumService : IFoorumService public class FoorumService : IFoorumService
{ {
private readonly IFoorumRepository _foorumRepository; private readonly IFoorumRepository _foorumRepository;
private readonly IPostRepository _postRepository;
private readonly FoorumFactory _factory; private readonly FoorumFactory _factory;
public FoorumService(IFoorumRepository foorumRepository) private readonly PostFactory _postFactory;
public FoorumService(IFoorumRepository foorumRepository, IPostRepository postRepository)
{ {
_foorumRepository = foorumRepository; _foorumRepository = foorumRepository;
_factory = new FoorumFactory(); _postRepository = postRepository;
_factory = new FoorumFactory(this);
_postFactory = new PostFactory(this);
} }
public FoorumDTO Add(FoorumDTO foorum) public FoorumDTO Add(FoorumDTO foorum)
{ {
@ -25,9 +30,20 @@ namespace BL.Services
return _factory.Create(f); 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) public FoorumDTO Delete(int id)
@ -37,23 +53,43 @@ namespace BL.Services
{ {
return null; return null;
} }
foreach (var post in f.Posts.ToArray())
{
_postRepository.Delete(post);
}
_postRepository.SaveChanges();
_foorumRepository.Delete(f); _foorumRepository.Delete(f);
_foorumRepository.SaveChanges(); _foorumRepository.SaveChanges();
return _factory.Create(f); return _factory.Create(f);
} }
public FoorumDTO Get(int id) public FoorumDTO Get(int id, bool withPosts=true)
{ {
var f =_foorumRepository.GetById(id); 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) 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) public FoorumDTO Update(FoorumDTO foorum)

View File

@ -8,10 +8,23 @@ namespace DAL.Domain
{ {
[Key] [Key]
public int FoorumId { get; set; } public int FoorumId { get; set; }
[MaxLength(length: 128)]
[MinLength(length: 1)]
public string Title { get; set; } public string Title { get; set; }
[MaxLength(length: 256)]
[MinLength(length: 1)]
public string Description { get; set; } public string Description { get; set; }
[MaxLength(length: 1024)]
[MinLength(length: 1)]
public string Body { get; set; } public string Body { get; set; }
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
[MaxLength(length: 128)]
[MinLength(length: 1)]
public string Author { get; set; } public string Author { get; set; }
public bool Visible { get; set; } = true; public bool Visible { get; set; } = true;

View File

@ -1,5 +1,6 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DAL.Domain namespace DAL.Domain
{ {
@ -7,13 +8,22 @@ namespace DAL.Domain
{ {
[Key] [Key]
public int PostId { get; set; } public int PostId { get; set; }
[MaxLength(length: 128)]
[MinLength(length: 1)]
public string Title { get; set; } public string Title { get; set; }
[MaxLength(length: 1024)]
[MinLength(length: 1)]
public string Body { get; set; } public string Body { get; set; }
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
[MaxLength(length: 128)]
[MinLength(length: 1)]
public string Author { get; set; } public string Author { get; set; }
public int ForumId { get; set; } public int ForumId { get; set; }
public Foorum Foorum { get; set; }
} }
} }

View File

@ -21,6 +21,7 @@ namespace DAL.Helpers
}; };
ctx.Foorums.Add(foorum); ctx.Foorums.Add(foorum);
ctx.SaveChanges();
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -30,7 +31,8 @@ namespace DAL.Helpers
Title = $"Post {i}", Title = $"Post {i}",
Author = "Karu", Author = "Karu",
Body = $"Pikk sisu {i}", Body = $"Pikk sisu {i}",
CreationTime = DateTime.Now CreationTime = DateTime.Now,
ForumId = foorum.FoorumId
}; };
ctx.Posts.Add(post); ctx.Posts.Add(post);
foorum.Posts.Add(post); foorum.Posts.Add(post);

View File

@ -83,5 +83,15 @@ namespace WebApi.Controllers
return Ok(f); return Ok(f);
} }
public IHttpActionResult PostPost(int id, [FromBody] PostDTO post)
{
var p = _foorumService.AddPost(id, post);
if (p == null)
{
return NotFound();
}
return Ok(p);
}
} }
} }

View File

@ -26,5 +26,10 @@ namespace WebApi.Controllers
{ {
return Ok(_foorumService.SearchFoorumTitle(title)); return Ok(_foorumService.SearchFoorumTitle(title));
} }
public IHttpActionResult GetAuthorPosts(string authorPosts)
{
return Ok(_foorumService.SearchPostAuthor(authorPosts));
}
} }
} }

View File

@ -11,6 +11,10 @@
<br/> <br/>
<button onclick="searchForum('author', prompt('Author: ', 'veeb'))">SearchForumAuthor</button> <button onclick="searchForum('author', prompt('Author: ', 'veeb'))">SearchForumAuthor</button>
<button onclick="searchForum('title', prompt('Title: ', 'test'))">SearchForumTitle</button> <button onclick="searchForum('title', prompt('Title: ', 'test'))">SearchForumTitle</button>
<button onclick="searchForum('authorPosts', prompt('Author: ', 'test'))">SearchAuthorPosts</button>
<br/>
<button onclick="addPost(prompt('ForumId', '1'), 'Best title', prompt('Body', 'Lahe post'))">AddPost</button>
<code><pre id="output"></pre></code> <code><pre id="output"></pre></code>
@ -83,4 +87,22 @@
out.innerText = JSON.stringify(data, null, 2); out.innerText = JSON.stringify(data, null, 2);
}); });
} }
function addPost(id, title, body) {
var dto = {
Author: "Ajax",
Body: body,
Title: title
}
fetch("/api/foorum/"+id,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
}).then(resp => getForum(id));
}
getForums();
</script> </script>