Skip to content

Commit 219b21e

Browse files
Add solution for gin challenge-1-basic-routing (#708)
Co-authored-by: go-interview-practice-bot[bot] <230190823+go-interview-practice-bot[bot]@users.noreply.github.com>
1 parent f811e38 commit 219b21e

File tree

1 file changed

+253
-0
lines changed
  • packages/gin/challenge-1-basic-routing/submissions/AlexO-85

1 file changed

+253
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
// User represents a user in our system
12+
type User struct {
13+
ID int `json:"id" binding:"omitempty,number"`
14+
Name string `json:"name" binding:"required"`
15+
Email string `json:"email" binding:"required,email"`
16+
Age int `json:"age" binding:"required,number"`
17+
}
18+
19+
// Response represents a standard API response
20+
type Response struct {
21+
Success bool `json:"success"`
22+
Data interface{} `json:"data,omitempty"`
23+
Message string `json:"message,omitempty"`
24+
Error string `json:"error,omitempty"`
25+
Code int `json:"code,omitempty"`
26+
}
27+
28+
// In-memory storage
29+
var users = []User{
30+
{ID: 1, Name: "John Doe", Email: "[email protected]", Age: 30},
31+
{ID: 2, Name: "Jane Smith", Email: "[email protected]", Age: 25},
32+
{ID: 3, Name: "Bob Wilson", Email: "[email protected]", Age: 35},
33+
}
34+
var nextID = 4
35+
36+
func main() {
37+
router := gin.Default()
38+
39+
// TODO: Setup routes
40+
// GET /users - Get all users
41+
router.GET("/users", getAllUsers)
42+
// GET /users/:id - Get user by ID
43+
router.GET("/users/:id", getUserByID)
44+
// POST /users - Create new user
45+
router.POST("/users", createUser)
46+
// PUT /users/:id - Update user
47+
router.PUT("/users/:id", updateUser)
48+
// DELETE /users/:id - Delete user
49+
router.DELETE("/users/:id", deleteUser)
50+
// GET /users/search - Search users by name
51+
router.GET("/users/search", searchUsers)
52+
53+
router.Run("localhost:8080")
54+
}
55+
56+
// TODO: Implement handler functions
57+
58+
// getAllUsers handles GET /users
59+
func getAllUsers(c *gin.Context) {
60+
response := Response{
61+
Success: true,
62+
Data: users,
63+
}
64+
c.IndentedJSON(http.StatusOK, response)
65+
}
66+
67+
// getUserByID handles GET /users/:id
68+
func getUserByID(c *gin.Context) {
69+
idStr := c.Param("id")
70+
// Handle invalid ID format
71+
id, err := strconv.Atoi(idStr)
72+
if err != nil {
73+
response := Response{
74+
Success: false,
75+
Error: "User ID format is wrong",
76+
Message: err.Error(),
77+
Code: http.StatusBadRequest,
78+
}
79+
c.IndentedJSON(http.StatusBadRequest, response)
80+
return
81+
}
82+
83+
if user, _ := findUserByID(id); user != nil {
84+
response := Response{
85+
Success: true,
86+
Data: user,
87+
}
88+
c.IndentedJSON(http.StatusOK, response)
89+
return
90+
}
91+
92+
response := Response{
93+
Success: false,
94+
Error: "User not found",
95+
Code: http.StatusNotFound,
96+
}
97+
98+
c.IndentedJSON(http.StatusNotFound, response)
99+
}
100+
101+
// createUser handles POST /users
102+
func createUser(c *gin.Context) {
103+
var user User
104+
105+
if err := c.ShouldBindJSON(&user); err != nil {
106+
response := Response{
107+
Success: false,
108+
Error: "Bas request",
109+
Message: err.Error(),
110+
Code: http.StatusBadRequest,
111+
}
112+
c.IndentedJSON(http.StatusBadRequest, response)
113+
return
114+
}
115+
116+
user.ID = nextID
117+
users = append(users, user)
118+
nextID++
119+
120+
response := Response{
121+
Success: true,
122+
Data: user,
123+
}
124+
c.IndentedJSON(http.StatusCreated, response)
125+
}
126+
127+
// updateUser handles PUT /users/:id
128+
func updateUser(c *gin.Context) {
129+
idStr := c.Param("id")
130+
// Handle invalid ID format
131+
id, err := strconv.Atoi(idStr)
132+
if err != nil {
133+
response := Response{
134+
Success: false,
135+
Error: "User ID format is wrong",
136+
Message: err.Error(),
137+
Code: http.StatusBadRequest,
138+
}
139+
c.IndentedJSON(http.StatusBadRequest, response)
140+
return
141+
}
142+
143+
var newUser User
144+
145+
if err := c.ShouldBindJSON(&newUser); err != nil {
146+
response := Response{
147+
Success: false,
148+
Error: "Bas request",
149+
Message: err.Error(),
150+
Code: http.StatusBadRequest,
151+
}
152+
c.IndentedJSON(http.StatusBadRequest, response)
153+
return
154+
}
155+
156+
if user, _ := findUserByID(id); user != nil {
157+
user.Name = newUser.Name
158+
user.Email = newUser.Email
159+
user.Age = newUser.Age
160+
161+
response := Response{
162+
Success: true,
163+
Data: user,
164+
}
165+
c.IndentedJSON(http.StatusOK, response)
166+
return
167+
}
168+
169+
response := Response{
170+
Success: false,
171+
Error: "User not found",
172+
Code: http.StatusNotFound,
173+
}
174+
175+
c.IndentedJSON(http.StatusNotFound, response)
176+
}
177+
178+
// deleteUser handles DELETE /users/:id
179+
func deleteUser(c *gin.Context) {
180+
idStr := c.Param("id")
181+
// Handle invalid ID format
182+
id, err := strconv.Atoi(idStr)
183+
if err != nil {
184+
response := Response{
185+
Success: false,
186+
Error: "User ID format is wrong",
187+
Message: err.Error(),
188+
Code: http.StatusBadRequest,
189+
}
190+
c.IndentedJSON(http.StatusBadRequest, response)
191+
return
192+
}
193+
194+
if user, i := findUserByID(id); user != nil {
195+
response := Response{
196+
Success: true,
197+
Data: user,
198+
}
199+
c.IndentedJSON(http.StatusOK, response)
200+
201+
users = append(users[:i], users[i+1:]...)
202+
203+
return
204+
}
205+
206+
response := Response{
207+
Success: false,
208+
Error: "User not found",
209+
Code: http.StatusNotFound,
210+
}
211+
212+
c.IndentedJSON(http.StatusNotFound, response)
213+
}
214+
215+
// searchUsers handles GET /users/search?name=value
216+
func searchUsers(c *gin.Context) {
217+
218+
name := strings.ToLower(c.Query("name"))
219+
220+
if name == "" {
221+
response := Response{
222+
Success: false,
223+
Error: "Name parameter is missing",
224+
Code: http.StatusBadRequest,
225+
}
226+
c.IndentedJSON(http.StatusBadRequest, response)
227+
return
228+
}
229+
230+
found := []User{}
231+
232+
for _, user := range users {
233+
if strings.Contains(strings.ToLower(user.Name), name) {
234+
found = append(found, user)
235+
}
236+
}
237+
238+
response := Response{
239+
Success: true,
240+
Data: found,
241+
}
242+
c.IndentedJSON(http.StatusOK, response)
243+
}
244+
245+
// Helper function to find user by ID
246+
func findUserByID(id int) (*User, int) {
247+
for i := range users {
248+
if users[i].ID == id {
249+
return &users[i], i
250+
}
251+
}
252+
return nil, -1
253+
}

0 commit comments

Comments
 (0)