Skip to content

Commit

Permalink
feat: add getOnePostHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
wd-David committed May 17, 2022
1 parent f30ea17 commit 134c26d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
11 changes: 1 addition & 10 deletions src/plugins/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ export default fp<FastifyDynamicSwaggerOptions>(async (fastify, opts) => {
{
url: 'http://localhost'
}
],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
}
]
},
hideUntagged: true,
exposeRoute: true
Expand Down
16 changes: 8 additions & 8 deletions src/routes/v1/posts/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import {
} from './schema'
import { posts } from './posts'

export const getAllPostsHandler: RouteHandler<{
export const getPostsHandler: RouteHandler<{
Querystring: PostsQuery
Reply: GetPostsResponse
}> = async function (req, reply) {
if (req.query?.deleted !== undefined) {
const { deleted } = req.query
const { deleted } = req.query
if (deleted !== undefined) {
const filteredPosts = posts.filter((post) => post.deleted === deleted)
reply.send({ posts: filteredPosts })
} else reply.send({ posts })
}

export const getPostsHandler: RouteHandler<{
export const getOnePostHandler: RouteHandler<{
Params: PostsParams
Reply: GetPostsResponse | NotFoundResponse
}> = async function (req, reply) {
Expand All @@ -44,29 +44,29 @@ export const postPostsHandler: RouteHandler<{
export const putPostsHandler: RouteHandler<{
Params: PostsParams
Body: PostsBody
Reply: Record<string, never> | NotFoundResponse
Reply: NotFoundResponse
}> = async function (req, reply) {
const { postid } = req.params
const post = posts.find((p) => p.id == postid)
if (post) {
post.title = req.body.title
post.content = req.body.content
post.tags = req.body.tags
reply.code(204).send({})
reply.code(204)
} else {
reply.code(404).send({ error: 'Post not found' })
}
}

export const deletePostsHandler: RouteHandler<{
Params: PostsParams
Reply: Record<string, never> | NotFoundResponse
Reply: NotFoundResponse
}> = async function (req, reply) {
const { postid } = req.params
const post = posts.find((p) => p.id == postid)
if (post) {
post.deleted = true
reply.code(204).send({})
reply.code(204)
} else {
reply.code(404).send({ error: 'Post not found' })
}
Expand Down
13 changes: 6 additions & 7 deletions src/routes/v1/posts/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { FastifyPluginAsync } from 'fastify'
import { type FastifyInstance } from 'fastify'
import {
postSchema,
postNotFoundSchema,
getPostsSchema,
getOnePostSchema,
postPostsSchema,
putPostsSchema,
deletePostsSchema
} from './schema'
import {
getAllPostsHandler,
getPostsHandler,
getOnePostHandler,
postPostsHandler,
putPostsHandler,
deletePostsHandler
} from './handler'

const posts: FastifyPluginAsync = async (fastify): Promise<void> => {
export default async (fastify: FastifyInstance) => {
fastify.addSchema(postSchema)
fastify.addSchema(postNotFoundSchema)
fastify.get('/', { schema: getPostsSchema }, getAllPostsHandler)
fastify.get('/:postid', { schema: getPostsSchema }, getPostsHandler)
fastify.get('/', { schema: getPostsSchema }, getPostsHandler)
fastify.get('/:postid', { schema: getOnePostSchema }, getOnePostHandler)
fastify.post('/', { schema: postPostsSchema }, postPostsHandler)
fastify.put('/:postid', { schema: putPostsSchema }, putPostsHandler)
fastify.delete('/:postid', { schema: deletePostsSchema }, deletePostsHandler)
}

export default posts
16 changes: 13 additions & 3 deletions src/routes/v1/posts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ export type GetPostsResponse = FromSchema<
/* Get */
export const getPostsSchema: FastifySchema = {
tags: ['Posts'],
description: 'Get a post by id',
description: 'Get posts',
querystring: postsQuerySchema,
response: {
200: {
...getPostsResponseSchema
}
}
}

export const getOnePostSchema: FastifySchema = {
tags: ['Posts'],
description: 'Get a post by id',
params: postsParamsSchema,
response: {
200: {
Expand Down Expand Up @@ -117,7 +127,7 @@ export const putPostsSchema: FastifySchema = {
response: {
204: {
description: 'The post was updated',
type: 'object'
type: 'null'
},
404: {
description: 'The post was not found',
Expand All @@ -134,7 +144,7 @@ export const deletePostsSchema: FastifySchema = {
response: {
204: {
description: 'The post was deleted',
type: 'object'
type: 'null'
},
404: {
description: 'The post was not found',
Expand Down

0 comments on commit 134c26d

Please sign in to comment.