multi-account-post-schedule.../routes/index.js

119 lines
3.1 KiB
JavaScript

var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3');
const database_masto = "mastodon_multi_accounts.db"
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', {title: 'Express'});
});
// publier un message avec un certain compte
router.get('/publish', function (req, res, next) {
res.render('index', {title: 'Express'});
});
const connect = require('@databases/sqlite');
const {sql} = require('@databases/sqlite');
const db = connect(database_masto);
function createDatabase() {
var newdb = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
createTables(newdb);
})
}
function createTables(newdb) {
async function prepare() {
await db.query(sql`
create table posts_scheduled
(
action_id integer
constraint posts_scheduled_pk
primary key autoincrement,
post_username varchar not null,
content varchar not null,
medias varchar,
datetime datetime
);
create unique index posts_scheduled_action_id_uindex
on posts_scheduled (action_id);
insert into posts_scheduled
values (NULL, "modominem", "un message d'example", NULL);
`);
console.log('requête de création faite')
}
const prepared = prepare();
}
function getAllPosts(db) {
return db.query(sql`SELECT *
FROM posts_scheduled;`)
}
router.get('/init-db', function (req, res, next) {
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
console.log('got to create db')
createDatabase()
}
)
res.render('created_db', {});
});
router.get('/list', function (req, res, next) {
// let posts_list = [{
// action_id: 1,
// post_username: "modominem",
// content: "demo post list",
// medias: "media file name from assets folder",
// date_schedule: "2022-07-07 "
// }]
getAllPosts(db).then(
(results) => {
console.log( "liste de posts " ,results)
res.render('database', {posts_list: results});
},
(err) => console.error(err),
);
});
router.get('/add-to-db-example', function (req, res, next) {
let db = connect(database_masto);
async function prepare() {
await db.query(sql`
insert into posts_scheduled
values (NULL, "modominem", "un message d'example", "un jour")
;
`).then(resp => {
console.log(resp)
let posts_list = []
res.render('database', {posts_list: posts_list});
},
err => {
console.log(err)
});
}
}
);
// publier le message en db dans la file
router.get('/publish-last-entry', function (req, res, next) {
res.render('index', {title: 'Express'});
});
module.exports = router;