@@ -3,7 +3,10 @@ use std::collections::HashMap;
33use async_graphql:: dataloader:: Loader ;
44
55use crate :: db:: {
6- models:: { DbEquipment , DbMechData } ,
6+ models:: {
7+ DbArmorType , DbCockpitType , DbEngineType , DbEquipment , DbGyroType , DbHeatsinkType ,
8+ DbMechData , DbMyomerType , DbStructureType ,
9+ } ,
710 units,
811} ;
912
@@ -94,3 +97,159 @@ impl Loader<i32> for AmmoTypesLoader {
9497 Ok ( map)
9598 }
9699}
100+
101+ // ── Component Type Loaders (for MechData FK resolution) ──────────────────────
102+
103+ pub struct EngineTypeLoader {
104+ pub pool : sqlx:: PgPool ,
105+ }
106+
107+ impl Loader < i32 > for EngineTypeLoader {
108+ type Value = DbEngineType ;
109+ type Error = async_graphql:: Error ;
110+
111+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbEngineType > , async_graphql:: Error > {
112+ let rows = sqlx:: query_as :: < _ , DbEngineType > (
113+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
114+ rules_level::text AS rules_level,
115+ weight_multiplier, ct_crits, st_crits, intro_year
116+ FROM engine_types WHERE id = ANY($1)"# ,
117+ )
118+ . bind ( keys)
119+ . fetch_all ( & self . pool )
120+ . await ?;
121+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
122+ }
123+ }
124+
125+ pub struct ArmorTypeLoader {
126+ pub pool : sqlx:: PgPool ,
127+ }
128+
129+ impl Loader < i32 > for ArmorTypeLoader {
130+ type Value = DbArmorType ;
131+ type Error = async_graphql:: Error ;
132+
133+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbArmorType > , async_graphql:: Error > {
134+ let rows = sqlx:: query_as :: < _ , DbArmorType > (
135+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
136+ rules_level::text AS rules_level,
137+ points_per_ton, crits, intro_year
138+ FROM armor_types WHERE id = ANY($1)"# ,
139+ )
140+ . bind ( keys)
141+ . fetch_all ( & self . pool )
142+ . await ?;
143+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
144+ }
145+ }
146+
147+ pub struct StructureTypeLoader {
148+ pub pool : sqlx:: PgPool ,
149+ }
150+
151+ impl Loader < i32 > for StructureTypeLoader {
152+ type Value = DbStructureType ;
153+ type Error = async_graphql:: Error ;
154+
155+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbStructureType > , async_graphql:: Error > {
156+ let rows = sqlx:: query_as :: < _ , DbStructureType > (
157+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
158+ rules_level::text AS rules_level,
159+ weight_fraction, crits, intro_year
160+ FROM structure_types WHERE id = ANY($1)"# ,
161+ )
162+ . bind ( keys)
163+ . fetch_all ( & self . pool )
164+ . await ?;
165+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
166+ }
167+ }
168+
169+ pub struct HeatsinkTypeLoader {
170+ pub pool : sqlx:: PgPool ,
171+ }
172+
173+ impl Loader < i32 > for HeatsinkTypeLoader {
174+ type Value = DbHeatsinkType ;
175+ type Error = async_graphql:: Error ;
176+
177+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbHeatsinkType > , async_graphql:: Error > {
178+ let rows = sqlx:: query_as :: < _ , DbHeatsinkType > (
179+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
180+ rules_level::text AS rules_level,
181+ dissipation, crits, weight, intro_year
182+ FROM heatsink_types WHERE id = ANY($1)"# ,
183+ )
184+ . bind ( keys)
185+ . fetch_all ( & self . pool )
186+ . await ?;
187+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
188+ }
189+ }
190+
191+ pub struct GyroTypeLoader {
192+ pub pool : sqlx:: PgPool ,
193+ }
194+
195+ impl Loader < i32 > for GyroTypeLoader {
196+ type Value = DbGyroType ;
197+ type Error = async_graphql:: Error ;
198+
199+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbGyroType > , async_graphql:: Error > {
200+ let rows = sqlx:: query_as :: < _ , DbGyroType > (
201+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
202+ rules_level::text AS rules_level,
203+ weight_multiplier, crits, is_superheavy_only, intro_year
204+ FROM gyro_types WHERE id = ANY($1)"# ,
205+ )
206+ . bind ( keys)
207+ . fetch_all ( & self . pool )
208+ . await ?;
209+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
210+ }
211+ }
212+
213+ pub struct CockpitTypeLoader {
214+ pub pool : sqlx:: PgPool ,
215+ }
216+
217+ impl Loader < i32 > for CockpitTypeLoader {
218+ type Value = DbCockpitType ;
219+ type Error = async_graphql:: Error ;
220+
221+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbCockpitType > , async_graphql:: Error > {
222+ let rows = sqlx:: query_as :: < _ , DbCockpitType > (
223+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
224+ rules_level::text AS rules_level,
225+ weight, crits, intro_year
226+ FROM cockpit_types WHERE id = ANY($1)"# ,
227+ )
228+ . bind ( keys)
229+ . fetch_all ( & self . pool )
230+ . await ?;
231+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
232+ }
233+ }
234+
235+ pub struct MyomerTypeLoader {
236+ pub pool : sqlx:: PgPool ,
237+ }
238+
239+ impl Loader < i32 > for MyomerTypeLoader {
240+ type Value = DbMyomerType ;
241+ type Error = async_graphql:: Error ;
242+
243+ async fn load ( & self , keys : & [ i32 ] ) -> Result < HashMap < i32 , DbMyomerType > , async_graphql:: Error > {
244+ let rows = sqlx:: query_as :: < _ , DbMyomerType > (
245+ r#"SELECT id, slug, name, tech_base::text AS tech_base,
246+ rules_level::text AS rules_level,
247+ intro_year, properties
248+ FROM myomer_types WHERE id = ANY($1)"# ,
249+ )
250+ . bind ( keys)
251+ . fetch_all ( & self . pool )
252+ . await ?;
253+ Ok ( rows. into_iter ( ) . map ( |r| ( r. id , r) ) . collect ( ) )
254+ }
255+ }
0 commit comments