2022-08-11 09:57:51 +02:00
import Database from 'better-sqlite3'
import { v4 } from 'uuid'
2022-08-18 09:44:51 +02:00
2022-08-11 09:57:51 +02:00
const db = new Database ( './cosette.db' )
2022-08-16 14:43:33 +02:00
2022-08-22 17:18:00 +02:00
export function load ( ) {
2022-08-11 09:57:51 +02:00
db . pragma ( 'journal_mode = WAL' )
2022-08-12 18:23:03 +02:00
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)' )
2022-08-22 17:18:00 +02:00
db . exec ( 'CREATE INDEX IF NOT EXISTS cosette_updated_at_index ON cosette (updatedAt)' )
2022-08-12 18:23:03 +02:00
db . exec ( 'CREATE TABLE IF NOT EXISTS chan (uuid TEXT PRIMARY KEY, cosetta_uuid REFERENCES cosette(uuid), message TEXT, updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP)' )
2022-08-11 09:57:51 +02:00
}
2022-08-16 14:43:33 +02:00
export function add ( cosetta ) {
2022-08-23 23:26:57 +02:00
const q = db . prepare ( 'INSERT INTO cosette (uuid, name, description, tags, images) VALUES(:uuid, :name, :description, :tags, :images)' )
2022-08-11 09:57:51 +02:00
cosetta . uuid = v4 ( )
2022-08-25 17:03:17 +02:00
cosetta . tags = JSON . stringify (
cosetta . tags . split ( ',' )
. map ( t => t . toLowerCase ( ) . trim ( ) )
. filter ( t => t ) ) // tolgo i vuoti
2022-08-23 23:26:57 +02:00
cosetta . images = JSON . stringify ( cosetta . images )
2022-08-11 09:57:51 +02:00
q . run ( cosetta )
return cosetta
}
2022-08-16 14:43:33 +02:00
export function get ( uuid ) {
2022-08-12 18:23:03 +02:00
let q = db . prepare ( 'SELECT * from cosette WHERE uuid = ?' )
const cosetta = q . get ( uuid )
cosetta . tags = cosetta . tags ? JSON . parse ( cosetta . tags ) : [ ]
cosetta . images = cosetta . images ? JSON . parse ( cosetta . images ) : [ ]
q = db . prepare ( 'SELECT message, updatedAt FROM chan WHERE cosetta_uuid = ? ORDER BY updatedAt desc' )
const comments = q . all ( uuid )
return { cosetta , comments }
2022-08-11 09:57:51 +02:00
}
2022-08-16 14:43:33 +02:00
export function getAll ( { page } ) {
2022-08-16 00:57:18 +02:00
page = Number ( page || 0 )
const perPage = 3
let query = 'SELECT * FROM cosette ORDER BY updatedAt DESC LIMIT ? OFFSET ?'
const q = db . prepare ( query )
const ret = q . all ( perPage , perPage * page )
const cosette = ret . map ( r => {
2022-08-12 18:23:03 +02:00
r . tags = r . tags ? JSON . parse ( r . tags ) : [ ]
r . images = r . images ? JSON . parse ( r . images ) : [ ]
2022-08-11 09:57:51 +02:00
return r
} )
2022-08-16 00:57:18 +02:00
const count = db . prepare ( 'SELECT COUNT(*) count from cosette' ) . get ( ) . count
2022-08-16 14:43:33 +02:00
return { cosette , details : { page , count , prev : page !== 0 , next : count > perPage * ( page + 1 ) , n _page : Math . ceil ( count / perPage ) } }
2022-08-11 09:57:51 +02:00
}
2022-08-12 18:23:03 +02:00
2022-08-16 14:43:33 +02:00
export function addComment ( comment ) {
2022-08-12 18:23:03 +02:00
const q = db . prepare ( 'INSERT INTO chan (uuid, cosetta_uuid, message) VALUES(:uuid, :cosetta_uuid, :message)' )
comment . uuid = v4 ( )
q . run ( comment )
return comment
}
2022-08-16 14:43:33 +02:00
export function getComments ( cosetta _uuid ) {
2022-08-12 18:23:03 +02:00
const q = db . prepare ( 'SELECT * from chan WHERE cosetta_uuid = ?' )
return q . all ( cosetta _uuid )
2022-08-16 14:43:33 +02:00
}
2022-08-22 16:03:08 +02:00
2022-08-23 23:26:57 +02:00
export function getComment ( uuid ) {
2022-08-22 16:03:08 +02:00
const q = db . prepare ( 'SELECT * from chan WHERE uuid = ?' )
const comment = q . get ( uuid )
return comment
}