diff --git a/.gitignore b/.gitignore index a3e11e8..7996add 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules *.swp */*.swp /src/lib/db/*.sqlite +/src/lib/db/*.sqlite* diff --git a/src/lib/db/carteles_dev.sqlite b/src/lib/db/carteles_dev.sqlite index 7f0c286..cb0d28e 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 230d49d..dcaff01 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', { verbose: console.log }) +export const db = new Database('./src/lib/db/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 index cbc99f2..47a33a5 100644 --- a/src/lib/db/utils.js +++ b/src/lib/db/utils.js @@ -2,7 +2,7 @@ import { db } from '$lib/db/db'; /** * Return of functions that change change DB, either 'success' or 'failure' with explanation -* @typedef {Object} DBActionResult +* @typedef {Object} ChangeDBResult * @property {string} [error] * @property {string} [success] */ @@ -12,7 +12,7 @@ import { db } from '$lib/db/db'; * @param {string} email * @param {string} passwordHash * @param {boolean} isAdmin -* @returns {DBActionResult} return +* @returns {ChangeDBResult} return */ export const addUserToDB = (email, passwordHash, isAdmin) => { if (!email || !passwordHash || !isAdmin) { @@ -32,7 +32,7 @@ export const addUserToDB = (email, passwordHash, isAdmin) => { /** Removes user from db * @param {string} email -* @returns {DBActionResult} return +* @returns {ChangeDBResult} return */ export const delUserFromDB = (email) => { if (!email) { @@ -56,7 +56,7 @@ export const delUserFromDB = (email) => { * @param {string} template * @param {string} name * @param {string} content -* @returns {DBActionResult} return +* @returns {ChangeDBResult} 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}`); @@ -81,3 +81,10 @@ export const addPosterToDB = (image, template, name = "", content) => { error: `Could not save poster '${id}' to DB. Something went wrong` } } + +export const getAllPostersFromDB = () => { + const getPosters = db.prepare(`SELECT * FROM Posters;`); + const result = getPosters.all(); + console.log("resultado en utils", result); + return result; +} diff --git a/src/routes/admin/+page.server.js b/src/routes/admin/+page.server.js index 3b7fb48..1d769fc 100644 --- a/src/routes/admin/+page.server.js +++ b/src/routes/admin/+page.server.js @@ -1,38 +1,43 @@ import { error } from '@sveltejs/kit'; +import { getAllPostersFromDB } from '$lib/db/utils'; /** @type {import('./$types').PageServerLoad} */ -export const load = async ({ locals }) => { - - +export const load = async () => { + const posters = getAllPostersFromDB(); + if (posters !== undefined) { + return { + posters: posters + } + } throw error(404, "error"); }; -/** @type {import('./$types').Actions} */ -export const actions = { - add: async ({ request, locals }) => { - const form = await request.formData(); - - await api('POST', `todos/${locals.userid}`, { - text: form.get('text') - }); - }, - edit: async ({ request, locals }) => { - const form = await request.formData(); - - await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { - text: form.get('text') - }); - }, - toggle: async ({ request, locals }) => { - const form = await request.formData(); - - await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { - done: !!form.get('done') - }); - }, - delete: async ({ request, locals }) => { - const form = await request.formData(); - - await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`); - } -}; +// /** @type {import('./$types').Actions} */ +// export const actions = { +// add: async ({ request, locals }) => { +// const form = await request.formData(); + +// await api('POST', `todos/${locals.userid}`, { +// text: form.get('text') +// }); +// }, +// edit: async ({ request, locals }) => { +// const form = await request.formData(); + +// await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { +// text: form.get('text') +// }); +// }, +// toggle: async ({ request, locals }) => { +// const form = await request.formData(); + +// await api('PATCH', `todos/${locals.userid}/${form.get('uid')}`, { +// done: !!form.get('done') +// }); +// }, +// delete: async ({ request, locals }) => { +// const form = await request.formData(); + +// await api('DELETE', `todos/${locals.userid}/${form.get('uid')}`); +// } +// }; diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index e6e9ef8..9b064ab 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -1,3 +1,6 @@ + +

Test