From 39371792794a1c5a8f36c11317624b865eb6f74c Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Fri, 2 May 2025 18:46:38 +0900 Subject: [PATCH] refactor: enhance Header component by organizing useEffect hooks and updating client URL pathname handling --- src/app/api/suitable/category/route.ts | 16 ++++++ src/app/api/suitable/detail/route.ts | 17 ++++++ src/app/api/suitable/list/route.ts | 34 ++++++++++++ src/app/api/suitable/route.ts | 12 +++++ src/app/api/survey-sale/[id]/route.ts | 72 ++++++++++++++++++++++++++ src/app/api/survey-sale/route.ts | 33 ++++++++++++ src/app/api/user/create/route.ts | 23 ++++++++ src/app/api/user/list/route.ts | 7 +++ src/app/api/user/route.ts | 37 +++++++++++++ 9 files changed, 251 insertions(+) create mode 100644 src/app/api/suitable/category/route.ts create mode 100644 src/app/api/suitable/detail/route.ts create mode 100644 src/app/api/suitable/list/route.ts create mode 100644 src/app/api/suitable/route.ts create mode 100644 src/app/api/survey-sale/[id]/route.ts create mode 100644 src/app/api/survey-sale/route.ts create mode 100644 src/app/api/user/create/route.ts create mode 100644 src/app/api/user/list/route.ts create mode 100644 src/app/api/user/route.ts diff --git a/src/app/api/suitable/category/route.ts b/src/app/api/suitable/category/route.ts new file mode 100644 index 0000000..288a74a --- /dev/null +++ b/src/app/api/suitable/category/route.ts @@ -0,0 +1,16 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function GET() { + // @ts-ignore + const roofMaterialCategory = await prisma.MS_SUITABLE.findMany({ + select: { + roof_material: true, + }, + distinct: ['roof_material'], + orderBy: { + roof_material: 'asc', + }, + }) + return NextResponse.json(roofMaterialCategory) +} diff --git a/src/app/api/suitable/detail/route.ts b/src/app/api/suitable/detail/route.ts new file mode 100644 index 0000000..d29f666 --- /dev/null +++ b/src/app/api/suitable/detail/route.ts @@ -0,0 +1,17 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function GET(request: Request) { + const { searchParams } = new URL(request.url) + const roofMaterial = searchParams.get('roof-material') + console.log('๐Ÿš€ ~ GET ~ roof-material:', roofMaterial) + + // @ts-ignore + const suitables = await prisma.MS_SUITABLE.findMany({ + where: { + roof_material: roofMaterial, + }, + }) + + return NextResponse.json(suitables) +} diff --git a/src/app/api/suitable/list/route.ts b/src/app/api/suitable/list/route.ts new file mode 100644 index 0000000..d789275 --- /dev/null +++ b/src/app/api/suitable/list/route.ts @@ -0,0 +1,34 @@ +import { NextRequest, NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function GET(request: NextRequest) { + try { + const searchParams = request.nextUrl.searchParams + const category = searchParams.get('category') + const keyword = searchParams.get('keyword') + + let whereCondition: any = {} + if (category) { + whereCondition['roof_material'] = category + } + if (keyword) { + whereCondition['product_name'] = { + contains: keyword, + } + } + console.log('๐Ÿš€ ~ /api/suitable/list: ~ prisma where condition:', whereCondition) + + // @ts-ignore + const suitables = await prisma.MS_SUITABLE.findMany({ + where: whereCondition, + orderBy: { + product_name: 'asc', + }, + }) + + return NextResponse.json(suitables) + } catch (error) { + console.error('โŒ ๋ฐ์ดํ„ฐ ์กฐํšŒ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค:', error) + return NextResponse.json({ error: '๋ฐ์ดํ„ฐ ์กฐํšŒ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค' }, { status: 500 }) + } +} diff --git a/src/app/api/suitable/route.ts b/src/app/api/suitable/route.ts new file mode 100644 index 0000000..7f7be9c --- /dev/null +++ b/src/app/api/suitable/route.ts @@ -0,0 +1,12 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function POST(request: Request) { + const body = await request.json() + // @ts-ignore + const suitables = await prisma.MS_SUITABLE.createMany({ + data: body, + }) + + return NextResponse.json({ message: 'Suitable created successfully' }) +} diff --git a/src/app/api/survey-sale/[id]/route.ts b/src/app/api/survey-sale/[id]/route.ts new file mode 100644 index 0000000..bf5afe4 --- /dev/null +++ b/src/app/api/survey-sale/[id]/route.ts @@ -0,0 +1,72 @@ +import { NextResponse } from 'next/server' + +export async function POST(request: Request, context: { params: { id: string } }) { + const body = await request.json() + const { id } = await context.params + + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({ + where: { id: Number(id) }, + data: { + detail_info: { + create: body, + }, + }, + }) + return NextResponse.json({ message: 'Survey detail created successfully' }) +} +export async function GET(request: Request, context: { params: { id: string } }) { + const { id } = await context.params + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.findUnique({ + where: { id: Number(id) }, + include: { + detail_info: true, + }, + }) + return NextResponse.json(survey) +} + +export async function PUT(request: Request, context: { params: { id: string } }) { + const { id } = await context.params + const body = await request.json() + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({ + where: { id: Number(id) }, + data: { + ...body, + detail_info: { + update: body.detail_info, + }, + }, + }) + return NextResponse.json(survey) +} + +export async function DELETE(request: Request, context: { params: { id: string; detail_id: string } }) { + const { id, detail_id } = await context.params + if (detail_id) { + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_DETAIL_INFO.delete({ + where: { id: Number(detail_id) }, + }) + } else { + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.delete({ + where: { id: Number(id) }, + }) + } + return NextResponse.json({ message: 'Survey deleted successfully' }) +} + +export async function PATCH(request: Request, context: { params: { id: string } }) { + const { id } = await context.params + // @ts-ignore + const survey = await prisma.SD_SERVEY_SALES_BASIC_INFO.update({ + where: { id: Number(id) }, + data: { + submission_status: true, + }, + }) + return NextResponse.json({ message: 'Survey confirmed successfully' }) +} diff --git a/src/app/api/survey-sale/route.ts b/src/app/api/survey-sale/route.ts new file mode 100644 index 0000000..064c686 --- /dev/null +++ b/src/app/api/survey-sale/route.ts @@ -0,0 +1,33 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function POST(request: Request) { + const body = await request.json() + // @ts-ignore + const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.create({ + data: body, + }) + return NextResponse.json(res) +} + +export async function GET() { + try { + // @ts-ignore + const res = await prisma.SD_SERVEY_SALES_BASIC_INFO.findMany() + return NextResponse.json(res) + } catch (error) { + console.error(error) + } +} + +export async function PUT(request: Request) { + const body = await request.json() + console.log('๐Ÿš€ ~ PUT ~ body:', body) + const detailInfo = { ...body.detail_info, basic_info_id: body.id } + console.log('๐Ÿš€ ~ PUT ~ detailInfo:', detailInfo) + // @ts-ignore + const res = await prisma.SD_SERVEY_SALES_DETAIL_INFO.create({ + data: detailInfo, + }) + return NextResponse.json({ message: 'Survey sales updated successfully' }) +} diff --git a/src/app/api/user/create/route.ts b/src/app/api/user/create/route.ts new file mode 100644 index 0000000..c4e4060 --- /dev/null +++ b/src/app/api/user/create/route.ts @@ -0,0 +1,23 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export async function POST(request: Request) { + try { + const body = await request.json() + const { username, email, password } = body + + const user = await prisma.user.create({ + data: { + username, + email, + password, + updated_at: new Date(), + }, + }) + + return NextResponse.json(user) + } catch (error) { + console.error('Error creating user:', error) + return NextResponse.json({ error: 'Error creating user' }, { status: 500 }) + } +} diff --git a/src/app/api/user/list/route.ts b/src/app/api/user/list/route.ts new file mode 100644 index 0000000..f84af78 --- /dev/null +++ b/src/app/api/user/list/route.ts @@ -0,0 +1,7 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' + +export const GET = async () => { + const users = await prisma.user.findMany() + return NextResponse.json(users) +} diff --git a/src/app/api/user/route.ts b/src/app/api/user/route.ts new file mode 100644 index 0000000..d3cda5f --- /dev/null +++ b/src/app/api/user/route.ts @@ -0,0 +1,37 @@ +import { NextResponse } from 'next/server' +import { prisma } from '@/libs/prisma' +import { getIronSession } from 'iron-session' +import { cookies } from 'next/headers' +import { SessionData, sessionOptions } from '@/libs/session' + +export async function POST(request: Request) { + const { username, password } = await request.json() + + console.log('๐Ÿš€ ~ POST ~ username:', username) + console.log('๐Ÿš€ ~ POST ~ password:', password) + + const user = await prisma.user.findFirst({ + where: { + username: username, + password: password, + }, + }) + console.log('๐Ÿš€ ~ POST ~ user:', user) + + if (!user) { + return NextResponse.json({ error: 'User not found' }, { status: 404 }) + } + + const cookieStore = await cookies() + const session = await getIronSession(cookieStore, sessionOptions) + console.log('start session edit!') + session.username = user.username! + session.email = user.email! + session.isLoggedIn = true + console.log('end session edit!') + await session.save() + console.log('๐Ÿš€ ~ POST ~ session:', session) + + // return NextResponse.redirect(new URL(process.env.NEXT_PUBLIC_URL!, request.url)) + return NextResponse.json(user) +}