ya es capaz de leer de la db y mandarlo a la web

pull/16/head
Borja Robert 2 years ago
parent 493bd9b976
commit 5611c2775b

1
.gitignore vendored

@ -11,3 +11,4 @@ node_modules
*.swp
*/*.swp
/src/lib/db/*.sqlite
/src/lib/db/*.sqlite*

Binary file not shown.

@ -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");

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

@ -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')}`);
// }
// };

@ -1,3 +1,6 @@
<script>
export let data;
console.log(data);
</script>
<h1>Test</h1>

Loading…
Cancel
Save