diff --git a/BL/Factories/FoorumFactory.cs b/BL/Factories/FoorumFactory.cs index acbbd98..376b865 100644 --- a/BL/Factories/FoorumFactory.cs +++ b/BL/Factories/FoorumFactory.cs @@ -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() diff --git a/BL/Factories/PostFactory.cs b/BL/Factories/PostFactory.cs index ebcaba5..b08ed6b 100644 --- a/BL/Factories/PostFactory.cs +++ b/BL/Factories/PostFactory.cs @@ -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; + } } } diff --git a/BL/Interfaces/IFoorumService.cs b/BL/Interfaces/IFoorumService.cs index f6071e6..f60f484 100644 --- a/BL/Interfaces/IFoorumService.cs +++ b/BL/Interfaces/IFoorumService.cs @@ -9,13 +9,13 @@ namespace BL.Interfaces { public interface IFoorumService { - IEnumerable GetAll(); - FoorumDTO Get(int id); + IEnumerable 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 SearchFoorumTitle(string query); IEnumerable SearchFoorumAuthor(string query); diff --git a/BL/Services/FoorumService.cs b/BL/Services/FoorumService.cs index 104943f..61ea66b 100644 --- a/BL/Services/FoorumService.cs +++ b/BL/Services/FoorumService.cs @@ -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 GetAll() + public IEnumerable GetAll(bool withHidden = false, bool withPosts = false) { - foreach (var f in _foorumRepository.GetAll()) + IEnumerable 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 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) diff --git a/DAL/Domain/Foorum.cs b/DAL/Domain/Foorum.cs index 35c8f8f..065168b 100644 --- a/DAL/Domain/Foorum.cs +++ b/DAL/Domain/Foorum.cs @@ -8,10 +8,23 @@ namespace DAL.Domain { [Key] public int FoorumId { get; set; } + + [MaxLength(length: 128)] + [MinLength(length: 1)] public string Title { get; set; } + + [MaxLength(length: 256)] + [MinLength(length: 1)] public string Description { get; set; } + + + [MaxLength(length: 1024)] + [MinLength(length: 1)] public string Body { get; set; } public DateTime CreationTime { get; set; } + + [MaxLength(length: 128)] + [MinLength(length: 1)] public string Author { get; set; } public bool Visible { get; set; } = true; diff --git a/DAL/Domain/Post.cs b/DAL/Domain/Post.cs index cacdb79..0967458 100644 --- a/DAL/Domain/Post.cs +++ b/DAL/Domain/Post.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace DAL.Domain { @@ -7,13 +8,22 @@ namespace DAL.Domain { [Key] public int PostId { get; set; } + + [MaxLength(length: 128)] + [MinLength(length: 1)] public string Title { get; set; } + + [MaxLength(length: 1024)] + [MinLength(length: 1)] public string Body { get; set; } public DateTime CreationTime { get; set; } + + [MaxLength(length: 128)] + [MinLength(length: 1)] public string Author { get; set; } + public int ForumId { get; set; } - public Foorum Foorum { get; set; } } } diff --git a/DAL/Helpers/DbInitializer.cs b/DAL/Helpers/DbInitializer.cs index 1201d1b..42b2712 100644 --- a/DAL/Helpers/DbInitializer.cs +++ b/DAL/Helpers/DbInitializer.cs @@ -21,6 +21,7 @@ namespace DAL.Helpers }; ctx.Foorums.Add(foorum); + ctx.SaveChanges(); for (int i = 0; i < 3; i++) @@ -30,7 +31,8 @@ namespace DAL.Helpers Title = $"Post {i}", Author = "Karu", Body = $"Pikk sisu {i}", - CreationTime = DateTime.Now + CreationTime = DateTime.Now, + ForumId = foorum.FoorumId }; ctx.Posts.Add(post); foorum.Posts.Add(post); diff --git a/WebApi/Controllers/FoorumController.cs b/WebApi/Controllers/FoorumController.cs index dd1eaba..110f16d 100644 --- a/WebApi/Controllers/FoorumController.cs +++ b/WebApi/Controllers/FoorumController.cs @@ -83,5 +83,15 @@ namespace WebApi.Controllers 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); + } } } \ No newline at end of file diff --git a/WebApi/Controllers/SearchController.cs b/WebApi/Controllers/SearchController.cs index 82cd00b..1fa406c 100644 --- a/WebApi/Controllers/SearchController.cs +++ b/WebApi/Controllers/SearchController.cs @@ -26,5 +26,10 @@ namespace WebApi.Controllers { return Ok(_foorumService.SearchFoorumTitle(title)); } + + public IHttpActionResult GetAuthorPosts(string authorPosts) + { + return Ok(_foorumService.SearchPostAuthor(authorPosts)); + } } } \ No newline at end of file diff --git a/WebApi/index.html b/WebApi/index.html index c13d547..6685b29 100644 --- a/WebApi/index.html +++ b/WebApi/index.html @@ -11,6 +11,10 @@
+ + +
+
@@ -83,4 +87,22 @@ 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(); \ No newline at end of file