From 2b5f4a9fd63398717046bd26e291a8cbcebc128c Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 5 Feb 2022 13:43:55 +0100 Subject: [PATCH] #1 update api added Signed-off-by: Benny Samir Hierl --- examples.http | 12 +++++++ src/api/models/wishlist/index.ts | 10 ++++++ src/api/routes/wishlist/index.ts | 3 +- src/api/routes/wishlist/read.ts | 2 +- src/api/routes/wishlist/update.ts | 60 +++++++++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 4 deletions(-) diff --git a/examples.http b/examples.http index 966382e..700d9a6 100644 --- a/examples.http +++ b/examples.http @@ -31,3 +31,15 @@ GET {{BASE_URL}}/wishlist ### # @name getFirstWishlist 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" +} diff --git a/src/api/models/wishlist/index.ts b/src/api/models/wishlist/index.ts index fe7cbcc..15245e1 100644 --- a/src/api/models/wishlist/index.ts +++ b/src/api/models/wishlist/index.ts @@ -23,6 +23,16 @@ export default { 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 updateItem: async (itemId: number, payload: any) => { return await prisma.client.item.update({ diff --git a/src/api/routes/wishlist/index.ts b/src/api/routes/wishlist/index.ts index 1c9e7ee..33f34fb 100644 --- a/src/api/routes/wishlist/index.ts +++ b/src/api/routes/wishlist/index.ts @@ -1,11 +1,12 @@ import { FastifyInstance } from 'fastify' import { getAll, getBySlugUrl } from './read' -import { updateItem } from './update' +import { updateList, updateItem } from './update' import { createList } from './create' export default async (app: FastifyInstance) => { await app.route(getAll) await app.route(getBySlugUrl) await app.route(createList) + await app.route(updateList) await app.route(updateItem) } diff --git a/src/api/routes/wishlist/read.ts b/src/api/routes/wishlist/read.ts index 04fd5c9..03e5928 100644 --- a/src/api/routes/wishlist/read.ts +++ b/src/api/routes/wishlist/read.ts @@ -1,7 +1,7 @@ import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' import { wishlist } from '../../models' -export const getAll = { +export const getAll = { method: 'GET', url: '/', schema: { diff --git a/src/api/routes/wishlist/update.ts b/src/api/routes/wishlist/update.ts index a8775fe..bdc6952 100644 --- a/src/api/routes/wishlist/update.ts +++ b/src/api/routes/wishlist/update.ts @@ -1,13 +1,69 @@ +import { uniqueKeyError } from '../../config/errors' +import { prisma } from '../../services' +import { Wishlist } from '@/types' import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' import { wishlist } from '../../models' -interface GetBySlugUrlTextRequest extends FastifyRequest { +interface updateRequest extends FastifyRequest { + params: { + wishlistId: string + } +} +interface updateItemRequest extends FastifyRequest { params: { wishlistId: string itemId: number } } +export const updateList = { + 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 = { method: 'PUT', url: '/:wishlistId/item/:itemId', @@ -40,7 +96,7 @@ export const updateItem = { }, }, }, - handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => { + handler: async (request: updateItemRequest, reply: FastifyReply) => { request.log.debug(request.body) const item = await wishlist.updateItem( Number(request.params.itemId),