Refactor BL foorum service to use a factory for DTOS
This commit is contained in:
parent
4aadaf08fe
commit
475f0d539c
@ -42,6 +42,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="DTOs\FoorumDTO.cs" />
|
||||
<Compile Include="DTOs\PostDTO.cs" />
|
||||
<Compile Include="Factories\FoorumFactory.cs" />
|
||||
<Compile Include="Interfaces\IFoorumService.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\FoorumService.cs" />
|
||||
|
46
BL/Factories/FoorumFactory.cs
Normal file
46
BL/Factories/FoorumFactory.cs
Normal file
@ -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<PostDTO>();
|
||||
//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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user