Changeset 83fb5e2 for database/drizzle/queries/builds.ts
- Timestamp:
- 12/25/25 01:49:51 (7 months ago)
- Branches:
- main
- Children:
- e7f1bfa
- Parents:
- d4842f4
- File:
-
- 1 edited
-
database/drizzle/queries/builds.ts (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database/drizzle/queries/builds.ts
rd4842f4 r83fb5e2 46 46 47 47 export async function setBuildApprovalStatus(db: Database, buildId: number, isApproved: boolean){ 48 const result= await db48 const [result] = await db 49 49 .update(buildsTable) 50 50 .set({ … … 61 61 }) 62 62 63 return result .length;63 return result?.id ?? null; 64 64 } 65 65 … … 135 135 created_at: buildsTable.createdAt, 136 136 total_price: buildsTable.totalPrice, 137 avgRating: sql<number>` AVG(${ratingBuildsTable.value}::float)`137 avgRating: sql<number>`COALESCE(AVG(${ratingBuildsTable.value}::float),0)` 138 138 }) 139 139 .from(buildsTable) … … 160 160 161 161 return approvedBuildsList; 162 }163 164 export async function getHighestRankedBuilds(db: Database, limit? : number) {165 const highestRankedBuildsList = await db166 .select({167 id: buildsTable.id,168 user_id: buildsTable.userId,169 name: buildsTable.name,170 created_at: buildsTable.createdAt,171 total_price: buildsTable.totalPrice,172 avgRating: sql<number>`AVG(${ratingBuildsTable.value}::float)`173 })174 .from(buildsTable)175 .innerJoin(176 ratingBuildsTable,177 eq(buildsTable.id, ratingBuildsTable.buildId)178 )179 .where(180 eq(buildsTable.isApproved, true)181 )182 .groupBy(buildsTable.id)183 .orderBy(184 desc(sql<number>`AVG(${ratingBuildsTable.value}::float)`)185 )186 .limit(limit || 100); // 100 placeholder187 188 return highestRankedBuildsList;189 162 } 190 163 … … 335 308 .limit(1); 336 309 337 if (existing.length ) {310 if (existing.length > 0) { 338 311 await db 339 312 .delete(favoriteBuildsTable) … … 345 318 ); 346 319 347 return { favorite: false };320 return null; 348 321 } 349 322 … … 355 328 }); 356 329 357 return { favorite: true };330 return true; 358 331 } 359 332 … … 378 351 }) 379 352 380 return result ;353 return result ?? null; 381 354 } 382 355 … … 404 377 }) 405 378 406 return result ;379 return result ?? null; 407 380 } 408 381 … … 498 471 499 472 export async function deleteBuild(db: Database, userId: number, buildId: number) { 500 const result= await db473 const [result] = await db 501 474 .delete(buildsTable) 502 475 .where( … … 510 483 }) 511 484 512 return result .length;485 return result?.id ?? null; 513 486 } 514 487 … … 550 523 } 551 524 552 return newBuild .id;525 return newBuild?.id ?? null; 553 526 }); 554 527 }
Note:
See TracChangeset
for help on using the changeset viewer.
