Skip to content

Add solution for gin challenge-1-basic-routing by KuoZ#1559

Open
CV-Elevation wants to merge 2 commits intoRezaSi:mainfrom
CV-Elevation:package-gin-challenge-1-basic-routing-CV-Elevation
Open

Add solution for gin challenge-1-basic-routing by KuoZ#1559
CV-Elevation wants to merge 2 commits intoRezaSi:mainfrom
CV-Elevation:package-gin-challenge-1-basic-routing-CV-Elevation

Conversation

@CV-Elevation
Copy link
Copy Markdown

@CV-Elevation CV-Elevation commented Apr 7, 2026

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

  • Added solution file to packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

Walkthrough

A new solution.go file implements a complete Gin-based REST API for user management with in-memory storage. The implementation includes data models, six REST endpoints for CRUD operations and search functionality, request validation, and HTTP status code handling.

Changes

Cohort / File(s) Summary
Gin CRUD User API Implementation
packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go
New file with exported User and Response types, global in-memory storage, and a main() function registering REST endpoints (GET, POST, PUT, DELETE /users and variants). Implements six handler functions for CRUD operations and name-based search with request validation, ID parsing, and appropriate HTTP status codes (200, 201, 400, 404). Includes helper functions for user lookup and validation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 A router springs to life, with users stored in slice,
GET, POST, PUT, DELETE—each endpoint oh so nice!
Search by name, validate with care,
In-memory users dancing everywhere! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a solution for the gin challenge-1-basic-routing challenge submitted by CV-Elevation (KuoZ).
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the solution submission, file location, and verification steps.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (4)
packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go (4)

28-35: Inconsistent initial state with nextID.

The initial data includes 4 users (IDs 1-4), but nextID is 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 via setupRouter(), 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 of http.ErrBodyNotAllowed for validation errors.

http.ErrBodyNotAllowed is 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. Use errors.New() or fmt.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

📥 Commits

Reviewing files that changed from the base of the PR and between 6a002af and 260dfd4.

📒 Files selected for processing (1)
  • packages/gin/challenge-1-basic-routing/submissions/CV-Elevation/solution.go

Copy link
Copy Markdown
Author

@CV-Elevation CV-Elevation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finished

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant