añade función de añadir poster a la base de datos

pull/16/head
Borja Robert 2 years ago
parent 1cf47415c0
commit 43f2fc7809

@ -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
*/

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

Loading…
Cancel
Save