Skip to content

Commit d5eff40

Browse files
committed
news article editing
1 parent 3373fae commit d5eff40

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/handlers/web-content.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,26 @@ async function getNews({
2121
this.socket.sendMessage({ token, articles, pages });
2222
}
2323

24-
async function addNews({ token, title, date, category, body }) {
25-
const queryHandler = this.server.queryHandler;
24+
async function addNews({ token, id, title, date, category, body }) {
2625
date = date || Math.floor(Date.now() / 1000);
2726

27+
const queryHandler = this.server.queryHandler;
28+
2829
try {
29-
queryHandler.insertNews({ title, category, body, date });
30+
console.log(id, body);
31+
32+
if (typeof id === 'number') {
33+
queryHandler.editNews({ id, title, category, body, date });
34+
} else {
35+
queryHandler.insertNews({ title, category, body, date });
36+
}
37+
3038
this.socket.sendMessage({ token, success: true });
3139
} catch (e) {
3240
this.socket.sendMessage({ token, success: false });
3341
}
3442
}
3543

36-
async function editNews({ token, id, title, category, body }) {}
37-
3844
async function addFile({ token, name, file }) {}
3945

4046
async function getFile({ token, name }) {
@@ -54,7 +60,6 @@ async function getGodLetter({ token, id }) {
5460
module.exports = {
5561
getNews,
5662
addNews,
57-
editNews,
5863
addFile,
5964
getFile,
6065
getGodLetter

src/query-handler.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ class QueryHandler {
158158
`LIMIT ${NEWS_PER_PAGE} OFFSET (:page * ${NEWS_PER_PAGE})`,
159159
getNews: 'SELECT * FROM `news` WHERE `id` = ?',
160160
insertNews:
161-
'INSERT INTO `news` (`date`, `category`, `title`, `body`) ' +
161+
'INSERT OR REPLACE INTO `news` ' +
162+
'(`date`, `category`, `title`, `body`) ' +
162163
'VALUES (:date, :category, :title, :body)',
164+
editNews:
165+
'UPDATE `news` SET ' +
166+
'`title` = :title, `body` = :body, `date` = :date ' +
167+
'WHERE `id` = :id',
163168
getFile: 'SELECT `file` FROM `uploads` WHERE `name` = ?',
164169
getWebPlayer:
165170
'SELECT `id`, `rank` FROM `players` WHERE `username` = ?',
@@ -517,6 +522,10 @@ class QueryHandler {
517522
return this.statements.insertNews.run(article);
518523
}
519524

525+
editNews(article) {
526+
return this.statements.editNews.run(article);
527+
}
528+
520529
getFile(name) {
521530
return this.statements.getFile.pluck().get(name);
522531
}

0 commit comments

Comments
 (0)