lister les messages en bdd
This commit is contained in:
parent
3f4dc96e97
commit
b92e5bd049
Binary file not shown.
@ -8,3 +8,9 @@ a {
|
|||||||
padding: 1em;
|
padding: 1em;
|
||||||
margin-right:1ch;
|
margin-right:1ch;
|
||||||
}
|
}
|
||||||
|
.post-message{
|
||||||
|
margin: 1em;
|
||||||
|
border-left: 3px solid #ccc;
|
||||||
|
padding-left: 1em;
|
||||||
|
|
||||||
|
}
|
@ -20,8 +20,6 @@ const {sql} = require('@databases/sqlite');
|
|||||||
const db = connect(database_masto);
|
const db = connect(database_masto);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function createDatabase() {
|
function createDatabase() {
|
||||||
var newdb = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
var newdb = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
||||||
|
|
||||||
@ -38,37 +36,33 @@ function createTables(newdb) {
|
|||||||
constraint posts_scheduled_pk
|
constraint posts_scheduled_pk
|
||||||
primary key autoincrement,
|
primary key autoincrement,
|
||||||
post_username varchar not null,
|
post_username varchar not null,
|
||||||
content varchar not null,
|
content varchar not null,
|
||||||
medias varchar,
|
medias varchar,
|
||||||
datetime datetime
|
datetime datetime
|
||||||
);
|
);
|
||||||
|
|
||||||
create unique index posts_scheduled_action_id_uindex
|
create unique index posts_scheduled_action_id_uindex
|
||||||
on posts_scheduled (action_id);
|
on posts_scheduled (action_id);
|
||||||
|
|
||||||
insert into posts_scheduled
|
insert into posts_scheduled
|
||||||
values (NULL, "modominem", "un message d'example", NULL);
|
values (NULL, "modominem", "un message d'example", NULL);
|
||||||
|
|
||||||
`);
|
`);
|
||||||
console.log('requête de création faite')
|
console.log('requête de création faite')
|
||||||
}
|
}
|
||||||
|
|
||||||
const prepared = prepare();
|
const prepared = prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllPosts(db) {
|
function getAllPosts(db) {
|
||||||
db.query(sql`SELECT * FROM posts_scheduled;`).then(
|
return db.query(sql`SELECT *
|
||||||
(results) =>
|
FROM posts_scheduled;`)
|
||||||
{
|
|
||||||
console.log(results)
|
|
||||||
},
|
|
||||||
(err) => console.error(err),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
router.get('/init-db', function (req, res, next) {
|
router.get('/init-db', function (req, res, next) {
|
||||||
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
||||||
console.log('got to create db')
|
console.log('got to create db')
|
||||||
createDatabase()
|
createDatabase()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
res.render('created_db', {});
|
res.render('created_db', {});
|
||||||
@ -76,57 +70,44 @@ router.get('/init-db', function (req, res, next) {
|
|||||||
|
|
||||||
router.get('/list', function (req, res, next) {
|
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 "
|
||||||
|
// }]
|
||||||
|
|
||||||
var db = new sqlite3.Database('../masto_posts.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
|
getAllPosts(db).then(
|
||||||
if (err && err.code == "SQLITE_CANTOPEN") {
|
(results) => {
|
||||||
createDatabase();
|
console.log( "liste de posts " ,results)
|
||||||
return;
|
res.render('database', {posts_list: results});
|
||||||
} else if (err) {
|
},
|
||||||
console.log("Getting error " + err);
|
(err) => console.error(err),
|
||||||
exit(1);
|
);
|
||||||
}
|
|
||||||
getAllPosts(db);
|
|
||||||
});
|
|
||||||
|
|
||||||
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)
|
|
||||||
res.render('database', {posts_list: posts_list});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
router.get('/add-to-db-example', function (req, res, next) {
|
router.get('/add-to-db-example', function (req, res, next) {
|
||||||
|
let db = connect(database_masto);
|
||||||
|
|
||||||
var db = new sqlite3.Database('../masto_posts.db',
|
async function prepare() {
|
||||||
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
|
await db.query(sql`
|
||||||
(err) => {
|
|
||||||
|
|
||||||
if (err && err.code == "SQLITE_CANTOPEN") {
|
|
||||||
createDatabase();
|
|
||||||
return;
|
|
||||||
} else if (err) {
|
|
||||||
console.log("Getting error " + err);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
db.exec(`
|
|
||||||
insert into posts_scheduled
|
insert into posts_scheduled
|
||||||
values (NULL, "modominem", "un message d'example", "un jour")
|
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 => {
|
||||||
let posts_list = []
|
console.log(err)
|
||||||
res.render('database', {posts_list: posts_list});
|
});
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
// publier le message en db dans la file
|
// publier le message en db dans la file
|
||||||
|
@ -3,7 +3,9 @@ extends layout
|
|||||||
block content
|
block content
|
||||||
h1 Liste des prochains posts prévus
|
h1 Liste des prochains posts prévus
|
||||||
div.posts
|
div.posts
|
||||||
h2 posts_list.length + " posts"
|
h2= posts_list.length + " posts"
|
||||||
// for p in posts_list
|
|
||||||
// span.author = p.post_username
|
for p in posts_list
|
||||||
// span.content = p.content
|
div.post-message
|
||||||
|
strong.author= p.post_username
|
||||||
|
div.content= p.content
|
Loading…
Reference in New Issue
Block a user