mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 01:19:12 +08:00
Update v3/examples/gin-service/services/gin_service.go
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
parent
e97c390f12
commit
1f5e9852e4
@ -137,14 +137,41 @@ func (s *GinService) setupRoutes() {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
})
|
||||
|
||||
// Create a new user
|
||||
users.POST("", func(c *gin.Context) {
|
||||
// import block (ensure this exists in your file)
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ...
|
||||
|
||||
// Create a new user
|
||||
users.POST("", func(c *gin.Context) {
|
||||
var newUser User
|
||||
if err := c.ShouldBindJSON(&newUser); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if newUser.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Name is required"})
|
||||
return
|
||||
}
|
||||
if newUser.Email == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Email is required"})
|
||||
return
|
||||
}
|
||||
// Basic email validation (consider using a proper validator library in production)
|
||||
if !strings.Contains(newUser.Email, "@") {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid email format"})
|
||||
return
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
@ -160,7 +187,7 @@ func (s *GinService) setupRoutes() {
|
||||
|
||||
// Emit an event to notify about the new user
|
||||
s.app.EmitEvent("user-created", newUser)
|
||||
})
|
||||
})
|
||||
|
||||
// Delete a user
|
||||
users.DELETE("/:id", func(c *gin.Context) {
|
||||
|
Loading…
Reference in New Issue
Block a user