Commit | Line | Data |
---|---|---|
c5c47010 | 1 | const db = require("../utils/database"); |
604b951e BA |
2 | |
3 | /* | |
4 | * Structure: | |
5 | * id: integer | |
6 | * added: datetime | |
7 | * uid: user id (int) | |
8 | * content: text | |
9 | */ | |
10 | ||
11 | const NewsModel = | |
12 | { | |
13 | create: function(content, uid, cb) | |
14 | { | |
15 | db.serialize(function() { | |
16 | const query = | |
17 | "INSERT INTO News " + | |
18 | "(added, uid, content) " + | |
19 | "VALUES " + | |
20 | "(" + Date.now() + "," + uid + ",?)"; | |
21 | db.run(query, content, function(err) { | |
866842c3 | 22 | cb(err, {nid: this.lastID}); |
604b951e BA |
23 | }); |
24 | }); | |
25 | }, | |
26 | ||
27 | getNext: function(cursor, cb) | |
28 | { | |
29 | db.serialize(function() { | |
30 | const query = | |
31 | "SELECT * " + | |
32 | "FROM News " + | |
33 | "WHERE id > " + cursor + " " + | |
34 | "LIMIT 10"; //TODO: 10 currently hard-coded | |
35 | db.all(query, (err,newsList) => { | |
866842c3 | 36 | cb(err, newsList); |
604b951e BA |
37 | }); |
38 | }); | |
39 | }, | |
40 | ||
866842c3 | 41 | update: function(news) |
604b951e BA |
42 | { |
43 | db.serialize(function() { | |
44 | let query = | |
45 | "UPDATE News " + | |
46 | "SET content = ? " + | |
47 | "WHERE id = " + news.id; | |
866842c3 | 48 | db.run(query, news.content); |
604b951e BA |
49 | }); |
50 | }, | |
51 | ||
866842c3 | 52 | remove: function(id) |
604b951e BA |
53 | { |
54 | db.serialize(function() { | |
55 | const query = | |
56 | "DELETE FROM News " + | |
57 | "WHERE id = " + id; | |
866842c3 | 58 | db.run(query); |
604b951e BA |
59 | }); |
60 | }, | |
61 | } | |
62 | ||
63 | module.exports = NewsModel; |