diff --git a/app/api/carnet/[date]/route.ts b/app/api/carnet/[date]/route.ts index a8c59f92..001125bc 100644 --- a/app/api/carnet/[date]/route.ts +++ b/app/api/carnet/[date]/route.ts @@ -15,6 +15,9 @@ export async function GET( ); } + const { searchParams } = new URL(request.url); + const category = searchParams.get('category') || 'Notes'; + const service = await getNextCloudService(); const date = new Date(params.date); @@ -25,7 +28,7 @@ export async function GET( ); } - const content = await service.getNote(session.user.email, date); + const content = await service.getNote(session.user.email, category, date); if (!content) { return NextResponse.json( @@ -34,7 +37,7 @@ export async function GET( ); } - return NextResponse.json({ content }); + return NextResponse.json({ content, category }); } catch (error) { console.error('Failed to get note:', error); return NextResponse.json( diff --git a/app/api/carnet/route.ts b/app/api/carnet/route.ts index 0580a161..eb2c1c02 100644 --- a/app/api/carnet/route.ts +++ b/app/api/carnet/route.ts @@ -12,8 +12,11 @@ export async function GET(request: Request) { ); } + const { searchParams } = new URL(request.url); + const category = searchParams.get('category'); + const service = await getNextCloudService(); - const notes = await service.listNotes(session.user.email); + const notes = await service.listNotes(session.user.email, category || undefined); return NextResponse.json({ notes }); } catch (error) { @@ -36,15 +39,16 @@ export async function POST(request: Request) { } const service = await getNextCloudService(); - const { content, date } = await request.json(); + const { content, date, category = 'Notes' } = await request.json(); - const fileName = await service.saveNote( + const result = await service.saveNote( session.user.email, content, + category, date ? new Date(date) : undefined ); - return NextResponse.json({ fileName }); + return NextResponse.json(result); } catch (error) { console.error('Failed to save note:', error); return NextResponse.json( diff --git a/lib/nextcloud.ts b/lib/nextcloud.ts index 1f6423cc..561994a9 100644 --- a/lib/nextcloud.ts +++ b/lib/nextcloud.ts @@ -4,6 +4,7 @@ import { getSession } from 'next-auth/react'; export class NextCloudService { private webdav: WebDAV; private basePath: string = '/Personal/Carnet'; + private subFolders: string[] = ['Journal', 'Santé', 'Notes']; constructor(token: string) { this.webdav = new WebDAV( @@ -16,28 +17,56 @@ export class NextCloudService { ); } - async ensureCarnetFolder(username: string) { - const userPath = `${username}${this.basePath}`; + async initializeUserFolders(username: string) { + const userBasePath = `${username}${this.basePath}`; + + // Create base Carnet folder try { - await this.webdav.exists(userPath); - } catch { - await this.webdav.createDirectory(userPath, { recursive: true }); + const baseExists = await this.webdav.exists(userBasePath); + if (!baseExists) { + await this.webdav.createDirectory(userBasePath, { recursive: true }); + } + } catch (error) { + console.error('Error checking/creating base folder:', error); + throw new Error('Failed to initialize base folder'); + } + + // Create subfolders + for (const folder of this.subFolders) { + const folderPath = `${userBasePath}/${folder}`; + try { + const exists = await this.webdav.exists(folderPath); + if (!exists) { + await this.webdav.createDirectory(folderPath, { recursive: true }); + } + } catch (error) { + console.error(`Error checking/creating ${folder} folder:`, error); + throw new Error(`Failed to initialize ${folder} folder`); + } } } - async saveNote(username: string, content: string, date: Date = new Date()) { + async saveNote(username: string, content: string, category: string = 'Notes', date: Date = new Date()) { + if (!this.subFolders.includes(category)) { + throw new Error(`Invalid category: ${category}`); + } + const fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`; - const filePath = `${username}${this.basePath}/${fileName}`; + const filePath = `${username}${this.basePath}/${category}/${fileName}`; - await this.ensureCarnetFolder(username); + await this.initializeUserFolders(username); await this.webdav.putFileContents(filePath, content, { overwrite: true }); - return fileName; + return { fileName, category }; } - async getNote(username: string, date: Date) { + async getNote(username: string, category: string, date: Date) { + if (!this.subFolders.includes(category)) { + throw new Error(`Invalid category: ${category}`); + } + const fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`; - const filePath = `${username}${this.basePath}/${fileName}`; + const filePath = `${username}${this.basePath}/${category}/${fileName}`; try { const content = await this.webdav.getFileContents(filePath, { format: 'text' }); @@ -50,17 +79,34 @@ export class NextCloudService { } } - async listNotes(username: string) { + async listNotes(username: string, category?: string) { + await this.initializeUserFolders(username); + const userPath = `${username}${this.basePath}`; + const results = []; + try { - const files = await this.webdav.getDirectoryContents(userPath); - return files - .filter(file => file.basename.endsWith('.md')) - .map(file => ({ - date: this.fileNameToDate(file.basename), - name: file.basename, - path: file.filename - })); + if (category) { + if (!this.subFolders.includes(category)) { + throw new Error(`Invalid category: ${category}`); + } + const folderPath = `${userPath}/${category}`; + const files = await this.webdav.getDirectoryContents(folderPath); + results.push(...this.processFiles(files, category)); + } else { + for (const folder of this.subFolders) { + const folderPath = `${userPath}/${folder}`; + try { + const files = await this.webdav.getDirectoryContents(folderPath); + results.push(...this.processFiles(files, folder)); + } catch (error) { + if (error.response?.status !== 404) { + throw error; + } + } + } + } + return results; } catch (error) { if (error.response?.status === 404) { return []; @@ -69,6 +115,17 @@ export class NextCloudService { } } + private processFiles(files: any[], category: string) { + return files + .filter(file => file.basename.endsWith('.md')) + .map(file => ({ + date: this.fileNameToDate(file.basename), + name: file.basename, + path: file.filename, + category + })); + } + private fileNameToDate(fileName: string): Date { const [year, month, day] = fileName.split('.')[0].split('-').map(Number); return new Date(year, month - 1, day);