mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Pagination into its own page + added to categories
This commit is contained in:
@@ -139,6 +139,17 @@ describe('GET /v1/category/', () => {
|
|||||||
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')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('returns status code 200 and no categories', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.get('/v1/category?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/category?start=10&per_page=5')
|
||||||
|
expect(res.body.links.prev).toEqual('/v1/category?start=0&per_page=5')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /v1/category/:id', () => {
|
describe('GET /v1/category/:id', () => {
|
||||||
|
|||||||
+16
-5
@@ -1,6 +1,7 @@
|
|||||||
import { Category, Prisma, PrismaClient } from "@prisma/client"
|
import { Category, Prisma, PrismaClient } from "@prisma/client"
|
||||||
import express, { RequestHandler } from "express"
|
import express, { RequestHandler } from "express"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { getPaginationLinks, Pagination, queryPaginationParser } from "../utils/pagination.js"
|
||||||
import { userIdMiddleware } from "./user.js"
|
import { userIdMiddleware } from "./user.js"
|
||||||
|
|
||||||
const categoryRouter = express.Router()
|
const categoryRouter = express.Router()
|
||||||
@@ -18,20 +19,30 @@ const paramsIdParser = z.object({
|
|||||||
id: z.coerce.number(),
|
id: z.coerce.number(),
|
||||||
}).required()
|
}).required()
|
||||||
|
|
||||||
// TODO Ajouter pagination et queries de recherche
|
async function findCategories(userId: number, pagination: Pagination): Promise<Category[]> {
|
||||||
|
return await prisma.category.findMany({
|
||||||
|
where: { user_id: userId },
|
||||||
|
...pagination
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const categoryGet: RequestHandler = async (req, res) => {
|
const categoryGet: RequestHandler = async (req, res) => {
|
||||||
let categories: Category[] | null = null
|
let categories: Category[] | null = null
|
||||||
|
const pagination = queryPaginationParser.parse(req.query)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categories = await prisma.category.findMany({
|
categories = await findCategories(req.body.userId, pagination)
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ categories })
|
res.json({
|
||||||
|
categories,
|
||||||
|
pagination,
|
||||||
|
links: getPaginationLinks(pagination, 'category'),
|
||||||
|
total: categories.length
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const categoryGetUnique: RequestHandler = async (req, res) => {
|
const categoryGetUnique: RequestHandler = async (req, res) => {
|
||||||
|
|||||||
+2
-18
@@ -1,13 +1,9 @@
|
|||||||
import { Prisma, PrismaClient, Snippet } from "@prisma/client"
|
import { Prisma, PrismaClient, Snippet } from "@prisma/client"
|
||||||
import express, { RequestHandler } from "express"
|
import express, { RequestHandler } from "express"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { Pagination, getPaginationLinks, queryPaginationParser } from "../utils/pagination.js"
|
||||||
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()
|
||||||
|
|
||||||
@@ -30,11 +26,6 @@ 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 }))
|
||||||
@@ -42,13 +33,6 @@ function shortenSnippetsTagDepth(snippets: Snippet[]) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPaginationLinks(query: Pagination): object {
|
|
||||||
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[]> {
|
async function findSnippets(userId: number, pagination: Pagination): Promise<Snippet[]> {
|
||||||
return await prisma.snippet.findMany({
|
return await prisma.snippet.findMany({
|
||||||
where: { user_id: userId },
|
where: { user_id: userId },
|
||||||
@@ -76,7 +60,7 @@ const snippetGet: RequestHandler = async (req, res) => {
|
|||||||
res.json({
|
res.json({
|
||||||
snippets,
|
snippets,
|
||||||
pagination,
|
pagination,
|
||||||
links: getPaginationLinks(pagination),
|
links: getPaginationLinks(pagination, 'snippet'),
|
||||||
total: snippets.length
|
total: snippets.length
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
export interface Pagination {
|
||||||
|
skip: number,
|
||||||
|
take: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export const queryPaginationParser = z.object({
|
||||||
|
skip: z.coerce.number().optional().default(0),
|
||||||
|
take: z.coerce.number().optional().default(10)
|
||||||
|
})
|
||||||
|
|
||||||
|
export function getPaginationLinks(query: Pagination, routeName: string): object {
|
||||||
|
return {
|
||||||
|
next: `/v1/${routeName}?start=${query.skip + query.take}&per_page=${query.take}`,
|
||||||
|
prev: `/v1/${routeName}?start=${Math.max(0, query.skip - query.take)}&per_page=${query.take}`
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user