1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| package handlers
import ( "net/http" "strconv"
"github.com/gin-gonic/gin" "myapp/models" "myapp/repositories" )
type UserHandler struct { repo repositories.UserRepository }
func NewUserHandler(repo repositories.UserRepository) *UserHandler { return &UserHandler{repo: repo} }
func (h *UserHandler) List(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
users, total, err := h.repo.List(page, limit) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch users"}) return }
c.JSON(http.StatusOK, gin.H{ "data": users, "total": total, "page": page, "limit": limit, }) }
func (h *UserHandler) Get(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"}) return }
user, err := h.repo.FindByID(uint(id)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "User not found"}) return }
c.JSON(http.StatusOK, gin.H{"data": user}) }
func (h *UserHandler) Create(c *gin.Context) { var req models.CreateUserRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return }
user := &models.User{ Username: req.Username, Email: req.Email, Password: hashPassword(req.Password), }
if err := h.repo.Create(user); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"}) return }
c.JSON(http.StatusCreated, gin.H{"data": user}) }
func (h *UserHandler) Update(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"}) return }
var req models.UpdateUserRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return }
user, err := h.repo.FindByID(uint(id)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "User not found"}) return }
if req.Username != "" { user.Username = req.Username } if req.Email != "" { user.Email = req.Email }
if err := h.repo.Update(user); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user"}) return }
c.JSON(http.StatusOK, gin.H{"data": user}) }
func (h *UserHandler) Delete(c *gin.Context) { id, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"}) return }
if err := h.repo.Delete(uint(id)); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user"}) return }
c.Status(http.StatusNoContent) }
|
Comments