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 | ||
d9a7a1e4 BA |
41 | getTimestamp: function(cb) |
42 | { | |
43 | db.serialize(function() { | |
44 | const query = | |
45 | "SELECT added " + | |
46 | "FROM News " + | |
47 | "ORDER BY added DESC " + | |
48 | "LIMIT 1"; | |
49 | db.get(query, (err,ts) => { | |
50 | cb(err, ts); | |
51 | }); | |
52 | }); | |
53 | }, | |
54 | ||
866842c3 | 55 | update: function(news) |
604b951e BA |
56 | { |
57 | db.serialize(function() { | |
58 | let query = | |
59 | "UPDATE News " + | |
60 | "SET content = ? " + | |
61 | "WHERE id = " + news.id; | |
866842c3 | 62 | db.run(query, news.content); |
604b951e BA |
63 | }); |
64 | }, | |
65 | ||
866842c3 | 66 | remove: function(id) |
604b951e BA |
67 | { |
68 | db.serialize(function() { | |
69 | const query = | |
70 | "DELETE FROM News " + | |
71 | "WHERE id = " + id; | |
866842c3 | 72 | db.run(query); |
604b951e BA |
73 | }); |
74 | }, | |
75 | } | |
76 | ||
77 | module.exports = NewsModel; |