Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/server/controllers/lhcPeriodStatistics.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const listLhcPeriodStatisticsHandler = async (req, res) => {
);
if (validatedDTO) {
try {
const { filter, page: { limit = ApiConfig.pagination.limit, offset } = {}, sort = { name: 'DESC' } } = validatedDTO.query;
const { filter, page: { limit = ApiConfig.pagination.limit, offset } = {}, sort = { id: 'DESC' } } = validatedDTO.query;
const { count, rows: items } = await lhcPeriodStatisticsService.getAllForPhysicsRuns({
filter,
limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ class LhcPeriodStatisticsService {
sort,
} = {}) {
const queryBuilder = this.prepareQueryBuilder();

if (sort) {
for (const property in sort) {
let expression;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that all the current expressions are hardcoded (and presumably all future expressions too), I'd argue that you could safely replace the switch statement with a lookup table where you pass the result to a sequelize.literal() function call.

        const expressionMap = {
            id: '`lhcPeriod`.`id`',
            name: '`lhcPeriod`.`name`',
            year: 'SUBSTRING(lhcPeriod.name, 4, 2)',
            pdpBeamTypes: 'pdpBeamTypes',
        };

        if (sort) {
            for (const property in sort) {
                let expression;

                if (property in expressionMap) {
                    expression = (sequelize) => sequelize.literal(expressionMap[property]);
                }

                queryBuilder.orderBy(expression ?? property, sort[property]);
            }
        }

switch (property) {
case 'id':
expression = (sequelize) => sequelize.col('`lhcPeriod`.`id`');;
break;
case 'name':
expression = (sequelize) => sequelize.col('`lhcPeriod`.`name`');
break;
Expand Down
Loading