diff --git a/src/lib/db/carteles_dev.sqlite b/src/lib/db/carteles_dev.sqlite index cb0d28e..f7e68bc 100644 Binary files a/src/lib/db/carteles_dev.sqlite and b/src/lib/db/carteles_dev.sqlite differ diff --git a/src/lib/db/db.js b/src/lib/db/db.js index dcaff01..370aa16 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -14,9 +14,8 @@ const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS Users ( const createPostersTable = db.prepare(`CREATE TABLE IF NOT EXISTS Posters ( id INTEGER PRIMARY KEY UNIQUE NOT NULL, - image BLOB, - template TEXT, - name TEXT, + createdAt TEXT NOT NULL, + image BLOB NOT NULL, content TEXT );`); @@ -33,8 +32,7 @@ createPostersTable.run(); /** * @typedef {Object} Poster * @property {number} [id] +* @property {string} [createdAt] Date * @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 de5ddb4..6f2797f 100644 --- a/src/lib/db/utils.js +++ b/src/lib/db/utils.js @@ -4,6 +4,7 @@ import { db } from '$lib/db/db.js'; * @typedef {Object} ChangeDBResult * @property {string} [error] * @property {string} [success] +* @property {string} [id] */ /** @@ -51,29 +52,35 @@ export const delUserFromDB = (email) => { /** * @function addPosterToDB -* @param {Blob} image -* @param {string} template -* @param {string} name +* @param {any} image * @param {string} content * @returns {ChangeDBResult} return */ -export const addPosterToDB = (image, template, name, content) => { +export const addPosterToDB = (image, content) => { const id = crypto.randomUUID(); // Create random ID for Poster console.log("New id is: ", id); - if (!image || !template || !content) { // Return error if mandatory info is missing + const createdAt = Date.now().toString; + + if (!image) { // Return error if mandatory info is missing return { - error: "Image, template or content missing" + error: "Image missing" } } - const addPoster = db.prepare(`INSERT INTO Posters(id,image,template,name,content) VALUES (?,?,?,?,?);`); - const result = addPoster.run(id, image, template, name, content); + const addPoster = db.prepare(`INSERT INTO Posters(id,createdAt,image,content) VALUES ($id,$createdAt,$image,$content);`); + const result = addPoster.run({ + id: id, + createdAt: createdAt, + image: image, + content: content + }); if (result.changes === 1) { return { - success: `Poster added to DB with id ${id}` + success: `Poster added to DB with id ${id}`, + id } } return { // Default return just in case diff --git a/src/lib/form/Form.svelte b/src/lib/form/Form.svelte index 6b1906b..7d3a374 100644 --- a/src/lib/form/Form.svelte +++ b/src/lib/form/Form.svelte @@ -37,10 +37,8 @@ const img = await html2canvas($canvas, { scale: 2 }); const image = img.toDataURL('image/png'); const data = new FormData(); - data.set('image', image); - data.set('name', 'test'); - data.set('template', 'verde'); - data.set( + data.append('image', image); + data.append( 'content', JSON.stringify({ title: $title, @@ -54,16 +52,12 @@ city: $city }) ); - console.log(data.get('image')); - - // const req = await fetch('/admin', { - // method: 'POST', - // headers: { - // 'content-type': 'multipart/form-data', - // accept: 'application/json' - // }, - // body: data - // }); + + const req = await fetch('/admin', { + method: 'POST', + body: data + }); + console.log(req); } }; diff --git a/src/routes/admin/+page.server.js b/src/routes/admin/+page.server.js index a297b03..793c3af 100644 --- a/src/routes/admin/+page.server.js +++ b/src/routes/admin/+page.server.js @@ -1,5 +1,7 @@ import { error } from '@sveltejs/kit'; -import { canvas } from '$lib/stores/store'; +import fs from 'fs'; +import stream from 'stream'; +// import { canvas } from '$lib/stores/store'; import { getAllPostersFromDB, addPosterToDB } from '$lib/db/utils'; /** @type {import('./$types').PageServerLoad} */ @@ -10,7 +12,6 @@ export const load = () => { posters } } - console.log(posters); throw error(404, "error"); }; @@ -18,7 +19,15 @@ export const load = () => { export const actions = { default: async ({ request }) => { console.log("Estoy aquĆ­"); + const path = crypto.randomUUID(); const data = await request.formData(); + const image = data.get("image")?.toString(); + const content = data.get("content"); + fs.writeFileSync(`./static/${path}.png`, image, 'base64url'); + // const savetoDB = addPosterToDB(image, content); + return { + success: true + } } }