@@ -165,34 +165,33 @@ public ResponseEntity transferCredits(
165165 @ RequestParam (name ="target" , required =true ) String targetProfileId ,
166166 @ RequestParam (name ="amount" , required =true ) Integer amount
167167 ) {
168-
169- Profile sourceProfile = profileCol .get (sourceProfileId ).contentAs (Profile .class ),
170- targetProfile = profileCol .get (targetProfileId ).contentAs (Profile .class );
171-
172- if (sourceProfile == null ) {
173- return ResponseEntity .status (500 ).body ("Source profile not found" );
174- }
175- if (targetProfile == null ) {
176- return ResponseEntity .status (500 ).body ("Target profile not found" );
177- }
178-
179- TransactionOptions to = TransactionOptions .transactionOptions ();
180- TransactionQueryOptions args = TransactionQueryOptions .queryOptions ().parameters (
181- JsonObject .create ()
182- .put ("source" , sourceProfileId )
183- .put ("amount" , amount )
184- .put ("target" , targetProfileId )
168+ TransactionOptions to = TransactionOptions .transactionOptions ();
169+ TransactionQueryOptions args = TransactionQueryOptions .queryOptions ().parameters (
170+ JsonObject .create ()
171+ .put ("source" , sourceProfileId )
172+ .put ("amount" , amount )
173+ .put ("target" , targetProfileId )
185174 );
186175
187176 while (true ) {
188177 try {
189178 cluster .transactions ().run (ctx -> {
179+ Profile sourceProfile = profileCol .get (sourceProfileId ).contentAs (Profile .class ),
180+ targetProfile = profileCol .get (targetProfileId ).contentAs (Profile .class );
181+
182+ if (sourceProfile == null ) {
183+ throw new RuntimeException ("Source profile not found" );
184+ }
185+ if (targetProfile == null ) {
186+ throw new RuntimeException ("Target profile not found" );
187+ }
188+
190189 ctx .query ("UPDATE `" +dbProperties .getBucketName ()+"`.`_default`.`" +PROFILE +"` SET balance = balance - $amount WHERE pid = $source" , args );
191190 ctx .query ("UPDATE `" +dbProperties .getBucketName ()+"`.`_default`.`" +PROFILE +"` SET balance = balance + $amount WHERE pid = $target" , args );
191+
192192 if (sourceProfile .getBalance () < amount ) {
193193 throw new RuntimeException ("Insufficient balance" );
194194 }
195-
196195 }, to );
197196
198197 break ;
@@ -206,8 +205,8 @@ public ResponseEntity transferCredits(
206205 }
207206 }
208207
209-
210-
208+ Profile sourceProfile = profileCol . get ( sourceProfileId ). contentAs ( Profile . class ),
209+ targetProfile = profileCol . get ( targetProfileId ). contentAs ( Profile . class );
211210 return ResponseEntity .ok (Arrays .asList (sourceProfile , targetProfile ));
212211 }
213212
0 commit comments