| 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, {nid: 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 id > " + cursor + " " + |
| 34 | "LIMIT 10"; //TODO: 10 currently hard-coded |
| 35 | db.all(query, (err,newsList) => { |
| 36 | cb(err, newsList); |
| 37 | }); |
| 38 | }); |
| 39 | }, |
| 40 | |
| 41 | update: function(news) |
| 42 | { |
| 43 | db.serialize(function() { |
| 44 | let query = |
| 45 | "UPDATE News " + |
| 46 | "SET content = ? " + |
| 47 | "WHERE id = " + news.id; |
| 48 | db.run(query, news.content); |
| 49 | }); |
| 50 | }, |
| 51 | |
| 52 | remove: function(id) |
| 53 | { |
| 54 | db.serialize(function() { |
| 55 | const query = |
| 56 | "DELETE FROM News " + |
| 57 | "WHERE id = " + id; |
| 58 | db.run(query); |
| 59 | }); |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | module.exports = NewsModel; |