Getting a forum with id's works

This commit is contained in:
Arti Zirk 2017-06-12 14:43:31 +03:00
parent 475f0d539c
commit bf9ec07808
7 changed files with 60 additions and 11 deletions

View File

@ -43,6 +43,7 @@
<Compile Include="DTOs\FoorumDTO.cs" /> <Compile Include="DTOs\FoorumDTO.cs" />
<Compile Include="DTOs\PostDTO.cs" /> <Compile Include="DTOs\PostDTO.cs" />
<Compile Include="Factories\FoorumFactory.cs" /> <Compile Include="Factories\FoorumFactory.cs" />
<Compile Include="Factories\PostFactory.cs" />
<Compile Include="Interfaces\IFoorumService.cs" /> <Compile Include="Interfaces\IFoorumService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\FoorumService.cs" /> <Compile Include="Services\FoorumService.cs" />

View File

@ -10,6 +10,10 @@ namespace BL.DTOs
{ {
public int PostId { get; set; } public int PostId { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Body { get; set; }
public DateTime CreationTime { get; set; }
public string Author { get; set; }
public FoorumDTO Foorum { get; set; }
} }
} }

View File

@ -8,6 +8,7 @@ namespace BL.Factories
{ {
public class FoorumFactory public class FoorumFactory
{ {
private readonly PostFactory _postFactory = new PostFactory();
public FoorumDTO Create(Foorum f, bool withPosts = false) public FoorumDTO Create(Foorum f, bool withPosts = false)
{ {
var dto = new FoorumDTO() var dto = new FoorumDTO()
@ -23,9 +24,8 @@ namespace BL.Factories
if (withPosts) if (withPosts)
{ {
throw new NotImplementedException(); dto.Posts = new List<PostDTO>();
//dto.Posts = new List<PostDTO>(); f.Posts.ForEach(p => {dto.Posts.Add(_postFactory.Create(p));});
//f.Posts.ForEach(p => {dto.Posts.Add();});
} }
return dto; return dto;

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BL.DTOs;
using DAL.Domain;
namespace BL.Factories
{
public class PostFactory
{
public PostDTO Create(Post p, bool withForum=false)
{
var dto = new PostDTO()
{
PostId = p.PostId,
Title = p.Title,
Author = p.Author,
Body = p.Body,
CreationTime = p.CreationTime,
};
if (withForum)
{
throw new NotImplementedException();
}
return dto;
}
}
}

View File

@ -1,13 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using BL.DTOs; using BL.DTOs;
using BL.Factories; using BL.Factories;
using BL.Interfaces; using BL.Interfaces;
using DAL.Domain;
using DAL.Interfaces; using DAL.Interfaces;
namespace BL.Services namespace BL.Services
@ -41,7 +36,8 @@ namespace BL.Services
public FoorumDTO Get(int id) public FoorumDTO Get(int id)
{ {
throw new NotImplementedException(); var f =_foorumRepository.GetById(id);
return _factory.Create(f, withPosts:true);
} }
public IEnumerable<FoorumDTO> GetAll() public IEnumerable<FoorumDTO> GetAll()

View File

@ -23,9 +23,14 @@ namespace WebApi.Controllers
} }
// GET api/<controller>/5 // GET api/<controller>/5
public string Get(int id) public IHttpActionResult Get(int id)
{ {
return "value"; var f = _foorumService.Get(id);
if (f == null)
{
return NotFound();
}
return Ok(f);
} }
// POST api/<controller> // POST api/<controller>

View File

@ -1,5 +1,6 @@
<h1>api</h1> <h1>api</h1>
<a href="/api/foorum">foorum</a> <a href="/api/foorum">foorum</a>
<button onclick="getForum(prompt('ForumId: ', '1'))">GetForum</button>
<button onclick="getForums()">GetAllForums</button> <button onclick="getForums()">GetAllForums</button>
<button onclick="addForum()">AddForum</button> <button onclick="addForum()">AddForum</button>
<code><pre id="output"></pre></code> <code><pre id="output"></pre></code>
@ -7,6 +8,15 @@
<script> <script>
var out = document.getElementById("output"); var out = document.getElementById("output");
function getForum(id) {
fetch("/api/foorum/"+id).then(resp => {
return resp.json();
}).then(data => {
console.log(data);
out.innerText = JSON.stringify(data, null, 2);
});
}
function getForums() { function getForums() {
fetch("/api/foorum").then(resp => { fetch("/api/foorum").then(resp => {
return resp.json(); return resp.json();