mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Added pagination on Snippets GET route
This commit is contained in:
@@ -220,6 +220,17 @@ describe('GET /v1/snippet/', () => {
|
|||||||
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')
|
||||||
expect(res.body.snippets[0].category_id).toEqual(category_id)
|
expect(res.body.snippets[0].category_id).toEqual(category_id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('returns status code 200 and no snippets', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.get('/v1/snippet?skip=5&take=5')
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Cookie', jwtCookie as string)
|
||||||
|
|
||||||
|
expect(res.status).toEqual(200)
|
||||||
|
expect(res.body.links.next).toEqual('/v1/snippet?start=10&per_page=5')
|
||||||
|
expect(res.body.links.prev).toEqual('/v1/snippet?start=0&per_page=5')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('PUT /v1/snippet/:id', () => {
|
describe('PUT /v1/snippet/:id', () => {
|
||||||
|
|||||||
+37
-11
@@ -3,6 +3,11 @@ import express, { RequestHandler } from "express"
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { userIdMiddleware } from "./user.js"
|
import { userIdMiddleware } from "./user.js"
|
||||||
|
|
||||||
|
interface Pagination {
|
||||||
|
skip: number,
|
||||||
|
take: number
|
||||||
|
}
|
||||||
|
|
||||||
const snippetRouter = express.Router()
|
const snippetRouter = express.Router()
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
@@ -25,6 +30,11 @@ const paramsIdParser = z.object({
|
|||||||
id: z.coerce.number(),
|
id: z.coerce.number(),
|
||||||
}).required()
|
}).required()
|
||||||
|
|
||||||
|
const queryPaginationParser = z.object({
|
||||||
|
skip: z.coerce.number().optional().default(0),
|
||||||
|
take: z.coerce.number().optional().default(10)
|
||||||
|
})
|
||||||
|
|
||||||
function shortenSnippetsTagDepth(snippets: Snippet[]) {
|
function shortenSnippetsTagDepth(snippets: Snippet[]) {
|
||||||
snippets.forEach((snippet: any) => {
|
snippets.forEach((snippet: any) => {
|
||||||
snippet.tags = snippet.Snippet_tag.map((x: any) => ({ ...x.tag }))
|
snippet.tags = snippet.Snippet_tag.map((x: any) => ({ ...x.tag }))
|
||||||
@@ -32,27 +42,43 @@ function shortenSnippetsTagDepth(snippets: Snippet[]) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Ajouter paramètres sur la requête pour rechercher des snippets
|
function getPaginationLinks(query: Pagination): object {
|
||||||
// TODO Ajouter pagination
|
return {
|
||||||
|
next: `/v1/snippet?start=${query.skip + query.take}&per_page=${query.take}`,
|
||||||
|
prev: `/v1/snippet?start=${Math.max(0, query.skip - query.take)}&per_page=${query.take}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findSnippets(userId: number, pagination: Pagination): Promise<Snippet[]> {
|
||||||
|
return await prisma.snippet.findMany({
|
||||||
|
where: { user_id: userId },
|
||||||
|
include: {
|
||||||
|
Snippet_tag: {
|
||||||
|
select: { tag: { select: { id: true, name: true } } },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
...pagination
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const snippetGet: RequestHandler = async (req, res) => {
|
const snippetGet: RequestHandler = async (req, res) => {
|
||||||
let snippets: Snippet[] | null = null
|
let snippets: Snippet[] | null = null
|
||||||
|
const pagination = queryPaginationParser.parse(req.query)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
snippets = await prisma.snippet.findMany({
|
snippets = await findSnippets(req.body.userId, pagination)
|
||||||
where: { user_id: req.body.userId },
|
|
||||||
include: {
|
|
||||||
Snippet_tag: {
|
|
||||||
select: { tag: { select: { id: true, name: true } } },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
shortenSnippetsTagDepth(snippets)
|
shortenSnippetsTagDepth(snippets)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(400).json({ message: (error.issues ?? error) })
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ snippets })
|
res.json({
|
||||||
|
snippets,
|
||||||
|
pagination,
|
||||||
|
links: getPaginationLinks(pagination),
|
||||||
|
total: snippets.length
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const snippetGetUnique: RequestHandler = async (req, res) => {
|
const snippetGetUnique: RequestHandler = async (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user