Working delete and hide of the post

This commit is contained in:
Arti Zirk 2017-06-12 16:15:34 +03:00
parent b1dcf5f72d
commit 41991a68dd
4 changed files with 45 additions and 13 deletions

View File

@ -13,7 +13,7 @@ namespace BL.Interfaces
FoorumDTO Get(int id);
FoorumDTO Add(FoorumDTO foorum);
FoorumDTO Update(FoorumDTO foorum);
void Hide(int id);
void Hide(int id, bool hide=true);
void Delete(int id);
void AddPost(int id, PostDTO post);

View File

@ -32,7 +32,9 @@ namespace BL.Services
public void Delete(int id)
{
throw new NotImplementedException();
var f = _foorumRepository.GetById(id);
_foorumRepository.Delete(f);
_foorumRepository.SaveChanges();
}
public FoorumDTO Get(int id)
@ -50,9 +52,12 @@ namespace BL.Services
}
public void Hide(int id)
public void Hide(int id, bool hide=true)
{
throw new NotImplementedException();
var f = _foorumRepository.GetById(id);
f.Visible = !hide;
_foorumRepository.Update(f);
_foorumRepository.SaveChanges();
}
public IEnumerable<FoorumDTO> SearchFoorumAuthor(string query)

View File

@ -54,12 +54,25 @@ namespace WebApi.Controllers
public IHttpActionResult Put(int id, [FromBody]FoorumDTO foorum)
{
foorum.FoorumId = id;
return Ok(_foorumService.Update(foorum));
var updatedFoorum = _foorumService.Update(foorum);
if (updatedFoorum == null)
{
return NotFound();
}
return Ok(updatedFoorum);
}
// DELETE api/<controller>/5
public void Delete(int id)
public void Delete(int id, [FromUri]bool permanent=false)
{
if (permanent)
{
_foorumService.Delete(id);
}
else
{
_foorumService.Hide(id);
}
}
}
}

View File

@ -3,10 +3,15 @@
<br/>
<button onclick="getForum(prompt('ForumId: ', '1'))">GetForum</button>
<button onclick="getForums()">GetAllForums</button>
<button onclick="addForum()">AddForum</button>
<button onclick="addForum(prompt('Title: ', 'Uus postitus'))">AddForum</button>
<button onclick="changeForumTitle(prompt('ForumId: ', '1'), prompt('ForumTitle: ', 'Muudetud'))">ChangeForumTitle</button>
<br/>
<button onclick="searchForumAuthor(prompt('Author: ', 'veeb'))">SearchForumAuthor</button>
<button onclick="deleteForum(prompt('ForumId: ', '1'), true)">DeleteForum</button>
<button onclick="deleteForum(prompt('ForumId: ', '1'), false)">HideForum</button>
<br/>
<button onclick="searchForum('author', prompt('Author: ', 'veeb'))">SearchForumAuthor</button>
<button onclick="searchForum('title', prompt('Title: ', 'test'))">SearchForumTitle</button>
<code><pre id="output"></pre></code>
<script>
@ -30,12 +35,12 @@
});
}
function addForum() {
function addForum(title) {
var dto = {
Author: "Veebileht",
Body: "Siia tuleb äge pikk sisu mingist asjast",
Description: "Siia kirjeldus",
Title: "Uhiuus postitus"
Title: title
}
fetch("/api/foorum",
{
@ -44,7 +49,7 @@
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
});
}).then(resp => getForums());
}
function changeForumTitle(id, title) {
@ -61,8 +66,17 @@
});
}
function searchForumAuthor(author) {
fetch("/api/search?author="+author).then(resp => {
function deleteForum(id, permanent) {
var q = "";
if (permanent) {
q = "?permanent=true";
}
fetch("/api/foorum/"+id+q, { method: 'DELETE'});
}
function searchForum(what, author) {
fetch("/api/search?"+what+"="+author).then(resp => {
return resp.json();
}).then(data => {
console.log(data);