11package org .inhahackers .optmo_user_be .function ;
22
33import com .microsoft .azure .functions .*;
4- import com .microsoft .azure .functions .annotation .AuthorizationLevel ;
5- import com .microsoft .azure .functions .annotation .FunctionName ;
6- import com .microsoft .azure .functions .annotation .HttpTrigger ;
7- import lombok .NoArgsConstructor ;
84import lombok .RequiredArgsConstructor ;
95import org .inhahackers .optmo_user_be .dto .ElecRequest ;
106import org .inhahackers .optmo_user_be .exception .JwtAuthenticationException ;
117import org .inhahackers .optmo_user_be .exception .UserNotFoundException ;
128import org .inhahackers .optmo_user_be .service .JwtTokenService ;
139import org .inhahackers .optmo_user_be .service .UserService ;
10+ import org .springframework .stereotype .Component ;
1411
1512import java .util .Optional ;
13+ import java .util .function .Function ;
1614
15+ @ Component ("increaseElecFunction" )
1716@ RequiredArgsConstructor
18- public class IncreaseElecFunction {
17+ public class IncreaseElecFunction implements Function < HttpRequestMessage < Optional < String >>, HttpResponseMessage > {
1918
2019 private final JwtTokenService jwtTokenService ;
2120 private final UserService userService ;
2221
23- @ FunctionName ("increaseElecFunction" )
24- public HttpResponseMessage run (
25- @ HttpTrigger (
26- name = "increaseelec" ,
27- methods = {HttpMethod .POST },
28- authLevel = AuthorizationLevel .ANONYMOUS )
29- HttpRequestMessage <Void > request ,
30- final ExecutionContext context ) {
31-
32- context .getLogger ().info ("increaseElecFunction called" );
33-
22+ @ Override
23+ public HttpResponseMessage apply (HttpRequestMessage <Optional <String >> request ) {
3424 try {
3525 // 1. Authorization 헤더 추출
3626 String authHeader = request .getHeaders ().get ("Authorization" );
@@ -41,10 +31,30 @@ public HttpResponseMessage run(
4131 }
4232 String accessToken = authHeader .substring ("Bearer " .length ());
4333
44- // 2. Request Body 파싱
34+ // 2. 쿼리 파라미터에서 long 값 안전하게 파싱
35+ String useElecStr = request .getQueryParameters ().get ("useElecEstimate" );
36+ String llmElecStr = request .getQueryParameters ().get ("llmElecEstimate" );
37+
38+ if (useElecStr == null || llmElecStr == null ) {
39+ return request .createResponseBuilder (HttpStatus .BAD_REQUEST )
40+ .body ("Missing required query parameters: useElecEstimate and llmElecEstimate" )
41+ .build ();
42+ }
43+
44+ long useElecEstimate ;
45+ long llmElecEstimate ;
46+ try {
47+ useElecEstimate = Long .parseLong (useElecStr );
48+ llmElecEstimate = Long .parseLong (llmElecStr );
49+ } catch (NumberFormatException e ) {
50+ return request .createResponseBuilder (HttpStatus .BAD_REQUEST )
51+ .body ("Invalid number format in query parameters" )
52+ .build ();
53+ }
54+
4555 ElecRequest elecRequest = ElecRequest .builder ()
46- .useElecEstimate (Long . getLong ( request . getQueryParameters (). get ( " useElecEstimate" )) )
47- .llmElecEstimate (Long . getLong ( request . getQueryParameters (). get ( " llmElecEstimate" )) )
56+ .useElecEstimate (useElecEstimate )
57+ .llmElecEstimate (llmElecEstimate )
4858 .build ();
4959
5060 // 3. 토큰에서 userId 추출 및 처리
@@ -55,7 +65,7 @@ public HttpResponseMessage run(
5565 // 4. 성공 응답
5666 return request .createResponseBuilder (HttpStatus .OK )
5767 .header ("Content-Type" , "text/plain" )
58- .body ("Successfully Increase Elec and Cost Estimate" )
68+ .body ("Successfully Increased Elec and Cost Estimate" )
5969 .build ();
6070
6171 } catch (JwtAuthenticationException e ) {
@@ -68,11 +78,6 @@ public HttpResponseMessage run(
6878 .body ("User Not Found: " + e .getMessage ())
6979 .build ();
7080
71- } catch (IllegalArgumentException e ) {
72- return request .createResponseBuilder (HttpStatus .BAD_REQUEST )
73- .body (e .getMessage ())
74- .build ();
75-
7681 } catch (Exception e ) {
7782 return request .createResponseBuilder (HttpStatus .INTERNAL_SERVER_ERROR )
7883 .body ("Internal error: " + e .getMessage ())
0 commit comments