Changeset 59f8f5a


Ignore:
Timestamp:
12/24/25 23:41:42 (7 months ago)
Author:
Tome <gjorgievtome@…>
Branches:
main
Children:
2c6d75a
Parents:
3fca46c
Message:

update getApprovedBuilds

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • database/drizzle/queries/builds.ts

    r3fca46c r59f8f5a  
    88    reviewsTable, usersTable
    99} from "../schema";
    10 import {eq, desc, and, sql, ilike} from "drizzle-orm";
     10import {eq, desc, and, sql, ilike, asc} from "drizzle-orm";
    1111import {inArray} from "drizzle-orm/sql/expressions/conditions";
    1212
     
    8585}
    8686
    87 export async function getApprovedBuilds(db: Database, limit?: number, q?: string) {
     87export async function getApprovedBuilds(db: Database, limit?: number, sort?: string, q?: string) {
    8888    let queryConditions = [eq(buildsTable.isApproved, true)];
     89    let sortConditions = [];
    8990
    9091    if (q) {
     
    9394        );
    9495    }
     96
     97    switch(sort) {
     98        case 'price_asc':
     99            sortConditions.push(
     100                asc(buildsTable.totalPrice)
     101            );
     102            break;
     103        case 'price_desc':
     104            sortConditions.push(
     105                desc(buildsTable.totalPrice)
     106            );
     107            break;
     108        case 'rating_desc':
     109            sortConditions.push(
     110                desc(sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)`)
     111            );
     112            break;
     113        case 'oldest':
     114            sortConditions.push(
     115                asc(buildsTable.createdAt)
     116            );
     117            break;
     118        case 'newest':
     119            sortConditions.push(
     120                desc(buildsTable.createdAt)
     121            );
     122            break;
     123        default:
     124            sortConditions.push(
     125                desc(buildsTable.createdAt)
     126            );
     127            break;
     128        }
    95129
    96130    const approvedBuildsList = await db
     
    104138        })
    105139        .from(buildsTable)
     140        .leftJoin(
     141            ratingBuildsTable,
     142            eq(buildsTable.id, ratingBuildsTable.buildId)
     143        )
     144        .groupBy(
     145            buildsTable.userId,
     146            buildsTable.name,
     147            buildsTable.createdAt,
     148            buildsTable.totalPrice
     149        )
    106150        .where(
    107151            and (
     
    110154        )
    111155        .orderBy(
    112             desc(buildsTable.totalPrice)
     156            ...sortConditions
    113157        )
    114158        .limit(limit || 100); // 100 placeholder
  • pages/+Layout.telefunc.ts

    r3fca46c r59f8f5a  
    4343}
    4444
    45 export async function onGetApprovedBuilds({ limit, q }
    46                                                  : { limit?: number; q?: string }) {
     45export async function onGetApprovedBuilds({ limit, sort, q }
     46                                                 : { limit?: number; sort: string; q?: string }) {
    4747    const context = ctx();
    4848
    49     const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, q);
     49    const approvedBuilds = await drizzleQueries.getApprovedBuilds(context.db, limit, sort, q);
    5050
    5151    return approvedBuilds;
Note: See TracChangeset for help on using the changeset viewer.