| 1 | const db = require("../utils/database"); |
| 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) { |
| 22 | cb(err, { id: this.lastID }); |
| 23 | }); |
| 24 | }); |
| 25 | }, |
| 26 | |
| 27 | getNext: function(cursor, cb) |
| 28 | { |
| 29 | db.serialize(function() { |
| 30 | const query = |
| 31 | "SELECT * " + |
| 32 | "FROM News " + |
| 33 | "WHERE added < " + cursor + " " + |
| 34 | "ORDER BY added DESC " + |
| 35 | "LIMIT 10"; //TODO: 10 currently hard-coded |
| 36 | db.all(query, (err, newsList) => { |
| 37 | cb(err, newsList); |
| 38 | }); |
| 39 | }); |
| 40 | }, |
| 41 | |
| 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 | |
| 56 | update: function(news) |
| 57 | { |
| 58 | db.serialize(function() { |
| 59 | let query = |
| 60 | "UPDATE News " + |
| 61 | "SET content = ? " + |
| 62 | "WHERE id = " + news.id; |
| 63 | db.run(query, news.content); |
| 64 | }); |
| 65 | }, |
| 66 | |
| 67 | remove: function(id) |
| 68 | { |
| 69 | db.serialize(function() { |
| 70 | const query = |
| 71 | "DELETE FROM News " + |
| 72 | "WHERE id = " + id; |
| 73 | db.run(query); |
| 74 | }); |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | module.exports = NewsModel; |