From 32c16ff6638dcd2813b778218ea9eadcdb106562 Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 5 Feb 2022 09:53:46 +0100 Subject: [PATCH] #1 Create Endpoint added Signed-off-by: Benny Samir Hierl --- examples.http | 33 +++++++++++++++++++ src/api/config/errors/index.ts | 15 +++++++++ src/api/models/wishlist/index.ts | 6 ++++ src/api/routes/wishlist/create.ts | 55 +++++++++++++++++++++++++++++++ src/api/routes/wishlist/index.ts | 2 ++ src/api/services/prisma/index.ts | 4 ++- src/types.ts | 2 +- 7 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 examples.http create mode 100644 src/api/config/errors/index.ts create mode 100644 src/api/routes/wishlist/create.ts diff --git a/examples.http b/examples.http new file mode 100644 index 0000000..966382e --- /dev/null +++ b/examples.http @@ -0,0 +1,33 @@ +@BASE_URL=http://localhost:5000/api + + +### +# @name createWishlistFirst +POST {{BASE_URL}}/wishlist +Content-Type: application/json + +{ + "title": "Junior", + "imageSrc": "https://unsplash.com/photos/JZ51o_-UOY8/download?force=true&w=200", + "slugUrlText": "junior" +} + +### +# @name createWishlistSecond +POST {{BASE_URL}}/wishlist +Content-Type: application/json + +{ + "title": "Wedding", + "imageSrc": "https://unsplash.com/photos/8vaQKYnawHw/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjQ0MDQ4MTIy&force=true&w=200", + "description": "We are getting married", + "slugUrlText": "wedding" +} + +### +# @name getWishlists +GET {{BASE_URL}}/wishlist + +### +# @name getFirstWishlist +GET {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.slugUrlText}} diff --git a/src/api/config/errors/index.ts b/src/api/config/errors/index.ts new file mode 100644 index 0000000..a3d14a2 --- /dev/null +++ b/src/api/config/errors/index.ts @@ -0,0 +1,15 @@ +class httpError extends Error { + code: string + statusCode: number + constructor(message: string, statusCode: number, code: string) { + super(message) + this.name = this.constructor.name + Error.captureStackTrace(this, this.constructor) + this.statusCode = statusCode + this.code = code + } +} + +export const uniqueKeyError = (msg: string, code = '4001') => { + return new httpError(msg, 400, code) +} diff --git a/src/api/models/wishlist/index.ts b/src/api/models/wishlist/index.ts index 35b5ddc..fe7cbcc 100644 --- a/src/api/models/wishlist/index.ts +++ b/src/api/models/wishlist/index.ts @@ -1,4 +1,5 @@ import { prisma } from '../../services' +import { Wishlist } from '@/types' export default { getAll: async (): Promise => { @@ -17,6 +18,11 @@ export default { include: { items: includeItems }, }) }, + create: async (payload: Wishlist) => { + return await prisma.client.wishlist.create({ + 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/create.ts b/src/api/routes/wishlist/create.ts new file mode 100644 index 0000000..f6bbc50 --- /dev/null +++ b/src/api/routes/wishlist/create.ts @@ -0,0 +1,55 @@ +import { Wishlist } from '@/types' +import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify' +import { wishlist } from '../../models' +import { prisma } from '../../services' +import { uniqueKeyError } from '../../config/errors' + +interface GetBySlugUrlTextRequest extends FastifyRequest { + params: { + wishlistId: string + itemId: number + } +} + +export const createList = { + method: 'POST', + url: '/', + schema: { + body: { + type: 'object', + additionalProperties: false, + required: ['title', 'imageSrc', 'slugUrlText'], + properties: { + title: { type: 'string' }, + imageSrc: { type: 'string' }, + description: { type: 'string' }, + slugUrlText: { type: 'string' }, + }, + }, + response: { + 201: { + type: 'object', + properties: { + id: { type: 'string' }, + title: { type: 'string' }, + imageSrc: { type: 'string' }, + description: { type: 'string' }, + slugUrlText: { type: 'string' }, + }, + }, + }, + }, + handler: async (request: GetBySlugUrlTextRequest, reply: FastifyReply) => { + request.log.debug(request.body) + try { + const item = await wishlist.create(request.body as Wishlist) + return item + } catch (error) { + if (error instanceof prisma.errorType && error.code === 'P2002') { + return reply.send(uniqueKeyError('Slugtext has to be unique')) + } + request.log.error(error) + throw new Error('Unexptected Error') + } + }, +} diff --git a/src/api/routes/wishlist/index.ts b/src/api/routes/wishlist/index.ts index 32fd362..1c9e7ee 100644 --- a/src/api/routes/wishlist/index.ts +++ b/src/api/routes/wishlist/index.ts @@ -1,9 +1,11 @@ import { FastifyInstance } from 'fastify' import { getAll, getBySlugUrl } from './read' import { 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(updateItem) } diff --git a/src/api/services/prisma/index.ts b/src/api/services/prisma/index.ts index 3134896..9716a39 100644 --- a/src/api/services/prisma/index.ts +++ b/src/api/services/prisma/index.ts @@ -1,7 +1,9 @@ -import { PrismaClient } from '@prisma/client' +import { PrismaClient, Prisma } from '@prisma/client' const client = new PrismaClient() +const errorType = Prisma.PrismaClientKnownRequestError export default { client, + errorType, } diff --git a/src/types.ts b/src/types.ts index de90a26..5898f69 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,7 @@ export interface WishlistItem { wishlistId: boolean } export interface Wishlist { - id: string + id?: string title: string description: string imageSrc: string