Compare commits

...

1 Commits

Author SHA1 Message Date
louiscklaw
fc6ed533e2 feat: implement helloworld API with CRUD operations and test cases 2025-06-02 19:42:32 +08:00
6 changed files with 158 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<Helloworld[]> {
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 };