From fc6ed533e2532557b3b2e800e12d5272c471bb9d Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Mon, 2 Jun 2025 19:42:32 +0800 Subject: [PATCH] feat: implement helloworld API with CRUD operations and test cases --- .../src/app/api/helloworld/_GUIDELINES.md | 22 ++++++ .../src/app/api/helloworld/list/route.ts | 16 ----- .../src/app/api/helloworld/route.ts | 71 ++++++++++++++++++- .../src/app/api/helloworld/test.http | 23 +++++- .../src/app/api/helloworld/utils/response.ts | 0 .../src/app/services/helloworld.service.ts | 44 ++++++++++++ 6 files changed, 158 insertions(+), 18 deletions(-) create mode 100644 03_source/cms_backend/src/app/api/helloworld/_GUIDELINES.md delete mode 100644 03_source/cms_backend/src/app/api/helloworld/list/route.ts delete mode 100644 03_source/cms_backend/src/app/api/helloworld/utils/response.ts create mode 100644 03_source/cms_backend/src/app/services/helloworld.service.ts diff --git a/03_source/cms_backend/src/app/api/helloworld/_GUIDELINES.md b/03_source/cms_backend/src/app/api/helloworld/_GUIDELINES.md new file mode 100644 index 0000000..199ab1c --- /dev/null +++ b/03_source/cms_backend/src/app/api/helloworld/_GUIDELINES.md @@ -0,0 +1,22 @@ +# GUIDELINE + +this is a helloworld api endpoint +this is a demo to handle helloworld record + +## `route.ts` + +handle `GET`, `POST`, `PUT`, `DELETE` + +## `test.http` + +store test request + +## `../../services/helloworld.service.ts` + +helloworld schema CRUD handler + +`listHelloworlds` - list helloworld record +`getHelloworld` - get helloworld record by id +`createNewHelloworld` - create helloworld record +`updateHelloworld` - update helloworld record by id +`deleteHelloworld` - delete helloworld record by id diff --git a/03_source/cms_backend/src/app/api/helloworld/list/route.ts b/03_source/cms_backend/src/app/api/helloworld/list/route.ts deleted file mode 100644 index cfa0861..0000000 --- a/03_source/cms_backend/src/app/api/helloworld/list/route.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { NextRequest, NextResponse } from 'next/server'; - -import { STATUS, response, handleError } from 'src/utils/response'; - -import prisma from '../../../lib/prisma'; - -export async function GET(req: NextRequest, res: NextResponse) { - try { - const products = await prisma.productItem.findMany(); - console.log({ products }); - - return response({ products }, STATUS.OK); - } catch (error) { - return handleError('Post - Get latest', error); - } -} diff --git a/03_source/cms_backend/src/app/api/helloworld/route.ts b/03_source/cms_backend/src/app/api/helloworld/route.ts index bc90716..b8a5daf 100644 --- a/03_source/cms_backend/src/app/api/helloworld/route.ts +++ b/03_source/cms_backend/src/app/api/helloworld/route.ts @@ -2,10 +2,79 @@ import type { NextRequest, NextResponse } from 'next/server'; import { STATUS, response, handleError } from 'src/utils/response'; +import { listHelloworlds, deleteHelloworld, updateHelloworld, createNewHelloworld } from 'src/app/services/helloworld.service'; + +// import prisma from '../../lib/prisma'; + export async function GET(req: NextRequest, res: NextResponse) { try { - return response({ hello: 'world' }, STATUS.OK); + const result = await listHelloworlds(); + + return response(result, STATUS.OK); } catch (error) { return handleError('Post - Get latest', error); } } + +/** + *************************************** + * POST - create Helloworld + *************************************** + */ +export async function POST(req: NextRequest) { + const { data } = await req.json(); + + try { + const createResult = await createNewHelloworld(data); + + return response(createResult, STATUS.OK); + } catch (error) { + return handleError('Helloworld - Create', error); + } +} + +/** + *************************************** + * PUT - update Helloworld + *************************************** + */ +export async function PUT(req: NextRequest) { + const { searchParams } = req.nextUrl; + const helloworldId = searchParams.get('helloworldId'); + + const { data } = await req.json(); + + try { + if (!helloworldId) throw new Error('helloworldId cannot null'); + const id: number = parseInt(helloworldId); + + const updateResult = await updateHelloworld(id, data); + + return response(updateResult, STATUS.OK); + } catch (error) { + return handleError('Helloworld - Update', error); + } +} + +/** + *************************************** + * DELETE - update Helloworld + *************************************** + */ +export async function DELETE(req: NextRequest) { + const { searchParams } = req.nextUrl; + const helloworldId = searchParams.get('helloworldId'); + + const { data } = await req.json(); + + try { + if (!helloworldId) throw new Error('helloworldId cannot null'); + const id: number = parseInt(helloworldId); + + const deleteResult = await deleteHelloworld(id); + + return response(deleteResult, STATUS.OK); + } catch (error) { + return handleError('Helloworld - Update', error); + } +} diff --git a/03_source/cms_backend/src/app/api/helloworld/test.http b/03_source/cms_backend/src/app/api/helloworld/test.http index 5b69f09..2ef9402 100644 --- a/03_source/cms_backend/src/app/api/helloworld/test.http +++ b/03_source/cms_backend/src/app/api/helloworld/test.http @@ -1,4 +1,25 @@ ### - GET http://localhost:7272/api/helloworld +### +GET http://localhost:7272/api/helloworld?helloworldId=1 + +### +POST http://localhost:7272/api/helloworld?helloworldId=1 +content-type: application/json + +{ + "data":{"hello": "hell"} +} + +### +PUT http://localhost:7272/api/helloworld?helloworldId=1 +content-type: application/json + +{ + "data": {"hello": "hell"} +} + + +### +DELETE http://localhost:7272/api/helloworld?helloworldId=1 diff --git a/03_source/cms_backend/src/app/api/helloworld/utils/response.ts b/03_source/cms_backend/src/app/api/helloworld/utils/response.ts deleted file mode 100644 index e69de29..0000000 diff --git a/03_source/cms_backend/src/app/services/helloworld.service.ts b/03_source/cms_backend/src/app/services/helloworld.service.ts new file mode 100644 index 0000000..2dbec32 --- /dev/null +++ b/03_source/cms_backend/src/app/services/helloworld.service.ts @@ -0,0 +1,44 @@ +// src/app/services/helloworld.service.ts +// +// REQ0047/T.B.A. +// +// PURPOSE: +// - helloworld example for handling helloworld Record +// +// RULES: +// - T.B.A. +// + +import type { Helloworld } from '@prisma/client'; + +import prisma from '../lib/prisma'; + +type CreateHelloworld = { + hello: string; +}; + +type UpdateHelloworld = { + hello: string; +}; + +async function listHelloworlds(): Promise { + return prisma.helloworld.findMany(); +} + +async function getHelloworld(helloworldId: number) { + return prisma.helloworld.findFirst({ where: { id: helloworldId } }); +} + +async function createNewHelloworld(createForm: CreateHelloworld) { + return prisma.helloworld.create({ data: createForm }); +} + +async function updateHelloworld(helloworldId: number, updateForm: UpdateHelloworld) { + return prisma.helloworld.update({ where: { id: helloworldId }, data: updateForm }); +} + +async function deleteHelloworld(helloworldId: number) { + return prisma.helloworld.delete({ where: { id: helloworldId } }); +} + +export { getHelloworld, listHelloworlds, updateHelloworld, deleteHelloworld, createNewHelloworld };