Working forum post

This commit is contained in:
Arti Zirk 2017-06-12 14:16:38 +03:00
parent aeb18aa75b
commit 4aadaf08fe
8 changed files with 75 additions and 22 deletions

View File

@ -40,7 +40,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="DTOs\FoorumDTO.cs" /> <Compile Include="DTOs\FoorumDTO.cs" />
<Compile Include="DTOs\PostDTO.cs" /> <Compile Include="DTOs\PostDTO.cs" />
<Compile Include="Interfaces\IFoorumService.cs" /> <Compile Include="Interfaces\IFoorumService.cs" />

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BL
{
public class Class1
{
}
}

View File

@ -10,6 +10,13 @@ namespace BL.DTOs
{ {
public int FoorumId { get; set; } public int FoorumId { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; }
public string Body { get; set; }
public DateTime CreationTime { get; set; }
public string Author { get; set; }
public bool Visible { get; set; }
public List<PostDTO> Posts { get; set; }
} }
} }

View File

@ -11,7 +11,7 @@ namespace BL.Interfaces
{ {
IEnumerable<FoorumDTO> GetAll(); IEnumerable<FoorumDTO> GetAll();
FoorumDTO Get(int id); FoorumDTO Get(int id);
void Add(FoorumDTO foorum); FoorumDTO Add(FoorumDTO foorum);
void Update(FoorumDTO foorum); void Update(FoorumDTO foorum);
void Hide(int id); void Hide(int id);
void Delete(int id); void Delete(int id);

View File

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BL.DTOs; using BL.DTOs;
using BL.Interfaces; using BL.Interfaces;
using DAL.Domain;
using DAL.Interfaces; using DAL.Interfaces;
namespace BL.Services namespace BL.Services
@ -16,9 +18,27 @@ namespace BL.Services
{ {
_foorumRepository = foorumRepository; _foorumRepository = foorumRepository;
} }
public void Add(FoorumDTO foorum) public FoorumDTO Add(FoorumDTO foorum)
{ {
throw new NotImplementedException(); var foorumDomain = new Foorum()
{
Author = foorum.Author,
Body = foorum.Body,
CreationTime = DateTime.Now,
Description = foorum.Description,
Title = foorum.Title
};
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
};
} }
public void AddPost(int id, PostDTO post) public void AddPost(int id, PostDTO post)
@ -43,7 +63,12 @@ namespace BL.Services
yield return new FoorumDTO() yield return new FoorumDTO()
{ {
FoorumId = f.FoorumId, FoorumId = f.FoorumId,
Title = f.Title Title = f.Title,
Author = f.Author,
Body = f.Body,
CreationTime = f.CreationTime,
Description = f.Description,
Visible = f.Visible
}; };
} }

View File

@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
@ -42,8 +43,11 @@ namespace WebApi
defaults: new { id = RouteParameter.Optional } defaults: new { id = RouteParameter.Optional }
); );
// Dont include properties with null values
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
// Force always json response // Force always json response
var jsonFormatter = new JsonMediaTypeFormatter(); //var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here //optional: set serializer settings here
//config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); //config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
} }

View File

@ -29,8 +29,19 @@ namespace WebApi.Controllers
} }
// POST api/<controller> // POST api/<controller>
public void Post([FromBody]string value) public IHttpActionResult Post([FromBody]FoorumDTO foorum)
{ {
if (foorum == null)
{
return BadRequest();
}
var ret = _foorumService.Add(foorum);
if (ret != null)
{
return CreatedAtRoute("DefaultApi", new {controller = "Foorum", id = ret.FoorumId}, ret);
}
return BadRequest();
} }
// PUT api/<controller>/5 // PUT api/<controller>/5

View File

@ -1,11 +1,13 @@
<h1>api</h1> <h1>api</h1>
<a href="/api/foorum">foorum</a> <a href="/api/foorum">foorum</a>
<button onclick="getForums()">GetAll</button> <button onclick="getForums()">GetAllForums</button>
<button onclick="addForum()">AddForum</button>
<code><pre id="output"></pre></code> <code><pre id="output"></pre></code>
<script> <script>
function getForums() {
var out = document.getElementById("output"); var out = document.getElementById("output");
function getForums() {
fetch("/api/foorum").then(resp => { fetch("/api/foorum").then(resp => {
return resp.json(); return resp.json();
}).then(data => { }).then(data => {
@ -13,4 +15,21 @@
out.innerText = JSON.stringify(data, null, 2); out.innerText = JSON.stringify(data, null, 2);
}); });
} }
function addForum() {
var dto = {
Author: "Veebileht",
Body: "Siia tuleb äge pikk sisu mingist asjast",
Description: "Siia kirjeldus",
Title: "Uhiuus postitus"
}
fetch("/api/foorum",
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
});
}
</script> </script>