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` + } +}