mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-06-07 05:57:41 +00:00
#1 Create Endpoint added
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
a0f294ad44
commit
32c16ff663
7 changed files with 115 additions and 2 deletions
33
examples.http
Normal file
33
examples.http
Normal 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}}
|
15
src/api/config/errors/index.ts
Normal file
15
src/api/config/errors/index.ts
Normal 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)
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import { prisma } from '../../services'
|
import { prisma } from '../../services'
|
||||||
|
import { Wishlist } from '@/types'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getAll: async (): Promise<any> => {
|
getAll: async (): Promise<any> => {
|
||||||
|
@ -17,6 +18,11 @@ export default {
|
||||||
include: { items: includeItems },
|
include: { items: includeItems },
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
create: async (payload: Wishlist) => {
|
||||||
|
return await prisma.client.wishlist.create({
|
||||||
|
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({
|
||||||
|
|
55
src/api/routes/wishlist/create.ts
Normal file
55
src/api/routes/wishlist/create.ts
Normal 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')
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
import { FastifyInstance } from 'fastify'
|
import { FastifyInstance } from 'fastify'
|
||||||
import { getAll, getBySlugUrl } from './read'
|
import { getAll, getBySlugUrl } from './read'
|
||||||
import { updateItem } from './update'
|
import { updateItem } from './update'
|
||||||
|
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(updateItem)
|
await app.route(updateItem)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { PrismaClient } from '@prisma/client'
|
import { PrismaClient, Prisma } from '@prisma/client'
|
||||||
|
|
||||||
const client = new PrismaClient()
|
const client = new PrismaClient()
|
||||||
|
const errorType = Prisma.PrismaClientKnownRequestError
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
client,
|
client,
|
||||||
|
errorType,
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ export interface WishlistItem {
|
||||||
wishlistId: boolean
|
wishlistId: boolean
|
||||||
}
|
}
|
||||||
export interface Wishlist {
|
export interface Wishlist {
|
||||||
id: string
|
id?: string
|
||||||
title: string
|
title: string
|
||||||
description: string
|
description: string
|
||||||
imageSrc: string
|
imageSrc: string
|
||||||
|
|
Loading…
Add table
Reference in a new issue