mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Added snippet GET by ud
This commit is contained in:
@@ -223,8 +223,8 @@ describe('POST /v1/snippet', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /v1/snippet', () => {
|
||||
it('returns status code 200 and success message', async () => {
|
||||
describe('GET /v1/snippet/:id?', () => {
|
||||
it('returns status code 200 and all snippets', async () => {
|
||||
const res = await request(app)
|
||||
.get('/v1/snippet')
|
||||
.set('Content-Type', 'application/json')
|
||||
@@ -235,6 +235,16 @@ describe('GET /v1/snippet', () => {
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style')
|
||||
})
|
||||
|
||||
it('returns status code 200 and one snippet', async () => {
|
||||
const res = await request(app)
|
||||
.get(`/v1/snippet/${snippet_id}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style')
|
||||
})
|
||||
})
|
||||
|
||||
describe('PUT /v1/snippet/:id', () => {
|
||||
@@ -248,8 +258,6 @@ describe('PUT /v1/snippet/:id', () => {
|
||||
// language: 'html'
|
||||
}))
|
||||
|
||||
console.debug(res.body)
|
||||
|
||||
expect(res.status).toEqual(200)
|
||||
expect(res.body).toHaveProperty('message')
|
||||
})
|
||||
|
||||
@@ -26,9 +26,14 @@ const paramsIdParser = z.object({
|
||||
// TODO Ajouter pagination
|
||||
const snippetGet: RequestHandler = async (req, res) => {
|
||||
let snippets: Snippet[] | null = null
|
||||
const whereClause: any = { user_id: req.body.userId }
|
||||
|
||||
if (req.params.id != undefined) {
|
||||
whereClause.id = paramsIdParser.parse(req.params).id
|
||||
}
|
||||
|
||||
try {
|
||||
snippets = await prisma.snippet.findMany({ where: { user_id: req.body.userId } })
|
||||
snippets = await prisma.snippet.findMany({ where: whereClause })
|
||||
} catch (error: any) {
|
||||
res.status(400).json({ message: (error.issues ?? error) })
|
||||
return;
|
||||
@@ -101,7 +106,7 @@ const snippetDelete: RequestHandler = async (req, res) => {
|
||||
res.json({ message: `${deleted.count} snippet(s) successfully deleted.` })
|
||||
}
|
||||
|
||||
snippetRouter.get('/', userIdMiddleware, snippetGet)
|
||||
snippetRouter.get('/:id?', userIdMiddleware, snippetGet)
|
||||
snippetRouter.post('/', userIdMiddleware, snippetPost)
|
||||
snippetRouter.put('/:id', userIdMiddleware, snippetUpdate)
|
||||
snippetRouter.delete('/:id', userIdMiddleware, snippetDelete)
|
||||
|
||||
Reference in New Issue
Block a user