vreksam/WebApi/index.html

121 lines
3.9 KiB
HTML
Raw Permalink Normal View History

2017-06-12 17:45:33 +03:00
<h1><a href="/api/foorum">/api/foorum</a></h1>
2017-06-12 15:26:34 +03:00
<br/>
2017-06-12 14:43:31 +03:00
<button onclick="getForum(prompt('ForumId: ', '1'))">GetForum</button>
2017-06-12 14:16:38 +03:00
<button onclick="getForums()">GetAllForums</button>
2017-06-12 17:45:33 +03:00
<button onclick="addForum(prompt('Title: ', 'Uus foorum'), 'kirjeldus', '<27>ge body', prompt('Author:', 'Javascript'))">AddForum</button>
2017-06-12 15:46:53 +03:00
<button onclick="changeForumTitle(prompt('ForumId: ', '1'), prompt('ForumTitle: ', 'Muudetud'))">ChangeForumTitle</button>
<br/>
2017-06-12 16:15:34 +03:00
<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>
<button onclick="searchForum('authorPosts', prompt('Author: ', 'test'))">SearchAuthorPosts</button>
<br/>
2017-06-12 17:45:33 +03:00
<button onclick="addPost(prompt('ForumId', '1'), prompt('Title:', 'Best title'), prompt('Body', 'Lahe post'), prompt('Author', 'Ajax'))">AddPost</button>
2017-06-12 16:15:34 +03:00
2017-06-12 13:37:48 +03:00
<code><pre id="output"></pre></code>
<script>
2017-06-12 14:16:38 +03:00
var out = document.getElementById("output");
2017-06-12 14:43:31 +03:00
function getForum(id) {
fetch("/api/foorum/"+id).then(resp => {
return resp.json();
}).then(data => {
console.log(data);
out.innerText = JSON.stringify(data, null, 2);
});
}
2017-06-12 13:37:48 +03:00
function getForums() {
fetch("/api/foorum").then(resp => {
return resp.json();
}).then(data => {
console.log(data);
out.innerText = JSON.stringify(data, null, 2);
});
}
2017-06-12 14:16:38 +03:00
2017-06-12 17:45:33 +03:00
function addForum(title, description, body, author) {
if (title == null) throw new Error("title unset");
if (description == null) throw new Error("description unset");
if (body == null) throw new Error("body unset");
if (author == null) throw new Error("author unset");
2017-06-12 14:16:38 +03:00
var dto = {
2017-06-12 17:45:33 +03:00
Author: author,
Body: body,
Description: description,
2017-06-12 16:15:34 +03:00
Title: title
2017-06-12 14:16:38 +03:00
}
fetch("/api/foorum",
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
2017-06-12 16:15:34 +03:00
}).then(resp => getForums());
2017-06-12 14:16:38 +03:00
}
2017-06-12 15:26:34 +03:00
2017-06-12 15:46:53 +03:00
function changeForumTitle(id, title) {
var dto = {
Title: title
}
fetch("/api/foorum/"+id,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
});
}
2017-06-12 16:15:34 +03:00
function deleteForum(id, permanent) {
2017-06-12 17:45:33 +03:00
if (id == null) throw new Error("Forum id is unset");
2017-06-12 16:15:34 +03:00
var q = "";
if (permanent) {
q = "?permanent=true";
}
2017-06-12 17:45:33 +03:00
fetch("/api/foorum/" + id + q, { method: 'DELETE' })
.then(resp => getForums());
2017-06-12 16:15:34 +03:00
}
function searchForum(what, author) {
fetch("/api/search?"+what+"="+author).then(resp => {
2017-06-12 15:26:34 +03:00
return resp.json();
}).then(data => {
console.log(data);
out.innerText = JSON.stringify(data, null, 2);
});
}
2017-06-12 17:45:33 +03:00
function addPost(id, title, body, author) {
if (id == null) throw new Error("forum id is unset");
if (title == null) throw new Error("title is unset");
if (body == null) throw new Error("body is unset");
if (author == null) throw new Error("author is unset");
var dto = {
2017-06-12 17:45:33 +03:00
Author: author,
Body: body,
Title: title
}
2017-06-12 17:45:33 +03:00
fetch("/api/foorum/"+id,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(dto)
}).then(resp => getForum(id));
}
getForums();
2017-06-12 13:37:48 +03:00
</script>