@@ -74,20 +74,20 @@ func (h *handler) ListBundles(
7474) (* ListBundlesOutput , error ) {
7575 base := h .apiBase (ctx )
7676
77- idb := db .GetQueries (ctx ). DB
77+ queries := db .GetQueries (ctx )
7878
79- query := idb . NewSelect (). Model ((* db .Bundle )(nil ))
79+ q := queries . Select ((* db .Bundle )(nil ))
8080
8181 user := getAuthUser (ctx )
8282 if user != nil {
83- query = query .Where ("bundle.public = ? OR bundle.owner_id = ?" , true , user .ID )
83+ q = q .Where ("bundle.public = ? OR bundle.owner_id = ?" , true , user .ID )
8484 } else {
85- query = query .Where ("bundle.public = ?" , true )
85+ q = q .Where ("bundle.public = ?" , true )
8686 }
8787
88- query = applyBundleFilters (query , input )
88+ q = applyBundleFilters (q , input )
8989
90- total , err := query .Count (ctx )
90+ total , err := q .Count (ctx )
9191 if err != nil {
9292 log .Errorf ("count bundles: %v" , err )
9393 return nil , huma .Error500InternalServerError ("Internal error." )
@@ -103,7 +103,7 @@ func (h *handler) ListBundles(
103103 offset := (input .Page - 1 ) * perPage
104104
105105 var bundles []db.Bundle
106- if err := query .Model (& bundles ).
106+ if err := q .Model (& bundles ).
107107 Relation ("Owner" ).Relation ("Project" ).
108108 OrderExpr ("bundle.id ASC" ).Offset (offset ).Limit (perPage ).Scan (ctx ); err != nil {
109109 log .Errorf ("list bundles: %v" , err )
@@ -132,17 +132,17 @@ func (h *handler) GetBundle(
132132 ctx context.Context , input * GetBundleInput ,
133133) (* GetBundleOutput , error ) {
134134 base := h .apiBase (ctx )
135- idb := db .GetQueries (ctx ). DB
135+ q := db .GetQueries (ctx )
136136
137137 var bundle db.Bundle
138- if err := idb . NewSelect (). Model (& bundle ).
138+ if err := q . Select (& bundle ).
139139 Relation ("Owner" ).Relation ("Project" ).
140140 Where ("bundle.id = ?" , input .ID ).Scan (ctx ); err != nil {
141141 return nil , huma .Error404NotFound ("Not found." )
142142 }
143143
144144 bundles := []db.Bundle {bundle }
145- if err := db . GetQueries ( ctx ) .LoadBundlePatches (bundles ); err != nil {
145+ if err := q .LoadBundlePatches (bundles ); err != nil {
146146 log .Errorf ("load bundle patches: %v" , err )
147147 return nil , huma .Error500InternalServerError ("Internal error." )
148148 }
@@ -174,7 +174,7 @@ func (h *handler) CreateBundle(
174174
175175 q := db .GetQueries (ctx )
176176 patchIDs := * body .Patches
177- projectID , err := validateBundlePatches (ctx , q . DB , patchIDs )
177+ projectID , err := validateBundlePatches (ctx , q , patchIDs )
178178 if err != nil {
179179 return nil , huma .Error400BadRequest (err .Error ())
180180 }
@@ -194,12 +194,12 @@ func (h *handler) CreateBundle(
194194 return nil , huma .Error400BadRequest ("Bundle creation failed." )
195195 }
196196
197- if err := insertBundlePatches (ctx , q . DB , bundle .ID , patchIDs ); err != nil {
197+ if err := insertBundlePatches (ctx , q , bundle .ID , patchIDs ); err != nil {
198198 return nil , huma .Error400BadRequest ("Bundle creation failed." )
199199 }
200200
201201 // Re-fetch with relations.
202- if err := q .DB . NewSelect (). Model (& bundle ).
202+ if err := q .Select (& bundle ).
203203 Relation ("Owner" ).Relation ("Project" ).
204204 Where ("bundle.id = ?" , bundle .ID ).Scan (ctx ); err != nil {
205205 return nil , huma .Error400BadRequest ("Bundle creation failed." )
@@ -230,10 +230,9 @@ func (h *handler) UpdateBundle(
230230 return nil , err
231231 }
232232
233- idb := db .GetQueries (ctx ). DB
233+ q := db .GetQueries (ctx )
234234 var bundle db.Bundle
235- if err := idb .NewSelect ().Model (& bundle ).
236- Where ("id = ?" , input .ID ).Scan (ctx ); err != nil {
235+ if err := q .Select (& bundle ).Where ("id = ?" , input .ID ).Scan (ctx ); err != nil {
237236 return nil , huma .Error404NotFound ("Not found." )
238237 }
239238
@@ -247,21 +246,21 @@ func (h *handler) UpdateBundle(
247246 if len (* body .Patches ) == 0 {
248247 return nil , huma .Error400BadRequest ("Bundles cannot be empty." )
249248 }
250- projectID , err := validateBundlePatches (ctx , idb , * body .Patches )
249+ projectID , err := validateBundlePatches (ctx , q , * body .Patches )
251250 if err != nil {
252251 return nil , huma .Error400BadRequest (err .Error ())
253252 }
254- if _ , err := idb . NewDelete (). Model ((* db .BundlePatch )(nil )).
253+ if _ , err := q . Delete ((* db .BundlePatch )(nil )).
255254 Where ("bundle_id = ?" , input .ID ).Exec (ctx ); err != nil {
256255 return nil , huma .Error400BadRequest ("Update failed." )
257256 }
258- if err := insertBundlePatches (ctx , idb , input .ID , * body .Patches ); err != nil {
257+ if err := insertBundlePatches (ctx , q , input .ID , * body .Patches ); err != nil {
259258 return nil , huma .Error400BadRequest ("Update failed." )
260259 }
261260 bundle .ProjectID = projectID
262261 }
263262
264- up := idb . NewUpdate (). Model (& bundle ).Where ("id = ?" , input .ID )
263+ up := q . Update (& bundle ).Where ("id = ?" , input .ID )
265264 if body .Name != nil {
266265 up = up .Set ("name = ?" , * body .Name )
267266 }
@@ -275,15 +274,15 @@ func (h *handler) UpdateBundle(
275274 return nil , huma .Error400BadRequest ("Update failed." )
276275 }
277276
278- if err := idb . NewSelect (). Model (& bundle ).
277+ if err := q . Select (& bundle ).
279278 Relation ("Owner" ).Relation ("Project" ).
280279 Where ("bundle.id = ?" , input .ID ).Scan (ctx ); err != nil {
281280 log .Errorf ("get bundle: %v" , err )
282281 return nil , huma .Error500InternalServerError ("Internal error." )
283282 }
284283
285284 bundles := []db.Bundle {bundle }
286- if err := db . GetQueries ( ctx ) .LoadBundlePatches (bundles ); err != nil {
285+ if err := q .LoadBundlePatches (bundles ); err != nil {
287286 log .Errorf ("load bundle patches: %v" , err )
288287 return nil , huma .Error500InternalServerError ("Internal error." )
289288 }
@@ -308,24 +307,18 @@ func (h *handler) DeleteBundle(
308307 return nil , err
309308 }
310309
311- idb := db .GetQueries (ctx ). DB
310+ q := db .GetQueries (ctx )
312311
313312 var bundle db.Bundle
314- if err := idb .NewSelect ().Model (& bundle ).
315- Where ("id = ?" , input .ID ).Scan (ctx ); err != nil {
313+ if err := q .Select (& bundle ).Where ("id = ?" , input .ID ).Scan (ctx ); err != nil {
316314 return nil , huma .Error404NotFound ("Not found." )
317315 }
318316
319317 if bundle .OwnerID != user .ID {
320318 return nil , ForbiddenErr
321319 }
322320
323- if _ , err := idb .NewDelete ().Model ((* db .BundlePatch )(nil )).
324- Where ("bundle_id = ?" , input .ID ).Exec (ctx ); err != nil {
325- return nil , huma .Error400BadRequest ("Delete failed." )
326- }
327- if _ , err := idb .NewDelete ().Model ((* db .Bundle )(nil )).
328- Where ("id = ?" , input .ID ).Exec (ctx ); err != nil {
321+ if _ , err := q .Delete (& bundle ).WherePK ().Exec (ctx ); err != nil {
329322 return nil , huma .Error400BadRequest ("Delete failed." )
330323 }
331324
@@ -353,9 +346,9 @@ func applyBundleFilters(q *bun.SelectQuery, input *ListBundlesInput) *bun.Select
353346 return q
354347}
355348
356- func validateBundlePatches (ctx context.Context , idb bun. IDB , patchIDs []int ) (int , error ) {
349+ func validateBundlePatches (ctx context.Context , q * db. Queries , patchIDs []int ) (int , error ) {
357350 var projectIDs []int
358- idb . NewSelect (). Model ((* db .Patch )(nil )).
351+ q . Select ((* db .Patch )(nil )).
359352 Column ("project_id" ).
360353 Where ("id IN ?" , bun .Tuple (patchIDs )).
361354 Scan (ctx , & projectIDs )
@@ -374,14 +367,14 @@ func validateBundlePatches(ctx context.Context, idb bun.IDB, patchIDs []int) (in
374367 return projectID , nil
375368}
376369
377- func insertBundlePatches (ctx context.Context , idb bun. IDB , bundleID int , patchIDs []int ) error {
370+ func insertBundlePatches (ctx context.Context , q * db. Queries , bundleID int , patchIDs []int ) error {
378371 for i , pid := range patchIDs {
379372 bp := db.BundlePatch {
380373 BundleID : bundleID ,
381374 PatchID : pid ,
382375 Order : int (i ),
383376 }
384- if _ , err := idb . NewInsert (). Model ( & bp ). Exec ( ctx ); err != nil {
377+ if err := q . Insert ( & bp ); err != nil {
385378 return err
386379 }
387380 }
0 commit comments