diff --git a/svar-gantt-app/data.db b/svar-gantt-app/data.db index 7a2ca9a..652f098 100644 Binary files a/svar-gantt-app/data.db and b/svar-gantt-app/data.db differ diff --git a/svar-gantt-app/src/routes/api/planning/+server.ts b/svar-gantt-app/src/routes/api/planning/+server.ts new file mode 100644 index 0000000..dc10212 --- /dev/null +++ b/svar-gantt-app/src/routes/api/planning/+server.ts @@ -0,0 +1,72 @@ +import { json } from '@sveltejs/kit'; +import db from '$lib/server/db'; + +export async function POST({ request }) { + const body = await request.json(); + + try { + if (Array.isArray(body)) { + if (body.length > 1) { + // Cas d’un import CSV → remplacement total + const insert = db.prepare(` + INSERT INTO resource_planning (ressource, profil, date, disponibilite) + VALUES (@ressource, @profil, @date, @disponibilite) + `); + const clear = db.prepare(`DELETE FROM resource_planning`); + const transaction = db.transaction((data: any[]) => { + clear.run(); + for (const entry of data) { + insert.run(entry); + } + }); + + transaction(body); + } else if (body.length === 1) { + // Cas d'une modif manuelle → update ou insert + const { ressource, profil, date, disponibilite } = body[0]; + const existing = db.prepare(` + SELECT id FROM resource_planning + WHERE ressource = ? AND profil = ? AND date = ? + `).get(ressource, profil, date); + + if (existing) { + db.prepare(` + UPDATE resource_planning + SET disponibilite = ? + WHERE id = ? + `).run(disponibilite, existing.id); + } else { + db.prepare(` + INSERT INTO resource_planning (ressource, profil, date, disponibilite) + VALUES (?, ?, ?, ?) + `).run(ressource, profil, date, disponibilite); + } + } + } + + return json({ success: true }); + } catch (e) { + console.error('Erreur POST /api/planning', e); + return json({ success: false, error: e.message }, { status: 500 }); + } +} + +export async function GET() { + const rows = db.prepare(`SELECT * FROM resource_planning`).all(); + + const grouped = new Map(); + + for (const row of rows) { + const key = `${row.ressource}|${row.profil}`; + if (!grouped.has(key)) { + grouped.set(key, { + ressource: row.ressource, + profil: row.profil, + disponibilites: {} + }); + } + grouped.get(key).disponibilites[row.date] = row.disponibilite; + } + + return json(Array.from(grouped.values())); +} diff --git a/svar-gantt-app/src/routes/planning/+page.svelte b/svar-gantt-app/src/routes/planning/+page.svelte index 1312867..cd65228 100644 --- a/svar-gantt-app/src/routes/planning/+page.svelte +++ b/svar-gantt-app/src/routes/planning/+page.svelte @@ -1,5 +1,6 @@