mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Separated get/:id from get/
This commit is contained in:
@@ -127,7 +127,7 @@ describe('POST /v1/category', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /v1/category/:id?', () => {
|
describe('GET /v1/category/', () => {
|
||||||
it('returns status code 200 and the categories', async () => {
|
it('returns status code 200 and the categories', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.get('/v1/category')
|
.get('/v1/category')
|
||||||
@@ -139,7 +139,9 @@ describe('GET /v1/category/:id?', () => {
|
|||||||
expect(res.status).toEqual(200)
|
expect(res.status).toEqual(200)
|
||||||
expect(res.body.categories[0].name).toEqual('VueJS Composition API')
|
expect(res.body.categories[0].name).toEqual('VueJS Composition API')
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('GET /v1/category/:id', () => {
|
||||||
it('returns status code 200 and the categories', async () => {
|
it('returns status code 200 and the categories', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.get(`/v1/category/${category_id}`)
|
.get(`/v1/category/${category_id}`)
|
||||||
@@ -147,7 +149,7 @@ describe('GET /v1/category/:id?', () => {
|
|||||||
.set('Cookie', jwtCookie as string)
|
.set('Cookie', jwtCookie as string)
|
||||||
|
|
||||||
expect(res.status).toEqual(200)
|
expect(res.status).toEqual(200)
|
||||||
expect(res.body.categories[0].name).toEqual('VueJS Composition API')
|
expect(res.body.category.name).toEqual('VueJS Composition API')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -215,7 +217,7 @@ describe('POST /v1/snippet', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /v1/snippet/:id?', () => {
|
describe('GET /v1/snippet/', () => {
|
||||||
it('returns status code 200 and all snippets', async () => {
|
it('returns status code 200 and all snippets', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.get('/v1/snippet')
|
.get('/v1/snippet')
|
||||||
@@ -227,7 +229,9 @@ describe('GET /v1/snippet/:id?', () => {
|
|||||||
expect(res.status).toEqual(200)
|
expect(res.status).toEqual(200)
|
||||||
expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style')
|
expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style')
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('GET /v1/snippet/:id', () => {
|
||||||
it('returns status code 200 and one snippet', async () => {
|
it('returns status code 200 and one snippet', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.get(`/v1/snippet/${snippet_id}`)
|
.get(`/v1/snippet/${snippet_id}`)
|
||||||
@@ -235,7 +239,7 @@ describe('GET /v1/snippet/:id?', () => {
|
|||||||
.set('Cookie', jwtCookie as string)
|
.set('Cookie', jwtCookie as string)
|
||||||
|
|
||||||
expect(res.status).toEqual(200)
|
expect(res.status).toEqual(200)
|
||||||
expect(res.body.snippets[0].title).toEqual('Vue3 CompAPI TS script-template-style')
|
expect(res.body.snippet.title).toEqual('Vue3 CompAPI TS script-template-style')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+23
-7
@@ -21,14 +21,11 @@ const paramsIdParser = z.object({
|
|||||||
// TODO Ajouter pagination et queries de recherche
|
// TODO Ajouter pagination et queries de recherche
|
||||||
const categoryGet: RequestHandler = async (req, res) => {
|
const categoryGet: RequestHandler = async (req, res) => {
|
||||||
let categories: Category[] | null = null
|
let categories: Category[] | null = null
|
||||||
const whereClause: any = { user_id: req.body.userId }
|
|
||||||
|
|
||||||
if (req.params.id != undefined) {
|
|
||||||
whereClause.id = paramsIdParser.parse(req.params).id
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categories = await prisma.category.findMany({ where: whereClause })
|
categories = await prisma.category.findMany({
|
||||||
|
where: { user_id: req.body.userId }
|
||||||
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: (error.issues ?? error) })
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
return;
|
return;
|
||||||
@@ -37,6 +34,24 @@ const categoryGet: RequestHandler = async (req, res) => {
|
|||||||
res.json({ categories })
|
res.json({ categories })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const categoryGetUnique: RequestHandler = async (req, res) => {
|
||||||
|
let category: Category | null = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
category = await prisma.category.findFirst({
|
||||||
|
where: {
|
||||||
|
id: paramsIdParser.parse(req.params).id,
|
||||||
|
user_id: req.body.userId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ category })
|
||||||
|
}
|
||||||
|
|
||||||
const categoryPost: RequestHandler = async (req, res) => {
|
const categoryPost: RequestHandler = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const newCategory = categoryPostParser.parse(req.body)
|
const newCategory = categoryPostParser.parse(req.body)
|
||||||
@@ -94,7 +109,8 @@ const categoryDelete: RequestHandler = async (req, res) => {
|
|||||||
res.json({ message: `${deleted.count} category / categories successfully deleted.` })
|
res.json({ message: `${deleted.count} category / categories successfully deleted.` })
|
||||||
}
|
}
|
||||||
|
|
||||||
categoryRouter.get('/:id?', userIdMiddleware, categoryGet)
|
categoryRouter.get('/', userIdMiddleware, categoryGet)
|
||||||
|
categoryRouter.get('/:id', userIdMiddleware, categoryGetUnique)
|
||||||
categoryRouter.post('/', userIdMiddleware, categoryPost)
|
categoryRouter.post('/', userIdMiddleware, categoryPost)
|
||||||
categoryRouter.put('/:id', userIdMiddleware, categoryUpdate)
|
categoryRouter.put('/:id', userIdMiddleware, categoryUpdate)
|
||||||
categoryRouter.delete('/:id', userIdMiddleware, categoryDelete)
|
categoryRouter.delete('/:id', userIdMiddleware, categoryDelete)
|
||||||
|
|||||||
+23
-7
@@ -26,14 +26,11 @@ const paramsIdParser = z.object({
|
|||||||
// TODO Ajouter pagination
|
// TODO Ajouter pagination
|
||||||
const snippetGet: RequestHandler = async (req, res) => {
|
const snippetGet: RequestHandler = async (req, res) => {
|
||||||
let snippets: Snippet[] | null = null
|
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 {
|
try {
|
||||||
snippets = await prisma.snippet.findMany({ where: whereClause })
|
snippets = await prisma.snippet.findMany({
|
||||||
|
where: { user_id: req.body.userId }
|
||||||
|
})
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: (error.issues ?? error) })
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
return;
|
return;
|
||||||
@@ -42,6 +39,24 @@ const snippetGet: RequestHandler = async (req, res) => {
|
|||||||
res.json({ snippets })
|
res.json({ snippets })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const snippetGetUnique: RequestHandler = async (req, res) => {
|
||||||
|
let snippet: Snippet | null = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
snippet = await prisma.snippet.findFirst({
|
||||||
|
where: {
|
||||||
|
id: paramsIdParser.parse(req.params).id,
|
||||||
|
user_id: req.body.userId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ snippet })
|
||||||
|
}
|
||||||
|
|
||||||
const snippetPost: RequestHandler = async (req, res) => {
|
const snippetPost: RequestHandler = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const newSnippet = snippetPostParser.parse(req.body)
|
const newSnippet = snippetPostParser.parse(req.body)
|
||||||
@@ -106,7 +121,8 @@ const snippetDelete: RequestHandler = async (req, res) => {
|
|||||||
res.json({ message: `${deleted.count} snippet(s) successfully deleted.` })
|
res.json({ message: `${deleted.count} snippet(s) successfully deleted.` })
|
||||||
}
|
}
|
||||||
|
|
||||||
snippetRouter.get('/:id?', userIdMiddleware, snippetGet)
|
snippetRouter.get('/', userIdMiddleware, snippetGet)
|
||||||
|
snippetRouter.get('/:id', userIdMiddleware, snippetGetUnique)
|
||||||
snippetRouter.post('/', userIdMiddleware, snippetPost)
|
snippetRouter.post('/', userIdMiddleware, snippetPost)
|
||||||
snippetRouter.put('/:id', userIdMiddleware, snippetUpdate)
|
snippetRouter.put('/:id', userIdMiddleware, snippetUpdate)
|
||||||
snippetRouter.delete('/:id', userIdMiddleware, snippetDelete)
|
snippetRouter.delete('/:id', userIdMiddleware, snippetDelete)
|
||||||
|
|||||||
Reference in New Issue
Block a user