-
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
1 parent
f283cf6
commit f1d1f1c
Showing
8 changed files
with
88 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { PrismaClient } from '@prisma/client' | ||
import type { CreateUserSchema } from '../schemas/user.schema' | ||
|
||
export const createUser = (prisma: PrismaClient, user: CreateUserSchema) => | ||
prisma.user.create({ | ||
data: { | ||
email: user.email, | ||
name: user.name, | ||
}, | ||
}) |
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,3 @@ | ||
import type { PrismaClient } from '@prisma/client' | ||
|
||
export const findAllUsers = (prisma: PrismaClient) => prisma.user.findMany() |
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,25 @@ | ||
import { z } from 'zod' | ||
|
||
const user = { | ||
email: z | ||
.string({ | ||
required_error: 'Email is required', | ||
invalid_type_error: 'Email must be a string', | ||
}) | ||
.email(), | ||
name: z | ||
.string({ | ||
invalid_type_error: 'Name must be a string', | ||
}) | ||
.nullish(), | ||
} | ||
|
||
export const findAllUsersSchema = z.array(z.object({ ...user, id: z.string() })) | ||
|
||
export const createUserSchema = z.object(user) | ||
export const createUserResponseSchema = z.object({ | ||
id: z.string(), | ||
...user, | ||
}) | ||
|
||
export type CreateUserSchema = z.infer<typeof createUserSchema> |
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,9 +1,17 @@ | ||
import type { FastifyPluginAsync } from 'fastify' | ||
import type { ZodTypeProvider } from 'fastify-type-provider-zod' | ||
import { z } from 'zod' | ||
|
||
const example: FastifyPluginAsync = async (fastify, _opts): Promise<void> => { | ||
fastify.get('/', async (_request, _reply) => { | ||
return 'this is an example' | ||
}) | ||
fastify | ||
.withTypeProvider<ZodTypeProvider>() | ||
.get( | ||
'/', | ||
{ schema: { response: { 200: z.string() } } }, | ||
async (_request, _reply) => { | ||
return 'this is an example' | ||
}, | ||
) | ||
} | ||
|
||
export default example |
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,9 +1,35 @@ | ||
import type { FastifyPluginAsync } from 'fastify' | ||
import type { ZodTypeProvider } from 'fastify-type-provider-zod' | ||
import { findAllUsers } from '../../domains/users/db/findAllUsers.db' | ||
import { | ||
createUserResponseSchema, | ||
createUserSchema, | ||
findAllUsersSchema, | ||
} from '../../domains/users/schemas/user.schema' | ||
import { createUser } from '../../domains/users/db/createUser.db' | ||
|
||
const users: FastifyPluginAsync = async (fastify, _opts) => { | ||
fastify.get('/', async (_request, _reply) => { | ||
return fastify.prisma.user.findMany() | ||
}) | ||
fastify | ||
.withTypeProvider<ZodTypeProvider>() | ||
.get('/', { schema: { response: { 200: findAllUsersSchema } } }, () => { | ||
return findAllUsers(fastify.prisma) | ||
}) | ||
|
||
fastify.withTypeProvider<ZodTypeProvider>().post( | ||
'/', | ||
{ | ||
schema: { | ||
body: createUserSchema, | ||
response: { | ||
201: createUserResponseSchema, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const user = await createUser(fastify.prisma, request.body) | ||
reply.code(201).send(user) | ||
}, | ||
) | ||
} | ||
|
||
export default users |