mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-04-19 23:37:41 +00:00
#1 Add item API Endpoint added
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
56db8ba65a
commit
dea3aaf477
7 changed files with 74 additions and 31 deletions
|
@ -43,3 +43,15 @@ Content-Type: application/json
|
|||
"description": "Juniors Wishlist",
|
||||
"slugUrlText": "junior"
|
||||
}
|
||||
|
||||
###
|
||||
# @name addItemToFirstWishlist
|
||||
POST {{BASE_URL}}/wishlist/{{getWishlists.response.body.0.id}}/item
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"title": "Goldfish 40442 | BrickHeadz",
|
||||
"url": "https://www.lego.com/en-de/product/goldfish-40442",
|
||||
"imageSrc": "https://www.lego.com/cdn/cs/set/assets/blt1fc37afef51cfa9f/40442.jpg?fit=bounds&format=jpg&quality=80&width=1500&height=1500&dpr=1",
|
||||
"description": "Cute goldfish and fry, build-and-display BrickHeadz™ model"
|
||||
}
|
||||
|
|
|
@ -12,30 +12,12 @@ const wishlistData: Prisma.WishlistCreateInput[] = [
|
|||
items: {
|
||||
create: [
|
||||
{
|
||||
title: 'Mr Maria Lion "First Light" Lampe',
|
||||
url: 'https://babykochs.de/mr-maria-lion-first-light-lampe/',
|
||||
title: 'Goldfish 40442 | BrickHeadz',
|
||||
url: 'https://www.lego.com/en-de/product/goldfish-40442',
|
||||
imageSrc:
|
||||
'https://babykochs.de/wp-content/uploads/2021/01/First-Light-Lion1.jpg',
|
||||
'https://www.lego.com/cdn/cs/set/assets/blt1fc37afef51cfa9f/40442.jpg?fit=bounds&format=jpg&quality=80&width=1500&height=1500&dpr=1',
|
||||
description:
|
||||
'Lion ist Teil der Serie "First Light - Miffy und Freunde" Kollektion, eine Nachtlampe, die als "kleiner Freund für ein kleines Wunder" entworfen wurde.',
|
||||
comment: '',
|
||||
},
|
||||
{
|
||||
title: 'Liewood Nachtlicht Winston Bär dove blue',
|
||||
url: 'https://babykochs.de/liewood-nachtlicht-baer/',
|
||||
imageSrc:
|
||||
'https://babykochs.de/wp-content/uploads/2021/01/Liewood-Nachtlicht-Winston-bear-dove-blue.jpg',
|
||||
description:
|
||||
'Mit dem Licht dieses süßen Bären fühlen sich Eure Kleinen in der Nacht ganz sicher und haben’s schön gemütlich.',
|
||||
comment: '',
|
||||
},
|
||||
{
|
||||
title: 'Baby- und Kleinkindbett',
|
||||
url: 'https://www.tchibo.de/baby-und-kleinkindbett-p400114225.html#modal-productimagegallery-modalGalleryImage-400886394',
|
||||
imageSrc:
|
||||
'https://www.tchibo.de/newmedia/art_img/MAIN-IMPORTED/f045f71ebabea9e4/baby-und-kleinkindbett.jpg',
|
||||
description:
|
||||
'Ein Bett, das mitwächst Zu einem erholsamen Schlaf und einer schönen Nacht gehört natürlich auch ein gutes Bett – das gilt auch für die Kleinsten.',
|
||||
'Cute goldfish and fry, build-and-display BrickHeadz™ model',
|
||||
comment: '',
|
||||
},
|
||||
],
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
export const wishlistItemRequestSchema = {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['title', 'imageSrc'],
|
||||
required: ['title', 'description'],
|
||||
properties: {
|
||||
title: { type: 'string' },
|
||||
url: { type: 'string' },
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { prisma } from '../../services'
|
||||
import { Wishlist } from '@/types'
|
||||
import { Wishlist, WishlistItem } from '@/types'
|
||||
|
||||
export default {
|
||||
getAll: async (): Promise<any> => {
|
||||
|
@ -33,6 +33,22 @@ export default {
|
|||
},
|
||||
})
|
||||
},
|
||||
createItem: async (wishlistId: string, payload: WishlistItem) => {
|
||||
const wishlist = await prisma.client.wishlist.update({
|
||||
where: {
|
||||
id: wishlistId,
|
||||
},
|
||||
data: {
|
||||
items: {
|
||||
create: {
|
||||
...payload,
|
||||
},
|
||||
},
|
||||
},
|
||||
include: { items: true },
|
||||
})
|
||||
return wishlist.items.pop()
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
updateItem: async (itemId: number, payload: any) => {
|
||||
return await prisma.client.item.update({
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
import { Wishlist } from '@/types'
|
||||
import { Wishlist, WishlistItem } from '@/types'
|
||||
import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
|
||||
import { wishlist } from '../../models'
|
||||
import { prisma } from '../../services'
|
||||
import { uniqueKeyError } from '../../config/errors'
|
||||
import {
|
||||
wishlistItemRequestSchema,
|
||||
wishlistItemResponseSchema,
|
||||
wishlistRequestSchema,
|
||||
wishlistResponseSchema,
|
||||
} from '../../config/schemas'
|
||||
|
||||
interface createItemRequest extends FastifyRequest {
|
||||
params: {
|
||||
wishlistId: string
|
||||
}
|
||||
}
|
||||
|
||||
export const createList = <RouteOptions>{
|
||||
method: 'POST',
|
||||
url: '/',
|
||||
|
@ -30,3 +38,29 @@ export const createList = <RouteOptions>{
|
|||
reply.code(201).send(item)
|
||||
},
|
||||
}
|
||||
|
||||
export const createItem = <RouteOptions>{
|
||||
method: 'POST',
|
||||
url: '/:wishlistId/item',
|
||||
schema: {
|
||||
body: wishlistItemRequestSchema,
|
||||
response: {
|
||||
201: wishlistItemResponseSchema,
|
||||
},
|
||||
},
|
||||
errorHandler: (error, request, reply) => {
|
||||
if (error instanceof prisma.errorType && error.code === 'P2025') {
|
||||
return reply.callNotFound()
|
||||
}
|
||||
request.log.error(error)
|
||||
reply.send(new Error('Unexptected Error'))
|
||||
},
|
||||
handler: async (request: createItemRequest, reply: FastifyReply) => {
|
||||
request.log.debug(request.body)
|
||||
const item = await wishlist.createItem(
|
||||
request.params.wishlistId,
|
||||
request.body as WishlistItem
|
||||
)
|
||||
reply.code(201).send(item)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import { FastifyInstance } from 'fastify'
|
||||
import { getAll, getBySlugUrl } from './read'
|
||||
import { updateList, updateItem } from './update'
|
||||
import { createList } from './create'
|
||||
import { createList, createItem } from './create'
|
||||
|
||||
export default async (app: FastifyInstance) => {
|
||||
await app.route(getAll)
|
||||
await app.route(getBySlugUrl)
|
||||
await app.route(createList)
|
||||
await app.route(createItem)
|
||||
await app.route(updateList)
|
||||
await app.route(updateItem)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ interface updateRequest extends FastifyRequest {
|
|||
wishlistId: string
|
||||
}
|
||||
}
|
||||
|
||||
interface updateItemRequest extends FastifyRequest {
|
||||
params: {
|
||||
wishlistId: string
|
||||
|
@ -67,12 +68,9 @@ export const updateItem = <RouteOptions>{
|
|||
request.body
|
||||
)
|
||||
if (item) {
|
||||
return item
|
||||
reply.send(item)
|
||||
} else {
|
||||
return reply.code(404).send({
|
||||
error: 'notFound',
|
||||
http: 404,
|
||||
})
|
||||
reply.callNotFound()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue