-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
269 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {authenticated} from "@/app/api/utils/api-utils"; | ||
import dreamsService from "@/app/api/me/dreams/dreams.service"; | ||
import {PostDreamCharacterDto} from "@/app/api/me/dreams/dreams.dto"; | ||
|
||
export const GET = async () => { | ||
return authenticated((session) => ( | ||
dreamsService.fetchCharacters(session) | ||
)) | ||
} | ||
|
||
export const POST = async (req: Request) => { | ||
return authenticated(async (session) => { | ||
const body: PostDreamCharacterDto = await req.json() | ||
return dreamsService.createCharacter(session, body) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {z} from "zod"; | ||
|
||
export type PostDreamDto = { | ||
title: string, | ||
description: string, | ||
comments?: string, | ||
tags?: string[] | ||
characters?: string[] | ||
} | ||
|
||
export const DREAM_TITLE_MIN = 1 | ||
export const DREAM_TITLE_MAX = 500 | ||
export const DREAM_DESC_MIN = 1 | ||
export const DREAM_DESC_MAX = 5000 | ||
export const DREAM_COMMENTS_MAX = 1000 | ||
|
||
export const PostDreamSchema = z.object({ | ||
title: z.string() | ||
.min(DREAM_TITLE_MIN, `The title can't be less than ${DREAM_TITLE_MIN} character!`) | ||
.max(DREAM_TITLE_MAX, `The title can't be more than ${DREAM_TITLE_MAX} characters!`), | ||
|
||
description: z.string() | ||
.min(DREAM_DESC_MIN, `The description can't be less than ${DREAM_DESC_MIN} character!`) | ||
.max(DREAM_DESC_MAX, `The description can't be more than ${DREAM_DESC_MAX} characters!`), | ||
|
||
comments: z.string() | ||
.max(DREAM_COMMENTS_MAX, `The comment can't be more than ${DREAM_COMMENTS_MAX} characters!`) | ||
.optional(), | ||
|
||
tags: z.array(z.string()).optional(), | ||
characters: z.array(z.string()).optional() | ||
}).strict() | ||
|
||
export type PostDreamTagDto = { | ||
tag: string, | ||
} | ||
|
||
export const DREAM_TAG_MIN = 1 | ||
export const DREAM_TAG_MAX = 64 | ||
|
||
export const PostDreamTagSchema = z.object({ | ||
tag: z.string() | ||
.min(DREAM_TAG_MIN, `The tag can't be less than ${DREAM_TAG_MIN} character!`) | ||
.max(DREAM_TAG_MAX, `The tag can't be more than ${DREAM_TAG_MAX} characters!`) | ||
}).strict() | ||
|
||
export type PostDreamCharacterDto = { | ||
name: string, | ||
} | ||
|
||
export const DREAM_CHARACTER_MIN = 1 | ||
export const DREAM_CHARACTER_MAX = 256 | ||
|
||
export const PostDreamCharacterSchema = z.object({ | ||
name: z.string() | ||
.min(DREAM_CHARACTER_MIN, `The name can't be less than ${DREAM_CHARACTER_MIN} character!`) | ||
.max(DREAM_CHARACTER_MAX, `The name can't be more than ${DREAM_CHARACTER_MAX} characters!`) | ||
}).strict() |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import {buildFailedValidationResponse, buildResponse} from "@/app/api/utils/types"; | ||
import {Dream, DreamCharacter, DreamTag} from "@prisma/client"; | ||
import prisma from "@/libs/prisma"; | ||
import {NextResponse} from "next/server"; | ||
import {Session} from "next-auth"; | ||
import { | ||
PostDreamCharacterDto, | ||
PostDreamCharacterSchema, | ||
PostDreamDto, | ||
PostDreamSchema, PostDreamTagDto | ||
} from "@/app/api/me/dreams/dreams.dto"; | ||
|
||
class DreamsService { | ||
|
||
public async fetchDreams(session: Session): Promise<NextResponse<Dream[] | undefined>> { | ||
const member = session.user | ||
const dreams = await prisma.dream.findMany({ | ||
where: { | ||
userId: member.id | ||
} | ||
}); | ||
|
||
return buildResponse({ | ||
data: dreams | ||
}) | ||
} | ||
|
||
public async createDream(session: Session, dto: PostDreamDto): Promise<NextResponse<Dream | undefined>> { | ||
const dtoValidated = PostDreamSchema.safeParse(dto) | ||
if (!dtoValidated.success) | ||
return buildFailedValidationResponse(dtoValidated.error) | ||
|
||
const dream = await prisma.dream.create({ | ||
data: { | ||
title: dto.title, | ||
description: dto.description, | ||
comments: dto.comments, | ||
userId: session.user.id, | ||
tags: {connect: dto.tags?.map(id => ({id})) ?? []}, | ||
characters: {connect: dto.characters?.map(id => ({id})) ?? []} | ||
} | ||
}) | ||
|
||
return buildResponse({ | ||
data: dream | ||
}) | ||
} | ||
|
||
public async fetchCharacters(session: Session) { | ||
const characters = await prisma.dreamCharacter.findMany({ | ||
where: { | ||
userId: session.user.id | ||
} | ||
}) | ||
|
||
return buildResponse({ | ||
data: characters | ||
}) | ||
} | ||
|
||
public async createCharacter(session: Session, dto: PostDreamCharacterDto): Promise<NextResponse<DreamCharacter | undefined>> { | ||
const dtoValidated = PostDreamCharacterSchema.safeParse(dto) | ||
if (!dtoValidated.success) | ||
return buildFailedValidationResponse(dtoValidated.error) | ||
|
||
const character = await prisma.dreamCharacter.create({ | ||
data: { | ||
name: dto.name.toLowerCase(), | ||
userId: session.user.id, | ||
} | ||
}) | ||
|
||
return buildResponse({ | ||
data: character | ||
}) | ||
} | ||
|
||
public async fetchTags(session: Session) { | ||
const tags = await prisma.dreamTag.findMany({ | ||
where: { | ||
userId: session.user.id | ||
} | ||
}) | ||
|
||
return buildResponse({ | ||
data: tags | ||
}) | ||
} | ||
|
||
public async createTag(session: Session, dto: PostDreamTagDto): Promise<NextResponse<DreamTag | undefined>> { | ||
const dtoValidated = PostDreamCharacterSchema.safeParse(dto) | ||
if (!dtoValidated.success) | ||
return buildFailedValidationResponse(dtoValidated.error) | ||
|
||
const tag = await prisma.dreamTag.create({ | ||
data: { | ||
tag: dto.tag.toLowerCase(), | ||
userId: session.user.id, | ||
} | ||
}) | ||
|
||
return buildResponse({ | ||
data: tag | ||
}) | ||
} | ||
} | ||
|
||
const dreamsService = new DreamsService() | ||
export default dreamsService |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {authenticated} from "@/app/api/utils/api-utils"; | ||
import dreamsService from "@/app/api/me/dreams/dreams.service"; | ||
import {PostDreamDto} from "@/app/api/me/dreams/dreams.dto"; | ||
|
||
export const GET = async () => { | ||
return authenticated((session) => ( | ||
dreamsService.fetchDreams(session) | ||
)) | ||
} | ||
|
||
export const POST = async (req: Request) => { | ||
return authenticated(async (session) => { | ||
const body: PostDreamDto = await req.json() | ||
return dreamsService.createDream(session, body) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {authenticated} from "@/app/api/utils/api-utils"; | ||
import dreamsService from "@/app/api/me/dreams/dreams.service"; | ||
import {PostDreamTagDto} from "@/app/api/me/dreams/dreams.dto"; | ||
|
||
export const GET = async () => { | ||
return authenticated((session) => ( | ||
dreamsService.fetchTags(session) | ||
)) | ||
} | ||
|
||
export const POST = async (req: Request) => { | ||
return authenticated(async (session) => { | ||
const body: PostDreamTagDto = await req.json() | ||
return dreamsService.createTag(session, body) | ||
}) | ||
} |
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