90 lines
1.7 KiB
JavaScript
90 lines
1.7 KiB
JavaScript
export default function listingRequestTranslator(
|
|
headerParams,
|
|
pathParams,
|
|
queryParams,
|
|
body
|
|
) {
|
|
|
|
const {
|
|
id
|
|
} = pathParams || {};
|
|
|
|
const {
|
|
limit = 20,
|
|
offset = 0,
|
|
|
|
species_id,
|
|
breed_id,
|
|
sex,
|
|
min_price,
|
|
max_price,
|
|
status
|
|
} = queryParams || {};
|
|
|
|
// -----------------------------
|
|
// BASE WHERE CLAUSE
|
|
// -----------------------------
|
|
const where = {
|
|
deleted: false
|
|
};
|
|
|
|
// -----------------------------
|
|
// PATH FILTER (single listing)
|
|
// -----------------------------
|
|
if (id) {
|
|
where.id = id;
|
|
}
|
|
|
|
// -----------------------------
|
|
// FILTERS (denormalized columns)
|
|
// -----------------------------
|
|
if (species_id) {
|
|
where.filter_species_id = species_id;
|
|
}
|
|
|
|
if (breed_id) {
|
|
where.filter_breed_id = breed_id;
|
|
}
|
|
|
|
if (sex) {
|
|
where.filter_sex = sex;
|
|
}
|
|
|
|
if (status) {
|
|
where.status = status;
|
|
}
|
|
|
|
// -----------------------------
|
|
// BUILD QUERY JSON
|
|
// -----------------------------
|
|
const query = {
|
|
type: 'json',
|
|
op: 'SELECT',
|
|
table: 'listings',
|
|
|
|
where,
|
|
|
|
orderBy: [
|
|
{ column: 'created_at', direction: 'desc' }
|
|
],
|
|
|
|
limit: Math.min(Number(limit), 100),
|
|
offset: Number(offset)
|
|
};
|
|
|
|
// -----------------------------
|
|
// PRICE RANGE (handled separately)
|
|
// -----------------------------
|
|
// JSON executor extension point:
|
|
// price range is encoded as a special where clause
|
|
if (min_price !== undefined || max_price !== undefined) {
|
|
query.whereRange = {
|
|
column: 'price',
|
|
min: min_price !== undefined ? Number(min_price) : undefined,
|
|
max: max_price !== undefined ? Number(max_price) : undefined
|
|
};
|
|
}
|
|
|
|
return query;
|
|
}
|