diff --git a/src/lib/db/db.js b/src/lib/db/db.js index e1bbf28..53f2295 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -1,7 +1,7 @@ /** @typedef {typeof import('better-sqlite3')} better-sqlite3 */ import Database from 'better-sqlite3'; -export const db = new Database('./carteles_dev.sqlite', { fileMustExist: true, verbose: console.log }) +export const db = new Database('./carteles_dev.sqlite', { verbose: console.log }) db.pragma("journal_mode = WAL"); db.pragma("synchronous = normal"); db.pragma("temp_store = memory"); diff --git a/src/lib/db/utils.js b/src/lib/db/utils.js new file mode 100644 index 0000000..8e2685f --- /dev/null +++ b/src/lib/db/utils.js @@ -0,0 +1,50 @@ +import { db } from '$lib/db/db'; + +/** What all actions on DB return +* @typedef {Object} DBActionResult +* @property {string} [error] +* @property {string} [success] +*/ + +/** Adds user to the db +* @param {string} email +* @param {string} passwordHash +* @param {boolean} isAdmin +* @returns {DBActionResult} return +*/ +export const addUserToDB = (email, passwordHash, isAdmin) => { + if (!email || !passwordHash || !isAdmin) { + return { + error: "Either email, password or isAdmin are missing" + }; + } + const addUser = db.prepare(`INSERT INTO Users(email,password,isAdmin) VALUES(?,?,?);`) + const result = addUser.run(email, passwordHash, isAdmin); + if (result.changes === 1) { + return { + success: `User '${email}' added to DB` + } + } + return { error: `Could not add user '${email}' to DB`, }; +} + +/** Removes user from db +* @param {string} email +* @returns {DBActionResult} return +*/ +export const delUserFromDB = (email) => { + if (!email) { + return { + error: "Email not provided" + } + } + const delUser = db.prepare(`DELETE FROM Users WHERE email=${email};`); + const result = delUser.run(); + if (result.changes === 1) { + return { + success: `User '${email}' removed successfully` + } + } + return { error: `Could not remove user '${email}' from DB` } +} + diff --git a/src/lib/preview/Preview.svelte b/src/lib/preview/Preview.svelte index 8011724..96ffffa 100644 --- a/src/lib/preview/Preview.svelte +++ b/src/lib/preview/Preview.svelte @@ -2,7 +2,7 @@ /** @type {number} */ export let templateIndex; /** @type {string} */ export let templateImage; - import { organizedBy, colabs, canvas, multiplier } from '$lib/stores/store'; + import { organizedBy, colabs, canvas } from '$lib/stores/store'; import Heading from '$lib/components/Heading.svelte'; import Title from '$lib/components/Title.svelte';