-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d4b6f
commit ee7e66b
Showing
5 changed files
with
207 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,127 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { verifyToken } from '../../lib/token'; | ||
import { DIARY } from '../../../constants'; | ||
import { isLengthInRange } from '../../../utils'; | ||
import { createNewDiary, getAllDiaryByUser } from '../../lib/diary'; | ||
import { cookies } from 'next/headers'; | ||
import { addUserPoints } from '../../lib/point'; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { verifyToken } from "../../lib/token"; | ||
import { DIARY } from "../../../constants"; | ||
import { isLengthInRange } from "../../../utils"; | ||
import { createNewDiary, getAllDiaryByUser } from "../../lib/diary"; | ||
import { cookies } from "next/headers"; | ||
import { addUserPoints } from "../../lib/point"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const page = req.nextUrl.searchParams.get('page'); | ||
const pageSize = req.nextUrl.searchParams.get('pageSize'); | ||
const skip = (Number(page) - 1) * Number(pageSize); | ||
try { | ||
const userId = verifyToken( | ||
cookies().get('dreaming_accessToken')?.value ?? '' | ||
).userId; | ||
} catch (e) { | ||
console.log(e); | ||
return new Response( | ||
JSON.stringify({ | ||
error: '토큰이 만료되었습니다.', | ||
}), | ||
{ | ||
status: 401, | ||
} | ||
); | ||
} | ||
const page = req.nextUrl.searchParams.get("page"); | ||
const pageSize = req.nextUrl.searchParams.get("pageSize"); | ||
const skip = (Number(page) - 1) * Number(pageSize); | ||
try { | ||
const userId = verifyToken( | ||
cookies().get("dreaming_accessToken")?.value ?? "" | ||
).userId; | ||
} catch (e) { | ||
console.log(e); | ||
return new Response( | ||
JSON.stringify({ | ||
error: "토큰이 만료되었습니다.", | ||
}), | ||
{ | ||
status: 401, | ||
} | ||
); | ||
} | ||
|
||
try { | ||
const userId = verifyToken( | ||
cookies().get('dreaming_accessToken')?.value ?? '' | ||
).userId; | ||
const getAllPosts = await getAllDiaryByUser( | ||
userId, | ||
parseInt(skip + ''), | ||
parseInt(pageSize as string) | ||
); | ||
try { | ||
const userId = verifyToken( | ||
cookies().get("dreaming_accessToken")?.value ?? "" | ||
).userId; | ||
const getAllPosts = await getAllDiaryByUser( | ||
userId, | ||
parseInt(skip + ""), | ||
parseInt(pageSize as string) | ||
); | ||
|
||
if (userId && getAllPosts) { | ||
return new Response(JSON.stringify(getAllPosts), { | ||
status: 200, | ||
}); | ||
if (userId && getAllPosts) { | ||
return new Response(JSON.stringify(getAllPosts), { | ||
status: 200, | ||
}); | ||
} | ||
} catch (e) { | ||
return NextResponse.json( | ||
{ | ||
error: "유저의 다이어리를 불러올 수 없어요", | ||
}, | ||
{ | ||
status: 502, | ||
} | ||
); | ||
} | ||
} catch (e) { | ||
return NextResponse.json( | ||
{ | ||
error: '유저의 다이어리를 불러올 수 없어요', | ||
}, | ||
{ | ||
status: 502, | ||
} | ||
); | ||
} | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
const userId = verifyToken( | ||
cookies().get('dreaming_accessToken')?.value ?? '' | ||
).userId; | ||
const userId = verifyToken( | ||
cookies().get("dreaming_accessToken")?.value ?? "" | ||
).userId; | ||
|
||
if (!userId) { | ||
return new Response( | ||
JSON.stringify({ | ||
error: '토큰이 만료되었습니다.', | ||
}), | ||
{ | ||
status: 401, | ||
} | ||
); | ||
} | ||
if (!userId) { | ||
return new Response( | ||
JSON.stringify({ | ||
error: "토큰이 만료되었습니다.", | ||
}), | ||
{ | ||
status: 401, | ||
} | ||
); | ||
} | ||
|
||
const { title, content, isShare } = await req.json(); | ||
if (!isLengthInRange(title, DIARY.TITLE.MIN_LENGTH, DIARY.TITLE.MAX_LENGTH)) { | ||
return NextResponse.json( | ||
{ | ||
error: '0글자 이상 1000글자 이하의 제목을 입력해주세요', | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
const { title, content, isShare } = await req.json(); | ||
if ( | ||
!isLengthInRange(title, DIARY.TITLE.MIN_LENGTH, DIARY.TITLE.MAX_LENGTH) | ||
) { | ||
return NextResponse.json( | ||
{ | ||
error: "0글자 이상 1000글자 이하의 제목을 입력해주세요", | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
|
||
if ( | ||
!isLengthInRange( | ||
content, | ||
DIARY.CONTENT.MIN_LENGTH, | ||
DIARY.CONTENT.MAX_LENGTH | ||
) | ||
) { | ||
return NextResponse.json( | ||
{ | ||
error: '0글자 이상 5000글자 이하의 글을 입력해주세요', | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
try { | ||
const decodedToken = verifyToken( | ||
cookies().get('dreaming_accessToken')?.value ?? '' | ||
); | ||
if ( | ||
!isLengthInRange( | ||
content, | ||
DIARY.CONTENT.MIN_LENGTH, | ||
DIARY.CONTENT.MAX_LENGTH | ||
) | ||
) { | ||
return NextResponse.json( | ||
{ | ||
error: "0글자 이상 5000글자 이하의 글을 입력해주세요", | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
try { | ||
const decodedToken = verifyToken( | ||
cookies().get("dreaming_accessToken")?.value ?? "" | ||
); | ||
|
||
const newPost = await createNewDiary({ | ||
title, | ||
content, | ||
isShare, | ||
writer: Number(decodedToken?.userId), | ||
}); | ||
const newPost = await createNewDiary({ | ||
title, | ||
content, | ||
isShare, | ||
writer: Number(decodedToken?.userId), | ||
}); | ||
|
||
await addUserPoints(userId + ''); | ||
return new Response(JSON.stringify(newPost), { | ||
status: 200, | ||
}); | ||
} catch (e) { | ||
return NextResponse.json( | ||
{ error: '새로운 다이어리를 생성할 수 없어요' }, | ||
{ | ||
status: 502, | ||
} | ||
); | ||
} | ||
await addUserPoints(userId + ""); | ||
return new Response(JSON.stringify(newPost), { | ||
status: 200, | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
return NextResponse.json( | ||
{ error: "새로운 다이어리를 생성할 수 없어요" }, | ||
{ | ||
status: 502, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.