diff --git a/002_source/cms/src/app/api/db/lesson_categories/create/route.ts b/002_source/cms/src/app/api/db/lesson_categories/create/route.ts new file mode 100644 index 0000000..ebb04c5 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/create/route.ts @@ -0,0 +1,26 @@ +import PocketBase from 'pocketbase'; + +const pb = new PocketBase(`http://localhost:8090`); +const COLLECTION_NAME = 'LessonsTypes'; + +interface LessonTypeCreate { + name: string; + type: string; + pos: number; + visible: string; +} + +interface ReqLessonTypeCreate { + data: LessonTypeCreate; +} + +// POST - Create a new lesson type +export async function POST(request: Request): Promise { + const { data } = (await request.json()) as ReqLessonTypeCreate; + const record = await pb.collection(COLLECTION_NAME).create(data); + + return new Response(JSON.stringify(record), { + status: 201, + headers: { 'Content-Type': 'application/json' }, + }); +} diff --git a/002_source/cms/src/app/api/db/lesson_categories/delete/route.ts b/002_source/cms/src/app/api/db/lesson_categories/delete/route.ts new file mode 100644 index 0000000..1b7f929 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/delete/route.ts @@ -0,0 +1,19 @@ +// please keep the comment and update to perform delete operation +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function DELETE(request: Request) { + try { + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const { id } = await request.json(); + await pb.collection('LessonsTypes').delete(id); + + return new Response('Record deleted successfully', { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.error(error); + return new Response('Failed to delete record', { status: 500 }); + } +} diff --git a/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/route.ts b/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/route.ts new file mode 100644 index 0000000..fe84906 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/route.ts @@ -0,0 +1,31 @@ +// https://nextjs.org/blog/building-apis-with-nextjs +import { NextRequest } from 'next/server'; +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { + const id = (await params).id; + + const fields = ['id', 'name', 'type', 'visible', 'pos'].join(','); + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const lessonType = await pb.collection('LessonsTypes').getOne(id, { fields }); + + return new Response(JSON.stringify(lessonType), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); +} + +export async function POST(request: Request) { + // Parse the request body + const body = await request.json(); + const { name } = body; + + // e.g. Insert new user into your DB + const newUser = { id: Date.now(), name }; + + return new Response(JSON.stringify(newUser), { + status: 201, + headers: { 'Content-Type': 'application/json' }, + }); +} diff --git a/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/test.http b/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/test.http new file mode 100644 index 0000000..ae7f1fb --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/getById/[id]/test.http @@ -0,0 +1,5 @@ +### + +GET http://localhost:3000/api/db/lesson_types/getById/e60v95892466775 + +### diff --git a/002_source/cms/src/app/api/db/lesson_categories/list/route.ts b/002_source/cms/src/app/api/db/lesson_categories/list/route.ts new file mode 100644 index 0000000..7a86af7 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/list/route.ts @@ -0,0 +1,12 @@ +import PocketBase from 'pocketbase'; + +// const { PB_HOSTNAME } = process.env; + +export async function GET(): Promise { + const pb = new PocketBase(`http://localhost:8090`); + const resultList = await pb.collection('LessonsCategories').getList(1, 50, {}); + + // console.log(resultList); + + return Response.json(resultList); +} diff --git a/002_source/cms/src/app/api/db/lesson_categories/list/test.http b/002_source/cms/src/app/api/db/lesson_categories/list/test.http new file mode 100644 index 0000000..1e4a5ff --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/list/test.http @@ -0,0 +1,17 @@ +### + +GET http://localhost:3000/api/db/lesson_categories/list + +Content-Type: application/json +cache: no-store +cache-control: no-cache1 + +### + +GET http://localhost:8090/api/collections/LessonsCategories/records + +### + +POST http://localhost:3000/api/lesson_categories/helloworld + +Content-Type: application/json diff --git a/002_source/cms/src/app/api/db/lesson_categories/test/route.ts b/002_source/cms/src/app/api/db/lesson_categories/test/route.ts new file mode 100644 index 0000000..c6c67ab --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/test/route.ts @@ -0,0 +1,5 @@ +export async function GET(): Promise { + const record = { hello: 'world' }; + + return Response.json(record); +} diff --git a/002_source/cms/src/app/api/db/lesson_categories/update/route.ts b/002_source/cms/src/app/api/db/lesson_categories/update/route.ts new file mode 100644 index 0000000..a905e31 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_categories/update/route.ts @@ -0,0 +1,20 @@ +import { NextRequest } from 'next/server'; +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function PUT(request: NextRequest) { + try { + const { id, data } = await request.json(); + + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const updatedRecord = await pb.collection('LessonsTypes').update(id, data); + + return new Response(JSON.stringify(updatedRecord), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.error(error); + return new Response('Failed to update record', { status: 500 }); + } +} diff --git a/002_source/cms/src/app/api/db/lesson_types/create/route.ts b/002_source/cms/src/app/api/db/lesson_types/create/route.ts new file mode 100644 index 0000000..ebb04c5 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/create/route.ts @@ -0,0 +1,26 @@ +import PocketBase from 'pocketbase'; + +const pb = new PocketBase(`http://localhost:8090`); +const COLLECTION_NAME = 'LessonsTypes'; + +interface LessonTypeCreate { + name: string; + type: string; + pos: number; + visible: string; +} + +interface ReqLessonTypeCreate { + data: LessonTypeCreate; +} + +// POST - Create a new lesson type +export async function POST(request: Request): Promise { + const { data } = (await request.json()) as ReqLessonTypeCreate; + const record = await pb.collection(COLLECTION_NAME).create(data); + + return new Response(JSON.stringify(record), { + status: 201, + headers: { 'Content-Type': 'application/json' }, + }); +} diff --git a/002_source/cms/src/app/api/db/lesson_types/delete/route.ts b/002_source/cms/src/app/api/db/lesson_types/delete/route.ts new file mode 100644 index 0000000..1b7f929 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/delete/route.ts @@ -0,0 +1,19 @@ +// please keep the comment and update to perform delete operation +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function DELETE(request: Request) { + try { + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const { id } = await request.json(); + await pb.collection('LessonsTypes').delete(id); + + return new Response('Record deleted successfully', { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.error(error); + return new Response('Failed to delete record', { status: 500 }); + } +} diff --git a/002_source/cms/src/app/api/db/lesson_types/getById/[id]/route.ts b/002_source/cms/src/app/api/db/lesson_types/getById/[id]/route.ts new file mode 100644 index 0000000..fe84906 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/getById/[id]/route.ts @@ -0,0 +1,31 @@ +// https://nextjs.org/blog/building-apis-with-nextjs +import { NextRequest } from 'next/server'; +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { + const id = (await params).id; + + const fields = ['id', 'name', 'type', 'visible', 'pos'].join(','); + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const lessonType = await pb.collection('LessonsTypes').getOne(id, { fields }); + + return new Response(JSON.stringify(lessonType), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); +} + +export async function POST(request: Request) { + // Parse the request body + const body = await request.json(); + const { name } = body; + + // e.g. Insert new user into your DB + const newUser = { id: Date.now(), name }; + + return new Response(JSON.stringify(newUser), { + status: 201, + headers: { 'Content-Type': 'application/json' }, + }); +} diff --git a/002_source/cms/src/app/api/db/lesson_types/getById/[id]/test.http b/002_source/cms/src/app/api/db/lesson_types/getById/[id]/test.http new file mode 100644 index 0000000..ae7f1fb --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/getById/[id]/test.http @@ -0,0 +1,5 @@ +### + +GET http://localhost:3000/api/db/lesson_types/getById/e60v95892466775 + +### diff --git a/002_source/cms/src/app/api/db/lesson_types/list/route.ts b/002_source/cms/src/app/api/db/lesson_types/list/route.ts new file mode 100644 index 0000000..f07a0e9 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/list/route.ts @@ -0,0 +1,12 @@ +import PocketBase from 'pocketbase'; + +// const { PB_HOSTNAME } = process.env; + +export async function GET(): Promise { + const pb = new PocketBase(`http://localhost:8090`); + const resultList = await pb.collection('LessonsTypes').getList(1, 50, {}); + + // console.log(resultList); + + return Response.json(resultList); +} diff --git a/002_source/cms/src/app/api/db/lesson_types/list/test.http b/002_source/cms/src/app/api/db/lesson_types/list/test.http new file mode 100644 index 0000000..6910602 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/list/test.http @@ -0,0 +1,17 @@ +### + +GET http://localhost:3000/api/db/lesson_types/list + +Content-Type: application/json +cache: no-store +cache-control: no-cache1 + +### + +GET http://localhost:8090/api/collections/LessonsTypes/records + +### + +POST http://localhost:3000/api/lesson_types/helloworld + +Content-Type: application/json diff --git a/002_source/cms/src/app/api/db/lesson_types/test/route.ts b/002_source/cms/src/app/api/db/lesson_types/test/route.ts new file mode 100644 index 0000000..c6c67ab --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/test/route.ts @@ -0,0 +1,5 @@ +export async function GET(): Promise { + const record = { hello: 'world' }; + + return Response.json(record); +} diff --git a/002_source/cms/src/app/api/db/lesson_types/update/route.ts b/002_source/cms/src/app/api/db/lesson_types/update/route.ts new file mode 100644 index 0000000..a905e31 --- /dev/null +++ b/002_source/cms/src/app/api/db/lesson_types/update/route.ts @@ -0,0 +1,20 @@ +import { NextRequest } from 'next/server'; +import PocketBase from 'pocketbase'; + +const { PB_HOSTNAME } = process.env; +export async function PUT(request: NextRequest) { + try { + const { id, data } = await request.json(); + + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const updatedRecord = await pb.collection('LessonsTypes').update(id, data); + + return new Response(JSON.stringify(updatedRecord), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); + } catch (error) { + console.error(error); + return new Response('Failed to update record', { status: 500 }); + } +} diff --git a/002_source/cms/src/app/api/db/vocabularies/list/route.ts b/002_source/cms/src/app/api/db/vocabularies/list/route.ts new file mode 100644 index 0000000..ccff16f --- /dev/null +++ b/002_source/cms/src/app/api/db/vocabularies/list/route.ts @@ -0,0 +1,9 @@ +import PocketBase from 'pocketbase'; + +export async function GET(): Promise { + const { PB_HOSTNAME } = process.env; + const pb = new PocketBase(`http://${PB_HOSTNAME}:8090`); + const resultList = await pb.collection('Vocabularies').getList(1, 50, {}); + + return Response.json(resultList); +} diff --git a/002_source/cms/src/app/api/db/vocabularies/list/test.http b/002_source/cms/src/app/api/db/vocabularies/list/test.http new file mode 100644 index 0000000..33999ce --- /dev/null +++ b/002_source/cms/src/app/api/db/vocabularies/list/test.http @@ -0,0 +1,5 @@ +### + +GET http://localhost:3000/api/db/lesson_types/list + +### diff --git a/002_source/cms/src/app/api/helloworld/route.ts b/002_source/cms/src/app/api/helloworld/route.ts new file mode 100644 index 0000000..97d76cd --- /dev/null +++ b/002_source/cms/src/app/api/helloworld/route.ts @@ -0,0 +1,5 @@ +export const dynamic = 'force-static'; + +export async function GET(): Promise { + return Response.json({ hello: 'world' }); +}