You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

44 lines
816 B

package user
import (
"net/http"
"strconv"
"task-track-backend/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func (h *UserHandler) GetUser(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "Invalid user ID",
})
return
}
var user model.User
if err := h.db.First(&user, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"message": "User not found",
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": "Failed to get user",
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "Success",
"data": user,
})
}