1- import { error , type RequestEvent } from '@sveltejs/kit'
1+ import { error } from '@sveltejs/kit'
22import { query } from '$lib/server/db.catdat'
33import { render_nested_formulas } from '$lib/server/formulas'
44import { MAX_STRUCTURES_COMPARE } from '$lib/commons/compare.utils'
55import type { ComparisonResult , StructureType } from '$lib/commons/types'
66import { PLURALS } from '$lib/commons/structures'
7+ import { to_placeholders } from '$lib/server/utils'
78
8- export function compare_handler (
9- event : RequestEvent ,
9+ export function fetch_comparison_result (
10+ compared_ids : string [ ] ,
1011 type : StructureType ,
12+ callback : ( ) => void ,
1113) : ComparisonResult {
12- if ( ! event . params . ids ) error ( 400 , `No ${ type } selected for comparison` )
13-
14- const compared_ids = event . params . ids ?. split ( '/' )
15-
1614 if ( ! compared_ids . length ) error ( 400 , `No ${ type } selected for comparison` )
1715
1816 if ( compared_ids . length > MAX_STRUCTURES_COMPARE ) {
@@ -22,8 +20,6 @@ export function compare_handler(
2220 )
2321 }
2422
25- const placeholders = compared_ids . map ( ( ) => '?' ) . join ( ', ' )
26-
2723 const { rows, err : err_cat } = query < {
2824 id : string
2925 name : string
@@ -32,7 +28,8 @@ export function compare_handler(
3228 sql : `
3329 SELECT id, name, notation
3430 FROM structures
35- WHERE type = ? AND id in (${ placeholders } )` ,
31+ WHERE type = ?
32+ AND id in (${ to_placeholders ( compared_ids ) } )` ,
3633 values : [ type , ...compared_ids ] ,
3734 } )
3835
@@ -69,7 +66,7 @@ export function compare_handler(
6966
7067 values . push ( type )
7168
72- const stmt = `
69+ const comparison_query = `
7370 SELECT
7471 p.id AS property_id,
7572 ${ select_columns }
@@ -79,18 +76,15 @@ export function compare_handler(
7976 ORDER BY lower(p.id)`
8077
8178 const { rows : comparison_objects , err } = query < Record < string , string > > ( {
82- sql : stmt ,
79+ sql : comparison_query ,
8380 values,
8481 } )
8582
86- if ( err ) error ( 500 , 'Could not load properties ' )
83+ if ( err ) error ( 500 , 'Could not load comparison table ' )
8784
8885 const comparison_table = comparison_objects . map ( ( obj ) => Object . values ( obj ) )
8986
90- event . setHeaders ( {
91- // shared cache for 30min
92- 'cache-control' : 'public, max-age=0, s-maxage=1800' ,
93- } )
87+ callback ( )
9488
9589 return {
9690 structures : render_nested_formulas ( structures ) ,
0 commit comments