Add solution for gin challenge-1-basic-routing by WHFF521 - #1925
Conversation
WalkthroughAdds a Gin-based in-memory user REST API on port 8000. The API supports user listing, retrieval, creation, replacement, and deletion. It also defines case-insensitive name search logic, but does not register a search route. ChangesUser API
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 790c5ea2-ffa4-4966-9745-debe2d2319aa
📒 Files selected for processing (1)
packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go
| // In-memory storage | ||
| 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}, | ||
| } | ||
| var nextID = 4 |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
ast-grep outline packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go --items all --type function
rg -n -C 4 '\b(users|nextID)\b|func (getAllUsers|getUserByID|createUser|updateUser|deleteUser|searchUsers|findUserByID)' \
packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.goRepository: RezaSi/go-interview-practice
Length of output: 3989
🏁 Script executed:
#!/bin/bash
set -euo pipefail
cat -n packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go | sed -n '1,270p'
rg -n 'sync\.' packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go || trueRepository: RezaSi/go-interview-practice
Length of output: 7982
Synchronize users and nextID with sync.RWMutex.
Gin handles requests concurrently, and getAllUsers, getUserByID, createUser, updateUser, deleteUser, searchUsers, and findUserByID all access shared users and nextID without locking. Hold RLock for reads and Lock for write operations to prevent lost updates, duplicate IDs, and read races.
| // GET /users/search - Search users by name | ||
|
|
||
| r.Run(":8000") |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
ast-grep outline packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go --items all --type function
rg -n -C 4 'GET\("/users/search"|func searchUsers|GET\("/users/:id"' \
packages/gin/challenge-1-basic-routing/submissions/WHFF521/solution.go \
packages/gin/challenge-1-basic-routing/solution-template_test.goRepository: RezaSi/go-interview-practice
Length of output: 3612
Register the search route.
searchUsers handles GET /users/search?name=value, but the router only registers GET /users/:id. That request reaches getUserByID, fails integer parsing for search, and returns HTTP 400 instead of executing the handler.
Proposed fix
// GET /users/search - Search users by name
+ r.GET("/users/search", searchUsers)
r.Run(":8000")📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // GET /users/search - Search users by name | |
| r.Run(":8000") | |
| // GET /users/search - Search users by name | |
| r.GET("/users/search", searchUsers) | |
| r.Run(":8000") |
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, Response{ | ||
| Success: false, | ||
| Error: "", | ||
| Code: http.StatusNotFound, | ||
| }) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Set Response.Code to the transmitted status.
This branch sends HTTP 400 but serializes code: 404. Clients that consume the response body receive contradictory error status data.
c.JSON(http.StatusBadRequest, Response{
Success: false,
Error: "",
- Code: http.StatusNotFound,
+ Code: http.StatusBadRequest,
})📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if err != nil { | |
| c.JSON(http.StatusBadRequest, Response{ | |
| Success: false, | |
| Error: "", | |
| Code: http.StatusNotFound, | |
| }) | |
| if err != nil { | |
| c.JSON(http.StatusBadRequest, Response{ | |
| Success: false, | |
| Error: "", | |
| Code: http.StatusBadRequest, | |
| }) |
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @WHFF521! 📊 Scoreboards and badges will be updated shortly. |
gin challenge-1-basic-routing Solution
Submitted by: @WHFF521
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/WHFF521/solution.goTesting
Thank you for reviewing my submission! 🚀