yeah
This commit is contained in:
41
server/api/cards/[esid].ts
Normal file
41
server/api/cards/[esid].ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { useDB, schema } from '../../db'
|
||||
|
||||
export default eventHandler(async (event) => {
|
||||
const esid = getRouterParam(event, 'esid')
|
||||
|
||||
if (!esid) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'ESID manquant dans la requête'
|
||||
})
|
||||
}
|
||||
|
||||
const db = useDB()
|
||||
const card = await db.select().from(schema.cards).where(eq(schema.cards.esid, esid)).get()
|
||||
|
||||
if (!card) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Morceau non trouvé'
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
id: card.id,
|
||||
esid: card.esid,
|
||||
title: card.title,
|
||||
artist: card.artist,
|
||||
url_audio: card.url_audio,
|
||||
url_image: card.url_image,
|
||||
year: card.year,
|
||||
month: card.month,
|
||||
day: card.day,
|
||||
hour: card.hour,
|
||||
slug: card.slug,
|
||||
suit: card.suit,
|
||||
rank: card.rank,
|
||||
createdAt: card.createdAt,
|
||||
updatedAt: card.updatedAt
|
||||
}
|
||||
})
|
||||
84
server/api/cards/index.ts
Normal file
84
server/api/cards/index.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { and, eq, ilike, or, sql } from 'drizzle-orm'
|
||||
import { useDB, schema } from '../../db'
|
||||
|
||||
const PAGE_SIZE = 20 // Nombre d'éléments par page
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const page = Number(query.page) || 1
|
||||
const search = query.search?.toString()
|
||||
const cardRank = query.rank?.toString()
|
||||
const cardSuit = query.suit?.toString()
|
||||
const year = query.year?.toString()
|
||||
|
||||
const db = useDB()
|
||||
const offset = (page - 1) * PAGE_SIZE
|
||||
|
||||
// Log pour débogage
|
||||
console.log('Requête avec paramètres:', { search, cardRank, cardSuit, year })
|
||||
console.log('Schéma des cards:', Object.keys(schema.cards))
|
||||
|
||||
// Construction des conditions de filtrage
|
||||
const conditions = []
|
||||
|
||||
if (search) {
|
||||
const searchTerm = `%${search}%`
|
||||
conditions.push(
|
||||
or(ilike(schema.cards.title, searchTerm), ilike(schema.cards.artist, searchTerm))
|
||||
)
|
||||
}
|
||||
|
||||
if (cardRank) {
|
||||
conditions.push(eq(schema.cards.rank, cardRank))
|
||||
}
|
||||
|
||||
if (cardSuit) {
|
||||
conditions.push(eq(schema.cards.suit, cardSuit))
|
||||
}
|
||||
|
||||
if (year) {
|
||||
conditions.push(eq(schema.cards.year, year))
|
||||
}
|
||||
|
||||
// Requête pour le comptage total
|
||||
const countQuery = db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(schema.cards)
|
||||
.$dynamic()
|
||||
|
||||
// Log pour débogage SQL
|
||||
console.log('Requête de comptage SQL:', countQuery.toSQL())
|
||||
|
||||
// Requête pour les données paginées
|
||||
const cardsQuery = db
|
||||
.select()
|
||||
.from(schema.cards)
|
||||
.$dynamic()
|
||||
.limit(PAGE_SIZE)
|
||||
.offset(offset)
|
||||
.orderBy(schema.cards.title)
|
||||
|
||||
// Application des conditions si elles existent
|
||||
if (conditions.length > 0) {
|
||||
const where = and(...conditions)
|
||||
countQuery.where(where)
|
||||
cardsQuery.where(where)
|
||||
}
|
||||
|
||||
const [countResult, cards] = await Promise.all([countQuery, cardsQuery])
|
||||
|
||||
const totalItems = countResult[0]?.count || 0
|
||||
const totalPages = Math.ceil(totalItems / PAGE_SIZE)
|
||||
|
||||
return {
|
||||
data: cards,
|
||||
pagination: {
|
||||
currentPage: page,
|
||||
pageSize: PAGE_SIZE,
|
||||
totalItems,
|
||||
totalPages,
|
||||
hasNextPage: page < totalPages,
|
||||
hasPreviousPage: page > 1
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user