Add solution for gin challenge-1-basic-routing by KuoZ#1559
Add solution for gin challenge-1-basic-routing by KuoZ#1559CV-Elevation wants to merge 2 commits intoRezaSi:mainfrom
Conversation
WalkthroughA new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (4)
packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go (4)
28-35: Inconsistent initial state withnextID.The initial data includes 4 users (IDs 1-4), but
nextIDis set to 4 instead of 5. If a new user were created before any test reset, it would receive a duplicate ID. The test suite resets this state viasetupRouter(), so this won't cause test failures, but it's worth correcting for consistency.var users = []User{ {ID: 1, Name: "John Doe", Email: "john@example.com", Age: 30}, {ID: 2, Name: "Jane Smith", Email: "jane@example.com", Age: 25}, {ID: 3, Name: "Bob Wilson", Email: "bob@example.com", Age: 35}, - {ID: 4, Name: "kuoz", Email: "kuoz@qq.com", Age: 21}, } -var nextID = 4 +var nextID = 4
77-77: Inconsistent language in error message.The error message uses Chinese ("无效的id") while all other error messages in the file are in English. Consider using a consistent language for all user-facing messages.
if err != nil { c.JSON(http.StatusBadRequest, Response{ Success: false, - Message: "无效的id", + Message: "Invalid ID", }) return }
188-197: Swap-with-last deletion doesn't preserve order.The swap-with-last removal pattern (
users[index] = users[len(users)-1]) is O(1) but changes the order of remaining elements. This is acceptable for this challenge since no ordering guarantees are required, but worth noting if order preservation becomes important later.
252-263: Misuse ofhttp.ErrBodyNotAllowedfor validation errors.
http.ErrBodyNotAllowedis an HTTP protocol error indicating that a request method doesn't permit a body. Using it for field validation is semantically incorrect and may confuse debugging. Useerrors.New()orfmt.Errorf()to return meaningful validation messages.+import ( + "errors" + // ... other imports +) func validateUser(user User) error { if strings.TrimSpace(user.Name) == "" { - return &gin.Error{Err: http.ErrBodyNotAllowed, Type: gin.ErrorTypeBind} + return errors.New("name is required") } if strings.TrimSpace(user.Email) == "" { - return &gin.Error{Err: http.ErrBodyNotAllowed, Type: gin.ErrorTypeBind} + return errors.New("email is required") } if user.Age <= 0 { - return &gin.Error{Err: http.ErrBodyNotAllowed, Type: gin.ErrorTypeBind} + return errors.New("age must be positive") } if !strings.Contains(user.Email, "@") { - return &gin.Error{Err: http.ErrBodyNotAllowed, Type: gin.ErrorTypeBind} + return errors.New("invalid email format") } return nil }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1f114109-0da4-4f69-bbb2-6ee52e5d7812
📒 Files selected for processing (1)
packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go
gin challenge-1-basic-routing Solution
Submitted by: @CV-Elevation
Package: gin
Challenge: challenge-1-basic-routing
Description
This PR contains my solution for gin challenge-1-basic-routing.
Changes
packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.goTesting
Thank you for reviewing my submission! 🚀