diff --git a/BL/BL.csproj b/BL/BL.csproj index e5ce51e..c2e7a25 100644 --- a/BL/BL.csproj +++ b/BL/BL.csproj @@ -42,6 +42,7 @@ + diff --git a/BL/Factories/FoorumFactory.cs b/BL/Factories/FoorumFactory.cs new file mode 100644 index 0000000..bec3fec --- /dev/null +++ b/BL/Factories/FoorumFactory.cs @@ -0,0 +1,46 @@ + +using System; +using System.Collections.Generic; +using BL.DTOs; +using DAL.Domain; + +namespace BL.Factories +{ + public class FoorumFactory + { + public FoorumDTO Create(Foorum f, bool withPosts = false) + { + var dto = new FoorumDTO() + { + FoorumId = f.FoorumId, + Title = f.Title, + Author = f.Author, + Body = f.Body, + CreationTime = f.CreationTime, + Description = f.Description, + Visible = f.Visible + }; + + if (withPosts) + { + throw new NotImplementedException(); + //dto.Posts = new List(); + //f.Posts.ForEach(p => {dto.Posts.Add();}); + } + + return dto; + } + + public Foorum Create(FoorumDTO f) + { + return new Foorum() + { + Author = f.Author, + Body = f.Body, + CreationTime = f.CreationTime, + Description = f.Description, + Title = f.Title + }; + } + } +} diff --git a/BL/Services/FoorumService.cs b/BL/Services/FoorumService.cs index 364c62e..aed0e40 100644 --- a/BL/Services/FoorumService.cs +++ b/BL/Services/FoorumService.cs @@ -5,6 +5,7 @@ using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using BL.DTOs; +using BL.Factories; using BL.Interfaces; using DAL.Domain; using DAL.Interfaces; @@ -13,32 +14,19 @@ namespace BL.Services { public class FoorumService : IFoorumService { - private IFoorumRepository _foorumRepository; + private readonly IFoorumRepository _foorumRepository; + private readonly FoorumFactory _factory; public FoorumService(IFoorumRepository foorumRepository) { _foorumRepository = foorumRepository; + _factory = new FoorumFactory(); } public FoorumDTO Add(FoorumDTO foorum) { - var foorumDomain = new Foorum() - { - Author = foorum.Author, - Body = foorum.Body, - CreationTime = DateTime.Now, - Description = foorum.Description, - Title = foorum.Title - }; + var foorumDomain = _factory.Create(foorum); + foorumDomain.CreationTime = DateTime.Now; var f = _foorumRepository.Add(foorumDomain); - return new FoorumDTO() - { - FoorumId = f.FoorumId, - Title = f.Title, - Author = f.Author, - Body = f.Body, - CreationTime = f.CreationTime, - Description = f.Description, - Visible = f.Visible - }; + return _factory.Create(f); } public void AddPost(int id, PostDTO post) @@ -60,16 +48,7 @@ namespace BL.Services { foreach (var f in _foorumRepository.GetAll()) { - yield return new FoorumDTO() - { - FoorumId = f.FoorumId, - Title = f.Title, - Author = f.Author, - Body = f.Body, - CreationTime = f.CreationTime, - Description = f.Description, - Visible = f.Visible - }; + yield return _factory.Create(f); } }