22using Microsoft . AspNetCore . Mvc ;
33using Microsoft . Azure . Functions . Worker ;
44using Microsoft . Extensions . Logging ;
5+ using Survey . Models ;
6+ using Survey . Services ;
7+ using System . Text . Json ;
58
69namespace Survey ;
710
8- public class Function1
11+ public class SurveyFunctions
912{
10- private readonly ILogger < Function1 > _logger ;
13+ private readonly ILogger < SurveyFunctions > _logger ;
14+ private readonly ICosmosDbService _cosmosDbService ;
1115
12- public Function1 ( ILogger < Function1 > logger )
16+ public SurveyFunctions ( ILogger < SurveyFunctions > logger , ICosmosDbService cosmosDbService )
1317 {
1418 _logger = logger ;
19+ _cosmosDbService = cosmosDbService ;
1520 }
1621
17- [ Function ( "Function1" ) ]
18- public IActionResult Run ( [ HttpTrigger ( AuthorizationLevel . Function , "get" , "post" ) ] HttpRequest req )
22+ [ Function ( "CreateSurvey" ) ]
23+ public async Task < IActionResult > CreateSurvey (
24+ [ HttpTrigger ( AuthorizationLevel . Function , "post" , Route = "surveys" ) ] HttpRequest req )
1925 {
20- _logger . LogInformation ( "C# HTTP trigger function processed a request." ) ;
21- return new OkObjectResult ( "Welcome to Azure Functions!" ) ;
26+ _logger . LogInformation ( "アンケート登録APIが呼ばれました" ) ;
27+
28+ try
29+ {
30+ // リクエストボディを読み取り
31+ var requestBody = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ;
32+
33+ if ( string . IsNullOrWhiteSpace ( requestBody ) )
34+ {
35+ _logger . LogWarning ( "リクエストボディが空です" ) ;
36+ return new BadRequestObjectResult (
37+ ApiResponse < object > . ErrorResponse ( "リクエストボディが必要です" , "EMPTY_BODY" ) ) ;
38+ }
39+
40+ // JSONデシリアライズ
41+ var survey = JsonSerializer . Deserialize < Models . Survey > ( requestBody , new JsonSerializerOptions
42+ {
43+ PropertyNameCaseInsensitive = true
44+ } ) ;
45+
46+ if ( survey == null )
47+ {
48+ _logger . LogWarning ( "JSONの解析に失敗しました" ) ;
49+ return new BadRequestObjectResult (
50+ ApiResponse < object > . ErrorResponse ( "無効なJSONフォーマットです" , "INVALID_JSON" ) ) ;
51+ }
52+
53+ // バリデーション
54+ if ( ! survey . IsValid ( out var errors ) )
55+ {
56+ _logger . LogWarning ( "バリデーションエラー: {Errors}" , string . Join ( ", " , errors ) ) ;
57+ return new BadRequestObjectResult (
58+ ApiResponse < object > . ErrorResponse ( string . Join ( ", " , errors ) , "VALIDATION_ERROR" ) ) ;
59+ }
60+
61+ // 新しいIDと日時を設定
62+ survey . Id = Guid . NewGuid ( ) . ToString ( ) ;
63+ survey . EventDate = "2025-07-09" ; // イベント日付
64+ survey . CreatedAt = DateTime . UtcNow ;
65+ survey . UpdatedAt = DateTime . UtcNow ;
66+
67+ // Cosmos DBに保存
68+ var surveyId = await _cosmosDbService . CreateSurveyAsync ( survey ) ;
69+
70+ _logger . LogInformation ( "アンケートが正常に登録されました: {SurveyId}" , surveyId ) ;
71+
72+ var response = ApiResponse < object > . SuccessResponse (
73+ "アンケートの登録が完了しました" ,
74+ null ,
75+ surveyId ) ;
76+
77+ return new OkObjectResult ( response ) ;
78+ }
79+ catch ( JsonException ex )
80+ {
81+ _logger . LogError ( ex , "JSONの解析中にエラーが発生しました" ) ;
82+ return new BadRequestObjectResult (
83+ ApiResponse < object > . ErrorResponse ( "無効なJSONフォーマットです" , "JSON_PARSE_ERROR" ) ) ;
84+ }
85+ catch ( InvalidOperationException ex )
86+ {
87+ _logger . LogError ( ex , "無効な操作が実行されました" ) ;
88+ return new UnprocessableEntityObjectResult (
89+ ApiResponse < object > . ErrorResponse ( ex . Message , "INVALID_OPERATION" ) ) ;
90+ }
91+ catch ( Exception ex )
92+ {
93+ _logger . LogError ( ex , "アンケート登録中に予期しないエラーが発生しました" ) ;
94+ return new ObjectResult (
95+ ApiResponse < object > . ErrorResponse ( "内部サーバーエラーが発生しました" , "INTERNAL_ERROR" ) )
96+ {
97+ StatusCode = 500
98+ } ;
99+ }
100+ }
101+
102+ [ Function ( "GetSurveyResults" ) ]
103+ public async Task < IActionResult > GetSurveyResults (
104+ [ HttpTrigger ( AuthorizationLevel . Function , "get" , Route = "surveys/results" ) ] HttpRequest req )
105+ {
106+ _logger . LogInformation ( "集計結果取得APIが呼ばれました" ) ;
107+
108+ try
109+ {
110+ var results = await _cosmosDbService . GetSurveyResultsAsync ( ) ;
111+
112+ _logger . LogInformation ( "集計結果を正常に取得しました" ) ;
113+
114+ var response = ApiResponse < SurveyData > . SuccessResponse (
115+ "集計結果を取得しました" ,
116+ results ) ;
117+
118+ return new OkObjectResult ( response ) ;
119+ }
120+ catch ( Exception ex )
121+ {
122+ _logger . LogError ( ex , "集計結果取得中に予期しないエラーが発生しました" ) ;
123+ return new ObjectResult (
124+ ApiResponse < object > . ErrorResponse ( "内部サーバーエラーが発生しました" , "INTERNAL_ERROR" ) )
125+ {
126+ StatusCode = 500
127+ } ;
128+ }
22129 }
23130}
0 commit comments