@@ -444,6 +444,43 @@ func CreateActionHandler(c *gin.Context) {
444444 c .JSON (http .StatusCreated , action )
445445}
446446
447+ // GetActionsHandler handles fetching all actions for a specific goal of the authenticated user.
448+ func GetActionsHandler (c * gin.Context ) {
449+ userID , exists := c .Get ("userID" )
450+ if ! exists {
451+ c .JSON (http .StatusUnauthorized , NewErrorResponse (http .StatusUnauthorized , "User ID not found in context" ))
452+ return
453+ }
454+
455+ goalIDStr := c .Query ("goal_id" )
456+ if goalIDStr == "" {
457+ c .JSON (http .StatusBadRequest , NewErrorResponse (http .StatusBadRequest , "Query parameter 'goal_id' is required" ))
458+ return
459+ }
460+
461+ // Ensure the goal belongs to the user to prevent fetching actions for other users' goals
462+ var goal Goal
463+ if err := db .Where ("id = ? AND user_id = ?" , goalIDStr , userID .(string )).First (& goal ).Error ; err != nil {
464+ if err == gorm .ErrRecordNotFound {
465+ c .JSON (http .StatusNotFound , NewErrorResponse (http .StatusNotFound , "Goal not found or does not belong to user" ))
466+ return
467+ }
468+ c .JSON (http .StatusInternalServerError , NewErrorResponse (http .StatusInternalServerError , "Error verifying goal: " + err .Error ()))
469+ return
470+ }
471+
472+ var actions []Action
473+ if result := db .Where ("goal_id = ? AND user_id = ?" , goalIDStr , userID .(string )).Order ("created_at DESC" ).Find (& actions ); result .Error != nil {
474+ c .JSON (http .StatusInternalServerError , NewErrorResponse (http .StatusInternalServerError , "Failed to fetch actions: " + result .Error .Error ()))
475+ return
476+ }
477+
478+ if actions == nil {
479+ actions = []Action {}
480+ }
481+ c .JSON (http .StatusOK , actions )
482+ }
483+
447484func main () {
448485 // --- 環境変数からの設定読み込み (例) ---
449486 dbUser := os .Getenv ("DB_USER" )
@@ -566,6 +603,7 @@ func main() {
566603 actionsGroup := apiV1 .Group ("/actions" )
567604 {
568605 actionsGroup .POST ("" , CreateActionHandler )
606+ actionsGroup .GET ("" , GetActionsHandler )
569607 }
570608 }
571609
0 commit comments