|
@@ -2,7 +2,8 @@ import Database from 'better-sqlite3'
|
|
|
import { v4 } from 'uuid'
|
|
|
const db = new Database('./cosette.db')
|
|
|
|
|
|
-function load () {
|
|
|
+
|
|
|
+function load() {
|
|
|
db.pragma('journal_mode = WAL')
|
|
|
db.exec('CREATE TABLE IF NOT EXISTS cosette (uuid TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT, tags TEXT, images TEXT, updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP)')
|
|
|
db.exec('CREATE INDEX cosette_updated_at_index ON cosette (updatedAt)')
|
|
@@ -11,7 +12,7 @@ function load () {
|
|
|
// TODO: da gestire in qualche modo all'avvio
|
|
|
// load()
|
|
|
|
|
|
-export function add (cosetta) {
|
|
|
+export function add(cosetta) {
|
|
|
const q = db.prepare('INSERT INTO cosette (uuid, name, description, tags, images) VALUES(:uuid, :name, :description, :tags, :imgs)')
|
|
|
cosetta.uuid = v4()
|
|
|
cosetta.tags = JSON.stringify(cosetta.tags.split(',').map(t => t.toLowerCase().trim()))
|
|
@@ -20,7 +21,7 @@ export function add (cosetta) {
|
|
|
return cosetta
|
|
|
}
|
|
|
|
|
|
-export function get (uuid) {
|
|
|
+export function get(uuid) {
|
|
|
let q = db.prepare('SELECT * from cosette WHERE uuid = ?')
|
|
|
const cosetta = q.get(uuid)
|
|
|
cosetta.tags = cosetta.tags ? JSON.parse(cosetta.tags) : []
|
|
@@ -32,7 +33,7 @@ export function get (uuid) {
|
|
|
return { cosetta, comments }
|
|
|
}
|
|
|
|
|
|
-export function getAll ({ page }) {
|
|
|
+export function getAll({ page }) {
|
|
|
page = Number(page || 0)
|
|
|
const perPage = 3
|
|
|
let query = 'SELECT * FROM cosette ORDER BY updatedAt DESC LIMIT ? OFFSET ?'
|
|
@@ -46,18 +47,18 @@ export function getAll ({ page }) {
|
|
|
|
|
|
const count = db.prepare('SELECT COUNT(*) count from cosette').get().count
|
|
|
|
|
|
- return { cosette, details: { page, count, prev: page !== 0, next: count > perPage*(page+1), n_page: Math.ceil(count / perPage) } }
|
|
|
+ return { cosette, details: { page, count, prev: page !== 0, next: count > perPage * (page + 1), n_page: Math.ceil(count / perPage) } }
|
|
|
}
|
|
|
|
|
|
|
|
|
-export function addComment (comment) {
|
|
|
+export function addComment(comment) {
|
|
|
const q = db.prepare('INSERT INTO chan (uuid, cosetta_uuid, message) VALUES(:uuid, :cosetta_uuid, :message)')
|
|
|
comment.uuid = v4()
|
|
|
q.run(comment)
|
|
|
return comment
|
|
|
}
|
|
|
|
|
|
-export function getComments (cosetta_uuid) {
|
|
|
+export function getComments(cosetta_uuid) {
|
|
|
const q = db.prepare('SELECT * from chan WHERE cosetta_uuid = ?')
|
|
|
return q.all(cosetta_uuid)
|
|
|
-}
|
|
|
+}
|