Skip to content

Commit 06ff074

Browse files
committed
Implemented Complex model, ComplexInput type, CreateComplexHandler & enable complex post routing
1 parent 50f4ddd commit 06ff074

1 file changed

Lines changed: 62 additions & 24 deletions

File tree

backend/main.go

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ import (
1010
"github.com/gin-gonic/gin"
1111
"github.com/go-playground/validator/v10"
1212
"github.com/golang-migrate/migrate/v4"
13-
_ "github.com/golang-migrate/migrate/v4/database/mysql" // MySQL driver for migrate
14-
_ "github.com/golang-migrate/migrate/v4/source/file" // File source for migrate
13+
_ "github.com/golang-migrate/migrate/v4/database/mysql"
14+
_ "github.com/golang-migrate/migrate/v4/source/file"
1515
"gorm.io/driver/mysql"
1616
"gorm.io/gorm"
1717
"gorm.io/gorm/logger"
1818
)
1919

20-
// Global Variables
20+
/* Global Variables */
2121
var (
2222
db *gorm.DB
2323
validate *validator.Validate
2424
)
2525

26-
// Models (仮定義: 実際には models/ ディレクトリなどに分離推奨)
27-
// OpenAPIのスキーマに基づいてGORMモデルを定義します。
28-
// 例:
29-
// type Complex struct {
30-
// ID uint `gorm:"primarykey" json:"id"`
31-
// UserID string `json:"user_id" gorm:"type:varchar(36);not null;index"` // UUID
32-
// Content string `json:"content" gorm:"not null" validate:"required"`
33-
// Category string `json:"category" gorm:"not null" validate:"required"`
34-
// CreatedAt time.Time `json:"created_at"`
35-
// UpdatedAt time.Time `json:"updated_at"`
36-
// }
26+
/* --- Models (実際には models/ ディレクトリなどに分離推奨) --- */
27+
28+
// Complex represents the complex entity.
29+
type Complex struct {
30+
ID uint `gorm:"primarykey" json:"id"`
31+
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index"` // UUID from AuthMiddleware
32+
Content string `json:"content" gorm:"not null" validate:"required"`
33+
Category string `json:"category" gorm:"not null" validate:"required"`
34+
CreatedAt time.Time `json:"created_at"`
35+
UpdatedAt time.Time `json:"updated_at"`
36+
}
37+
38+
// ComplexInput defines the expected input for creating a new complex.
39+
type ComplexInput struct {
40+
Content string `json:"content" validate:"required,min=1,max=255"`
41+
Category string `json:"category" validate:"required,min=1,max=100"`
42+
}
43+
3744
// type Goal struct { ... }
3845
// type Action struct { ... }
3946

@@ -70,8 +77,39 @@ func PingHandler(c *gin.Context) {
7077
c.JSON(http.StatusOK, gin.H{"message": "pong"})
7178
}
7279

73-
// Complex Handlers (スタブ)
74-
// func CreateComplexHandler(c *gin.Context) { /* ... */ }
80+
// CreateComplexHandler handles the creation of a new complex.
81+
func CreateComplexHandler(c *gin.Context) {
82+
userID, exists := c.Get("userID")
83+
if !exists {
84+
c.JSON(http.StatusUnauthorized, NewErrorResponse(http.StatusUnauthorized, "User ID not found in context"))
85+
return
86+
}
87+
88+
var input ComplexInput
89+
if err := c.ShouldBindJSON(&input); err != nil {
90+
c.JSON(http.StatusBadRequest, NewErrorResponse(http.StatusBadRequest, "Invalid request body: "+err.Error()))
91+
return
92+
}
93+
94+
if err := validate.Struct(input); err != nil {
95+
c.JSON(http.StatusBadRequest, NewErrorResponse(http.StatusBadRequest, "Validation failed: "+err.Error()))
96+
return
97+
}
98+
99+
complex := Complex{
100+
UserID: userID.(string),
101+
Content: input.Content,
102+
Category: input.Category,
103+
}
104+
105+
if result := db.Create(&complex); result.Error != nil {
106+
c.JSON(http.StatusInternalServerError, NewErrorResponse(http.StatusInternalServerError, "Failed to create complex: "+result.Error.Error()))
107+
return
108+
}
109+
110+
c.JSON(http.StatusCreated, complex)
111+
}
112+
75113
// func GetComplexesHandler(c *gin.Context) { /* ... */ }
76114
// func GetComplexHandler(c *gin.Context) { /* ... */ }
77115
// func UpdateComplexHandler(c *gin.Context) { /* ... */ }
@@ -186,14 +224,14 @@ func main() {
186224
apiV1.GET("/ping", PingHandler)
187225

188226
// Complexes
189-
// complexesGroup := apiV1.Group("/complexes")
190-
// {
191-
// // complexesGroup.POST("", CreateComplexHandler)
192-
// // complexesGroup.GET("", GetComplexesHandler)
193-
// // complexesGroup.GET("/:complexId", GetComplexHandler)
194-
// // complexesGroup.PUT("/:complexId", UpdateComplexHandler)
195-
// // complexesGroup.DELETE("/:complexId", DeleteComplexHandler)
196-
// }
227+
complexesGroup := apiV1.Group("/complexes")
228+
{
229+
complexesGroup.POST("", CreateComplexHandler)
230+
// complexesGroup.GET("", GetComplexesHandler)
231+
// complexesGroup.GET("/:complexId", GetComplexHandler)
232+
// complexesGroup.PUT("/:complexId", UpdateComplexHandler)
233+
// complexesGroup.DELETE("/:complexId", DeleteComplexHandler)
234+
}
197235

198236
// Goals
199237
// goalsGroup := apiV1.Group("/goals")

0 commit comments

Comments
 (0)