Refactor models (merge Players in Games), add cursor to correspondance games. Finishe...
[vchess.git] / server / models / News.js
CommitLineData
c5c47010 1const 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
11const 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) {
0234201f 22 cb(err, { id: 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 " +
0234201f
BA
33 "WHERE added < " + cursor + " " +
34 "ORDER BY added DESC " +
604b951e
BA
35 "LIMIT 10"; //TODO: 10 currently hard-coded
36 db.all(query, (err,newsList) => {
866842c3 37 cb(err, newsList);
604b951e
BA
38 });
39 });
40 },
41
d9a7a1e4
BA
42 getTimestamp: function(cb)
43 {
44 db.serialize(function() {
45 const query =
46 "SELECT added " +
47 "FROM News " +
48 "ORDER BY added DESC " +
49 "LIMIT 1";
50 db.get(query, (err,ts) => {
51 cb(err, ts);
52 });
53 });
54 },
55
866842c3 56 update: function(news)
604b951e
BA
57 {
58 db.serialize(function() {
59 let query =
60 "UPDATE News " +
61 "SET content = ? " +
62 "WHERE id = " + news.id;
866842c3 63 db.run(query, news.content);
604b951e
BA
64 });
65 },
66
866842c3 67 remove: function(id)
604b951e
BA
68 {
69 db.serialize(function() {
70 const query =
71 "DELETE FROM News " +
72 "WHERE id = " + id;
866842c3 73 db.run(query);
604b951e
BA
74 });
75 },
76}
77
78module.exports = NewsModel;