Skip to content

Commit 24155ea

Browse files
committed
Added GetComplexHandler, UpdateComplexHandler, DeleteComplexHandler & related routing
1 parent 9823284 commit 24155ea

1 file changed

Lines changed: 83 additions & 6 deletions

File tree

backend/main.go

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,86 @@ func GetComplexesHandler(c *gin.Context) {
131131
c.JSON(http.StatusOK, complexes)
132132
}
133133

134-
// func GetComplexHandler(c *gin.Context) { /* ... */ }
135-
// func UpdateComplexHandler(c *gin.Context) { /* ... */ }
136-
// func DeleteComplexHandler(c *gin.Context) { /* ... */ }
134+
// GetComplexHandler handles fetching a specific complex by its ID.
135+
func GetComplexHandler(c *gin.Context) {
136+
userID, exists := c.Get("userID")
137+
if !exists {
138+
c.JSON(http.StatusUnauthorized, NewErrorResponse(http.StatusUnauthorized, "User ID not found in context"))
139+
return
140+
}
141+
142+
complexID := c.Param("complexId")
143+
144+
var complex Complex
145+
if result := db.Where("id = ? AND user_id = ?", complexID, userID.(string)).First(&complex); result.Error != nil {
146+
if result.Error == gorm.ErrRecordNotFound {
147+
c.JSON(http.StatusNotFound, NewErrorResponse(http.StatusNotFound, "Complex not found"))
148+
return
149+
}
150+
c.JSON(http.StatusInternalServerError, NewErrorResponse(http.StatusInternalServerError, "Failed to fetch complex: "+result.Error.Error()))
151+
return
152+
}
153+
154+
c.JSON(http.StatusOK, complex)
155+
}
156+
157+
// UpdateComplexHandler handles updating an existing complex.
158+
func UpdateComplexHandler(c *gin.Context) {
159+
userID, exists := c.Get("userID")
160+
if !exists {
161+
c.JSON(http.StatusUnauthorized, NewErrorResponse(http.StatusUnauthorized, "User ID not found in context"))
162+
return
163+
}
164+
165+
complexID := c.Param("complexId")
166+
167+
var input ComplexInput
168+
if err := c.ShouldBindJSON(&input); err != nil {
169+
c.JSON(http.StatusBadRequest, NewErrorResponse(http.StatusBadRequest, "Invalid request body: "+err.Error()))
170+
return
171+
}
172+
173+
if err := validate.Struct(input); err != nil {
174+
c.JSON(http.StatusBadRequest, NewErrorResponse(http.StatusBadRequest, "Validation failed: "+err.Error()))
175+
return
176+
}
177+
178+
var complex Complex
179+
if result := db.Where("id = ? AND user_id = ?", complexID, userID.(string)).First(&complex); result.Error != nil {
180+
if result.Error == gorm.ErrRecordNotFound {
181+
c.JSON(http.StatusNotFound, NewErrorResponse(http.StatusNotFound, "Complex not found to update"))
182+
return
183+
}
184+
c.JSON(http.StatusInternalServerError, NewErrorResponse(http.StatusInternalServerError, "Failed to find complex to update: "+result.Error.Error()))
185+
return
186+
}
187+
188+
complex.Content = input.Content
189+
complex.Category = input.Category
190+
191+
if result := db.Save(&complex); result.Error != nil {
192+
c.JSON(http.StatusInternalServerError, NewErrorResponse(http.StatusInternalServerError, "Failed to update complex: "+result.Error.Error()))
193+
return
194+
}
195+
196+
c.JSON(http.StatusOK, complex)
197+
}
198+
199+
// DeleteComplexHandler handles deleting a complex by its ID.
200+
func DeleteComplexHandler(c *gin.Context) {
201+
userID, exists := c.Get("userID")
202+
if !exists {
203+
c.JSON(http.StatusUnauthorized, NewErrorResponse(http.StatusUnauthorized, "User ID not found in context"))
204+
return
205+
}
206+
complexID := c.Param("complexId")
207+
208+
if result := db.Where("id = ? AND user_id = ?", complexID, userID.(string)).Delete(&Complex{}); result.Error != nil || result.RowsAffected == 0 {
209+
c.JSON(http.StatusNotFound, NewErrorResponse(http.StatusNotFound, "Complex not found or already deleted"))
210+
return
211+
}
212+
c.Status(http.StatusNoContent)
213+
}
137214

138215
// Goal Handlers (スタブ)
139216
// func CreateGoalHandler(c *gin.Context) { /* ... */ }
@@ -248,9 +325,9 @@ func main() {
248325
{
249326
complexesGroup.POST("", CreateComplexHandler)
250327
complexesGroup.GET("", GetComplexesHandler)
251-
// complexesGroup.GET("/:complexId", GetComplexHandler)
252-
// complexesGroup.PUT("/:complexId", UpdateComplexHandler)
253-
// complexesGroup.DELETE("/:complexId", DeleteComplexHandler)
328+
complexesGroup.GET("/:complexId", GetComplexHandler)
329+
complexesGroup.PUT("/:complexId", UpdateComplexHandler)
330+
complexesGroup.DELETE("/:complexId", DeleteComplexHandler)
254331
}
255332

256333
// Goals

0 commit comments

Comments
 (0)