build ok,

This commit is contained in:
louiscklaw
2025-04-14 10:39:32 +08:00
parent d5f1d4e31e
commit 4768ac65e2
19 changed files with 289 additions and 0 deletions

View File

@@ -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<Response> {
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' },
});
}

View File

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

View File

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

View File

@@ -0,0 +1,5 @@
###
GET http://localhost:3000/api/db/lesson_types/getById/e60v95892466775
###

View File

@@ -0,0 +1,12 @@
import PocketBase from 'pocketbase';
// const { PB_HOSTNAME } = process.env;
export async function GET(): Promise<Response> {
const pb = new PocketBase(`http://localhost:8090`);
const resultList = await pb.collection('LessonsCategories').getList(1, 50, {});
// console.log(resultList);
return Response.json(resultList);
}

View File

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

View File

@@ -0,0 +1,5 @@
export async function GET(): Promise<Response> {
const record = { hello: 'world' };
return Response.json(record);
}

View File

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

View File

@@ -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<Response> {
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' },
});
}

View File

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

View File

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

View File

@@ -0,0 +1,5 @@
###
GET http://localhost:3000/api/db/lesson_types/getById/e60v95892466775
###

View File

@@ -0,0 +1,12 @@
import PocketBase from 'pocketbase';
// const { PB_HOSTNAME } = process.env;
export async function GET(): Promise<Response> {
const pb = new PocketBase(`http://localhost:8090`);
const resultList = await pb.collection('LessonsTypes').getList(1, 50, {});
// console.log(resultList);
return Response.json(resultList);
}

View File

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

View File

@@ -0,0 +1,5 @@
export async function GET(): Promise<Response> {
const record = { hello: 'world' };
return Response.json(record);
}

View File

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

View File

@@ -0,0 +1,9 @@
import PocketBase from 'pocketbase';
export async function GET(): Promise<Response> {
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);
}

View File

@@ -0,0 +1,5 @@
###
GET http://localhost:3000/api/db/lesson_types/list
###

View File

@@ -0,0 +1,5 @@
export const dynamic = 'force-static';
export async function GET(): Promise<Response> {
return Response.json({ hello: 'world' });
}