From 43f2fc78099b481d1af0781b6d2bc05749224f05 Mon Sep 17 00:00:00 2001 From: Borja Robert Date: Mon, 10 Oct 2022 13:41:21 +0200 Subject: [PATCH] =?UTF-8?q?a=C3=B1ade=20funci=C3=B3n=20de=20a=C3=B1adir=20?= =?UTF-8?q?poster=20a=20la=20base=20de=20datos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/db/db.js | 16 ++++++++++++++++ src/lib/db/utils.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/lib/db/db.js b/src/lib/db/db.js index 53f2295..230d49d 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -22,3 +22,19 @@ const createPostersTable = db.prepare(`CREATE TABLE IF NOT EXISTS Posters ( createUserTable.run(); createPostersTable.run(); + +/** +* @typedef {Object} User +* @property {string} [email] +* @property {string} [password] +* @property {boolean} [isAdmin] +*/ + +/** +* @typedef {Object} Poster +* @property {number} [id] +* @property {Blob} [image] +* @property {string} [template] +* @property {string} [name] +* @property {string} [content] Stringified contents +*/ diff --git a/src/lib/db/utils.js b/src/lib/db/utils.js index 5b75f81..cbc99f2 100644 --- a/src/lib/db/utils.js +++ b/src/lib/db/utils.js @@ -50,3 +50,34 @@ export const delUserFromDB = (email) => { return { error: `Could not remove user '${email}' from DB` } } +/** +* @function addPosterToDB +* @param {Blob} image +* @param {string} template +* @param {string} name +* @param {string} content +* @returns {DBActionResult} return +*/ +export const addPosterToDB = (image, template, name = "", content) => { + console.log(`You passed this paramas:\n image: ${image}\n template: ${template}\n name: ${name}\n content: ${content}`); + + const id = crypto.randomUUID(); // Create random ID for Poster + + if (!image || !template || !content) { // Return error if mandatory info is missing + return { + error: "Image, template or content missing" + } + } + + const addPoster = db.prepare(`INSERT INTO Posters(id,image,template,name,content) VALUES (?,?,?,?,?);`); + const result = addPoster.run(id, image, template, name, content); + + if (result.changes === 1) { + return { + success: `Poster added to DB with id ${id}` + } + } + return { // Default return just in case + error: `Could not save poster '${id}' to DB. Something went wrong` + } +}