Skip to content

Add solution for gin challenge-1-basic-routing by RuslanG003#1469

Merged
RezaSi merged 2 commits intoRezaSi:mainfrom
RuslanG003:package-gin-challenge-1-basic-routing-RuslanG003
Mar 16, 2026
Merged

Add solution for gin challenge-1-basic-routing by RuslanG003#1469
RezaSi merged 2 commits intoRezaSi:mainfrom
RuslanG003:package-gin-challenge-1-basic-routing-RuslanG003

Conversation

@RuslanG003
Copy link
Contributor

gin challenge-1-basic-routing Solution

Submitted by: @RuslanG003
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/RuslanG003/solution.go

Testing

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

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 1, 2026

Walkthrough

This PR introduces a complete Gin-based HTTP API implementation for a basic routing challenge. It defines User and Response types, establishes in-memory user storage, and implements CRUD operations with a search endpoint, complete with validation and error handling.

Changes

Cohort / File(s) Summary
Gin API Challenge Solution
packages/gin/challenge-1-basic-routing/submissions/RuslanG003/solution.go
Introduces Gin HTTP API scaffold with User and Response structs, in-memory user storage, and full CRUD handler implementations (getAllUsers, getUserByID, createUser, updateUser, deleteUser, searchUsers). Includes validation logic, helper functions for user lookup and field validation, case-insensitive search, and standard error responses with appropriate HTTP status codes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 A user API hops to life with gin,
CRUD handlers dancing, data tucked within,
Searches prance through names with care,
Validation guards each field with flair,
Five endpoints leap—create, read, update, gone! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding a Gin challenge solution submission by a specific contributor.
Description check ✅ Passed The description is directly related to the changeset, explaining the submission of a Gin challenge solution with appropriate context about testing and code quality.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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
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: 5

🧹 Nitpick comments (2)
packages/gin/challenge-1-basic-routing/submissions/RuslanG003/solution.go (2)

30-36: Race condition on global state.

The users slice and nextID counter are modified by multiple handlers (createUser, updateUser, deleteUser) without synchronization. Concurrent HTTP requests will cause data races.

For a challenge solution this may be acceptable, but in production code you'd need a sync.Mutex or sync.RWMutex to protect access.

🔒 Proposed fix using mutex
+import "sync"
+
 // In-memory storage
+var usersMu sync.RWMutex
 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

Then use usersMu.RLock()/usersMu.RUnlock() for reads and usersMu.Lock()/usersMu.Unlock() for writes in handlers.


51-51: Handle error from router.Run().

The error returned by Run() is ignored. If the server fails to start (e.g., port already in use), the program will silently exit.

Proposed fix
-	router.Run(":8080") // TODO: Start server on http://localhost:8080
+	if err := router.Run(":8080"); err != nil {
+		panic(err)
+	}

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7ac764 and 7782e53.

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

}
var nextID = 4

//main.go starts herez
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix typo in comment.

"herez" should be "here".

-//main.go starts herez
+//main.go starts here
📝 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.

Suggested change
//main.go starts herez
//main.go starts here

c.JSON(http.StatusOK, Response{
Success: true,
Data: updatedUser,
Message: "User updated Succsesfully",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix typo in message.

"Succsesfully" should be "Successfully".

-		Message: "User updated Succsesfully",
+		Message: "User updated successfully",
📝 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.

Suggested change
Message: "User updated Succsesfully",
Message: "User updated successfully",

Comment on lines +185 to +192
if c == nil {
c.JSON(http.StatusBadRequest, Response{
Success: false,
Error: "User not found",
Code: http.StatusBadRequest,
})
return
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Remove dead code: nil check on gin.Context.

This check is ineffective—if c were nil, calling c.JSON() inside the block would panic. Gin always passes a valid *gin.Context to handlers; this code is unreachable.

Proposed fix
 func deleteUser(c *gin.Context) {
-	// TODO: Get user ID from path
-	if c == nil {
-		c.JSON(http.StatusBadRequest, Response{
-			Success: false,
-			Error:   "User not found",
-			Code:    http.StatusBadRequest,
-		})
-		return
-	}
 	userID, err := strconv.Atoi(c.Param("id"))

users = append(users[:index], users[index+1:]...)
c.JSON(http.StatusOK, Response{
Success: true,
Message: "User deleted Succsesfully",
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix typo in message.

Same typo as in updateUser: "Succsesfully" should be "Successfully".

-		Message: "User deleted Succsesfully",
+		Message: "User deleted successfully",
📝 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.

Suggested change
Message: "User deleted Succsesfully",
Message: "User deleted successfully",

Comment on lines +284 to +286
if matched, _ := regexp.MatchString(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`, user.Email); !matched {
return errors.New("Email is invalid")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Email validation fails on uppercase characters.

The regex ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$ only matches lowercase. Valid emails like "John@Example.COM" will fail validation.

Proposed fix: normalize email before validation
-	if matched, _ := regexp.MatchString(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`, user.Email); !matched {
+	if matched, _ := regexp.MatchString(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`, strings.ToLower(user.Email)); !matched {
 		return errors.New("Email is invalid")
 	}

Additionally, consider compiling the regex once at package level to avoid recompilation on every call:

var emailRegex = regexp.MustCompile(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`)

@RezaSi RezaSi merged commit 53e5d92 into RezaSi:main Mar 16, 2026
5 checks passed
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.

2 participants