mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
feat: sorting
This commit is contained in:
+1
-1
Submodule snippets-manager-front updated: ec9bc1bb4b...57d431a0ba
+17
-10
@@ -26,19 +26,29 @@ const paramsIdParser = z
|
|||||||
.required()
|
.required()
|
||||||
|
|
||||||
async function findCategories(userId: number, pagination: Pagination): Promise<Category[]> {
|
async function findCategories(userId: number, pagination: Pagination): Promise<Category[]> {
|
||||||
return await prisma.category.findMany({
|
const query: any = {
|
||||||
where: { user_id: userId },
|
where: { user_id: userId },
|
||||||
...pagination,
|
skip: pagination.skip,
|
||||||
})
|
take: pagination.take,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pagination.orderBy) {
|
||||||
|
query.orderBy = [{ [pagination.orderBy]: pagination.direction }]
|
||||||
|
}
|
||||||
|
|
||||||
|
return prisma.category.findMany(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
let categoriesTotal: number = 0
|
||||||
|
let pagination = {} as Pagination
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
pagination = queryPaginationParser.parse(req.query)
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
categories = await findCategories(userId ?? -1, pagination)
|
categories = await findCategories(userId ?? -1, pagination)
|
||||||
|
categoriesTotal = await prisma.category.count({ where: { user_id: 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
|
||||||
@@ -48,7 +58,7 @@ const categoryGet: RequestHandler = async (req, res) => {
|
|||||||
categories,
|
categories,
|
||||||
pagination,
|
pagination,
|
||||||
links: getPaginationLinks(pagination, "category"),
|
links: getPaginationLinks(pagination, "category"),
|
||||||
total: categories.length,
|
total: categoriesTotal,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,22 +122,19 @@ const categoryUpdate: RequestHandler = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const categoryDelete: RequestHandler = async (req, res) => {
|
const categoryDelete: RequestHandler = async (req, res) => {
|
||||||
let deleted: Prisma.BatchPayload
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
deleted = await prisma.category.deleteMany({
|
const deleted = await prisma.category.delete({
|
||||||
where: {
|
where: {
|
||||||
id: paramsIdParser.parse(req.params).id,
|
id: paramsIdParser.parse(req.params).id,
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
res.json({ message: `Category successfully deleted.`, id: deleted.id })
|
||||||
} 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({ message: `${deleted.count} category / categories successfully deleted.` })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
categoryRouter.get("/", userIdMiddleware, categoryGet)
|
categoryRouter.get("/", userIdMiddleware, categoryGet)
|
||||||
|
|||||||
+10
-4
@@ -3,19 +3,25 @@ import { z } from "zod"
|
|||||||
export interface Pagination {
|
export interface Pagination {
|
||||||
skip: number
|
skip: number
|
||||||
take: number
|
take: number
|
||||||
|
orderBy?: string
|
||||||
|
direction: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const queryPaginationParser = z.object({
|
export const queryPaginationParser = z.object({
|
||||||
skip: z.coerce.number().optional().default(0),
|
skip: z.coerce.number().optional().default(0),
|
||||||
take: z.coerce.number().optional().default(10),
|
take: z.coerce.number().optional().default(10),
|
||||||
|
orderBy: z.string().optional(),
|
||||||
|
direction: z.string().optional().default("asc"),
|
||||||
})
|
})
|
||||||
|
|
||||||
export function getPaginationLinks(query: Pagination, routeName: string): object {
|
export function getPaginationLinks(query: Pagination, routeName: string): object {
|
||||||
const nextStart = query.skip + query.take
|
const nextSkip = query.skip + query.take
|
||||||
const prevStart = Math.max(0, query.skip - query.take)
|
const prevSkip = Math.max(0, query.skip - query.take)
|
||||||
|
|
||||||
|
const sorting = query.orderBy ? `&orderBy=${query.orderBy}&direction=${query.direction}` : ""
|
||||||
|
|
||||||
return {
|
return {
|
||||||
next: `/v1/${routeName}?start=${nextStart}&per_page=${query.take}`,
|
next: `/v1/${routeName}?skip=${nextSkip}&take=${query.take}${sorting}`,
|
||||||
prev: `/v1/${routeName}?start=${prevStart}&per_page=${query.take}`,
|
prev: `/v1/${routeName}?skip=${prevSkip}&take=${query.take}${sorting}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user