mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-04-19 23:37:41 +00:00
#1 update api added
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
520ddfe653
commit
2b5f4a9fd6
5 changed files with 83 additions and 4 deletions
|
@ -31,3 +31,15 @@ GET {{BASE_URL}}/wishlist
|
||||||
###
|
###
|
||||||
# @name getFirstWishlist
|
# @name getFirstWishlist
|
||||||
GET {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.slugUrlText}}
|
GET {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.slugUrlText}}
|
||||||
|
|
||||||
|
###
|
||||||
|
# @name updateFirstWishlist
|
||||||
|
PUT {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.id}}
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"title": "Junior",
|
||||||
|
"imageSrc": "https://unsplash.com/photos/JZ51o_-UOY8/download?force=true&w=200",
|
||||||
|
"description": "Juniors Wishlist",
|
||||||
|
"slugUrlText": "junior"
|
||||||
|
}
|
||||||
|
|
|
@ -23,6 +23,16 @@ export default {
|
||||||
data: payload,
|
data: payload,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
update: async (id: string, payload: Wishlist) => {
|
||||||
|
return await prisma.client.wishlist.update({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
...payload,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
updateItem: async (itemId: number, payload: any) => {
|
updateItem: async (itemId: number, payload: any) => {
|
||||||
return await prisma.client.item.update({
|
return await prisma.client.item.update({
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { FastifyInstance } from 'fastify'
|
import { FastifyInstance } from 'fastify'
|
||||||
import { getAll, getBySlugUrl } from './read'
|
import { getAll, getBySlugUrl } from './read'
|
||||||
import { updateItem } from './update'
|
import { updateList, updateItem } from './update'
|
||||||
import { createList } from './create'
|
import { createList } from './create'
|
||||||
|
|
||||||
export default async (app: FastifyInstance) => {
|
export default async (app: FastifyInstance) => {
|
||||||
await app.route(getAll)
|
await app.route(getAll)
|
||||||
await app.route(getBySlugUrl)
|
await app.route(getBySlugUrl)
|
||||||
await app.route(createList)
|
await app.route(createList)
|
||||||
|
await app.route(updateList)
|
||||||
await app.route(updateItem)
|
await app.route(updateItem)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
||||||
import { wishlist } from '../../models'
|
import { wishlist } from '../../models'
|
||||||
|
|
||||||
export const getAll = <any>{
|
export const getAll = <RouteOptions>{
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/',
|
url: '/',
|
||||||
schema: {
|
schema: {
|
||||||
|
|
|
@ -1,13 +1,69 @@
|
||||||
|
import { uniqueKeyError } from '../../config/errors'
|
||||||
|
import { prisma } from '../../services'
|
||||||
|
import { Wishlist } from '@/types'
|
||||||
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
||||||
import { wishlist } from '../../models'
|
import { wishlist } from '../../models'
|
||||||
|
|
||||||
interface GetBySlugUrlTextRequest extends FastifyRequest {
|
interface updateRequest extends FastifyRequest {
|
||||||
|
params: {
|
||||||
|
wishlistId: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
interface updateItemRequest extends FastifyRequest {
|
||||||
params: {
|
params: {
|
||||||
wishlistId: string
|
wishlistId: string
|
||||||
itemId: number
|
itemId: number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const updateList = <RouteOptions>{
|
||||||
|
method: 'PUT',
|
||||||
|
url: '/:wishlistId',
|
||||||
|
schema: {
|
||||||
|
body: {
|
||||||
|
type: 'object',
|
||||||
|
additionalProperties: false,
|
||||||
|
required: ['title', 'imageSrc', 'slugUrlText'],
|
||||||
|
properties: {
|
||||||
|
title: { type: 'string' },
|
||||||
|
imageSrc: { type: 'string' },
|
||||||
|
description: { type: 'string' },
|
||||||
|
slugUrlText: { type: 'string' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
response: {
|
||||||
|
200: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
id: { type: 'string' },
|
||||||
|
title: { type: 'string' },
|
||||||
|
imageSrc: { type: 'string' },
|
||||||
|
description: { type: 'string' },
|
||||||
|
slugUrlText: { type: 'string' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
errorHandler: (error, request, reply) => {
|
||||||
|
if (error instanceof prisma.errorType && error.code === 'P2002') {
|
||||||
|
return reply.send(uniqueKeyError('Slugtext has to be unique'))
|
||||||
|
}
|
||||||
|
if (error instanceof prisma.errorType && error.code === 'P2025') {
|
||||||
|
return reply.callNotFound()
|
||||||
|
}
|
||||||
|
request.log.error(error)
|
||||||
|
reply.send(new Error('Unexptected Error'))
|
||||||
|
},
|
||||||
|
handler: async (request: updateRequest, reply: FastifyReply) => {
|
||||||
|
request.log.debug(request.body)
|
||||||
|
const item = await wishlist.update(
|
||||||
|
request.params.wishlistId,
|
||||||
|
request.body as Wishlist
|
||||||
|
)
|
||||||
|
reply.code(201).send(item)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
export const updateItem = <RouteOptions>{
|
export const updateItem = <RouteOptions>{
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
url: '/:wishlistId/item/:itemId',
|
url: '/:wishlistId/item/:itemId',
|
||||||
|
@ -40,7 +96,7 @@ export const updateItem = <RouteOptions>{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => {
|
handler: async (request: updateItemRequest, reply: FastifyReply) => {
|
||||||
request.log.debug(request.body)
|
request.log.debug(request.body)
|
||||||
const item = await wishlist.updateItem(
|
const item = await wishlist.updateItem(
|
||||||
Number(request.params.itemId),
|
Number(request.params.itemId),
|
||||||
|
|
Loading…
Add table
Reference in a new issue