#1 Create Endpoint added

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-05 09:53:46 +01:00
parent a0f294ad44
commit 32c16ff663
7 changed files with 115 additions and 2 deletions

33
examples.http Normal file
View file

@ -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}}

View file

@ -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)
}

View file

@ -1,4 +1,5 @@
import { prisma } from '../../services'
import { Wishlist } from '@/types'
export default {
getAll: async (): Promise<any> => {
@ -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({

View file

@ -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 = <RouteOptions>{
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')
}
},
}

View file

@ -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)
}

View file

@ -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,
}

View file

@ -9,7 +9,7 @@ export interface WishlistItem {
wishlistId: boolean
}
export interface Wishlist {
id: string
id?: string
title: string
description: string
imageSrc: string