"feat: add EventItem, EventReview models with seed data and mock files, update User and Event schemas"
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// src/app/api/user/promoteToAdmin/route.ts
|
||||
//
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { changeToAdmin } from 'src/app/services/userItem.service';
|
||||
|
||||
/**
|
||||
***************************************
|
||||
* PUT - promote user to admin
|
||||
***************************************
|
||||
*/
|
||||
export async function PUT(req: NextRequest) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
|
||||
// userId, the userId going to be admin
|
||||
// {data: requestUserId}, the userId sending request to promote admin
|
||||
// the requestUserId should be a admin admin already
|
||||
|
||||
const userId = searchParams.get('userId');
|
||||
const {
|
||||
data: { requestUserId },
|
||||
} = await req.json();
|
||||
|
||||
try {
|
||||
if (!userId) return response('userId cannot be null', STATUS.BAD_REQUEST);
|
||||
if (!requestUserId) return response('requestUserId cannot be null', STATUS.BAD_REQUEST);
|
||||
|
||||
const result = await changeToAdmin(userId, requestUserId);
|
||||
|
||||
return response(result, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('promote to admin', JSON.stringify(error));
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
# step 1 check not a admin
|
||||
###
|
||||
GET http://localhost:7272/api/user/details?userId=00b8b53b-dfe6-4b07-8d23-0dc9f75e7a2c
|
||||
|
||||
# step 1 check not a admin
|
||||
###
|
||||
GET http://localhost:7272/api/user/details?userId=5e072d02-919c-49c4-98b4-9659b8eff231
|
||||
|
||||
|
||||
# step 2 request to change admin
|
||||
###
|
||||
PUT http://localhost:7272/api/user/changeToAdmin?userId=00b8b53b-dfe6-4b07-8d23-0dc9f75e7a2c
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"data": {"requestUserId": "5e072d02-919c-49c4-98b4-9659b8eff231"}
|
||||
}
|
||||
|
||||
|
||||
|
||||
# step 2 request to change user
|
||||
###
|
||||
PUT http://localhost:7272/api/user/changeToUser?userId=00b8b53b-dfe6-4b07-8d23-0dc9f75e7a2c
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"data": {"requestUserId": "5e072d02-919c-49c4-98b4-9659b8eff231"}
|
||||
}
|
||||
|
||||
|
||||
# step 3 it is now admin
|
||||
###
|
||||
GET http://localhost:7272/api/user/details?userId=00b8b53b-dfe6-4b07-8d23-0dc9f75e7a2c
|
36
03_source/cms_backend/src/app/api/user/changeToUser/route.ts
Normal file
36
03_source/cms_backend/src/app/api/user/changeToUser/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// src/app/api/user/changeToUser/route.ts
|
||||
//
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { changeToUser } from 'src/app/services/userItem.service';
|
||||
|
||||
/**
|
||||
***************************************
|
||||
* PUT - change admin back to regular user
|
||||
***************************************
|
||||
*/
|
||||
export async function PUT(req: NextRequest) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
|
||||
// userId, the userId going to be changed to regular user
|
||||
// {data: requestUserId}, the userId sending request to change role
|
||||
// the requestUserId should be an admin
|
||||
|
||||
const userId = searchParams.get('userId');
|
||||
const {
|
||||
data: { requestUserId },
|
||||
} = await req.json();
|
||||
|
||||
try {
|
||||
if (!userId) throw new Error('userId cannot be null');
|
||||
if (!requestUserId) throw new Error('requestUserId cannot be null');
|
||||
|
||||
const result = await changeToUser(userId, requestUserId);
|
||||
|
||||
return response(result, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('change to regular user', JSON.stringify(error));
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
###
|
||||
PUT http://localhost:7272/api/user/changeToUser?userId=cmbfx38e30000ssek6ufjewcp
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"data": {"requestUserId": "cmbfx38kk000esseka9qo8lpt"}
|
||||
}
|
||||
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user/changeToUser?userId=
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user?userId=cmbfx38e30000ssek6ufjewcp
|
21
03_source/cms_backend/src/app/api/user/checkAdmin/route.ts
Normal file
21
03_source/cms_backend/src/app/api/user/checkAdmin/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { isAdmin } from 'src/app/services/userItem.service';
|
||||
|
||||
// import prisma from '../../lib/prisma';
|
||||
|
||||
export async function GET(req: NextRequest, res: NextResponse) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
const userId = searchParams.get('userId');
|
||||
|
||||
try {
|
||||
if (!userId) throw new Error('userId cannot be null');
|
||||
const result = await isAdmin(userId);
|
||||
|
||||
return response(result, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('Post - Get latest', error);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
###
|
||||
GET http://localhost:7272/api/user/checkAdmin?userId=cmbfx38ey0001ssek5pr05o70
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user/checkAdmin?userId=cmbfvonkx000411ykuah8pp8s
|
||||
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user/checkAdmin
|
@@ -1,4 +1,4 @@
|
||||
// src/app/api/product/details/route.ts
|
||||
// src/app/api/user/details/route.ts
|
||||
//
|
||||
// PURPOSE:
|
||||
// read user from db by id
|
||||
@@ -24,9 +24,7 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
// RULES: userId must exist
|
||||
const userId = searchParams.get('userId');
|
||||
if (!userId) {
|
||||
return response({ message: 'userId is required!' }, STATUS.BAD_REQUEST);
|
||||
}
|
||||
if (!userId) return response({ message: 'userId is required!' }, STATUS.BAD_REQUEST);
|
||||
|
||||
// NOTE: userId confirmed exist, run below
|
||||
const user = await prisma.userItem.findFirst({
|
||||
@@ -34,9 +32,7 @@ export async function GET(req: NextRequest) {
|
||||
where: { id: userId },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return response({ message: 'User not found!' }, STATUS.NOT_FOUND);
|
||||
}
|
||||
if (!user) return response({ message: 'User not found!' }, STATUS.NOT_FOUND);
|
||||
|
||||
logger('[User] details', user.id);
|
||||
|
||||
|
21
03_source/cms_backend/src/app/api/user/helloworld/route.ts
Normal file
21
03_source/cms_backend/src/app/api/user/helloworld/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { isAdmin } from 'src/app/services/userItem.service';
|
||||
|
||||
// import prisma from '../../lib/prisma';
|
||||
|
||||
export async function GET(req: NextRequest, res: NextResponse) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
const userId = searchParams.get('userId');
|
||||
|
||||
try {
|
||||
if (!userId) throw new Error('userId cannot be null');
|
||||
const result = await isAdmin(userId);
|
||||
|
||||
return response(result, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('Post - Get latest', error);
|
||||
}
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
###
|
||||
GET http://localhost:7272/api/user
|
@@ -1,17 +1,20 @@
|
||||
// src/app/api/product/list/route.ts
|
||||
//
|
||||
import { logger } from 'src/utils/logger';
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import prisma from '../../../lib/prisma';
|
||||
import { listUsers } from 'src/app/services/userItem.service';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** **************************************
|
||||
/**
|
||||
***************************************
|
||||
* GET - Products
|
||||
*************************************** */
|
||||
***************************************
|
||||
*/
|
||||
export async function GET() {
|
||||
try {
|
||||
const users = await prisma.userItem.findMany();
|
||||
const users = await listUsers();
|
||||
|
||||
logger('[User] list', users.length);
|
||||
|
||||
|
80
03_source/cms_backend/src/app/api/user/route.ts
Normal file
80
03_source/cms_backend/src/app/api/user/route.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { listUsers, deleteUser, updateUser, createNewUser } from 'src/app/services/userItem.service';
|
||||
|
||||
// import prisma from '../../lib/prisma';
|
||||
|
||||
export async function GET(req: NextRequest, res: NextResponse) {
|
||||
try {
|
||||
const result = await listUsers();
|
||||
|
||||
return response(result, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('User - Get latest', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
***************************************
|
||||
* POST - create User
|
||||
***************************************
|
||||
*/
|
||||
export async function POST(req: NextRequest) {
|
||||
const { data } = await req.json();
|
||||
|
||||
try {
|
||||
const createResult = await createNewUser(data);
|
||||
|
||||
return response(createResult, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('User - Create', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
***************************************
|
||||
* PUT - update User
|
||||
***************************************
|
||||
*/
|
||||
export async function PUT(req: NextRequest) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
const userId = searchParams.get('userId');
|
||||
|
||||
const { data } = await req.json();
|
||||
|
||||
try {
|
||||
if (!userId) throw new Error('userId cannot null');
|
||||
const id: number = parseInt(userId);
|
||||
|
||||
const updateResult = await updateUser(id, data);
|
||||
|
||||
return response(updateResult, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('User - Update', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
***************************************
|
||||
* DELETE - update User
|
||||
***************************************
|
||||
*/
|
||||
export async function DELETE(req: NextRequest) {
|
||||
const { searchParams } = req.nextUrl;
|
||||
const userId = searchParams.get('userId');
|
||||
|
||||
const { data } = await req.json();
|
||||
|
||||
try {
|
||||
if (!userId) throw new Error('userId cannot null');
|
||||
const id: number = parseInt(userId);
|
||||
|
||||
const deleteResult = await deleteUser(id);
|
||||
|
||||
return response(deleteResult, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('User - Update', error);
|
||||
}
|
||||
}
|
26
03_source/cms_backend/src/app/api/user/test.http
Normal file
26
03_source/cms_backend/src/app/api/user/test.http
Normal file
@@ -0,0 +1,26 @@
|
||||
###
|
||||
GET http://localhost:7272/api/user
|
||||
|
||||
###
|
||||
GET http://localhost:7272/api/user?userId=cmbfvonhl000011ykdi345yc9
|
||||
|
||||
###
|
||||
POST http://localhost:7272/api/user?userId=1
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"data":{"name": "John Doe"}
|
||||
}
|
||||
|
||||
###
|
||||
PUT http://localhost:7272/api/user?userId=1
|
||||
content-type: application/json
|
||||
|
||||
{
|
||||
"data": {"name": "John Doe"}
|
||||
}
|
||||
|
||||
|
||||
###
|
||||
DELETE http://localhost:7272/api/user?userId=1
|
||||
|
Reference in New Issue
Block a user