init project
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"furtuna-be/internal/common/errors"
|
||||
"furtuna-be/internal/entity"
|
||||
"furtuna-be/internal/handlers/request"
|
||||
"furtuna-be/internal/handlers/response"
|
||||
"furtuna-be/internal/services"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service services.User
|
||||
}
|
||||
|
||||
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route := group.Group("/user")
|
||||
|
||||
route.POST("/", jwt, h.Create)
|
||||
route.GET("/list", jwt, h.GetAll)
|
||||
route.GET("/:id", jwt, h.GetByID)
|
||||
route.PUT("/:id", jwt, h.Update)
|
||||
route.DELETE("/:id", jwt, h.Delete)
|
||||
}
|
||||
|
||||
func NewHandler(service services.User) *Handler {
|
||||
return &Handler{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Create handles the creation of a new user.
|
||||
// @Summary Create a new user
|
||||
// @Description Create a new user based on the provided data.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param req body request.User true "New user details"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.User} "User created successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/user [post]
|
||||
// @Tags User APIs
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
var req request.User
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorInvalidRequest)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.service.Create(ctx, req.ToEntity())
|
||||
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := response.User{
|
||||
ID: res.ID,
|
||||
Name: res.Name,
|
||||
Email: res.Email,
|
||||
RoleID: int64(res.RoleID),
|
||||
PartnerID: res.PartnerID,
|
||||
Status: string(res.Status),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: resp,
|
||||
})
|
||||
}
|
||||
|
||||
// Update handles the update of an existing user.
|
||||
// @Summary Update an existing user
|
||||
// @Description Update the details of an existing user based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "User ID to update"
|
||||
// @Param req body request.User true "Updated user details"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.User} "User updated successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/user/{id} [put]
|
||||
// @Tags User APIs
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
if !ctx.IsSuperAdmin() {
|
||||
response.ErrorWrapper(c, errors.ErrorUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
userID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var req request.User
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(req); err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
updatedUser, err := h.service.Update(ctx, userID, req.ToEntity())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toUserResponse(updatedUser),
|
||||
})
|
||||
}
|
||||
|
||||
// GetAll retrieves a list of users.
|
||||
// @Summary Get a list of users
|
||||
// @Description Get a paginated list of users based on query parameters.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param Limit query int false "Number of items to retrieve (default 10)"
|
||||
// @Param Offset query int false "Offset for pagination (default 0)"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.UserList} "List of users"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/user/list [get]
|
||||
// @Tags User APIs
|
||||
func (h *Handler) GetAll(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
var req request.UserParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
|
||||
}
|
||||
|
||||
if !ctx.IsSuperAdmin() {
|
||||
response.ErrorWrapper(c, errors.ErrorUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
users, total, err := h.service.GetAll(ctx, req.ToEntity())
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toUserResponseList(users, int64(total), req),
|
||||
})
|
||||
}
|
||||
|
||||
// GetByID retrieves details of a specific user by ID.
|
||||
// @Summary Get details of a user by ID
|
||||
// @Description Get details of a user based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "User ID to retrieve"
|
||||
// @Success 200 {object} response.BaseResponse{data=response.User} "User details"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/user/{id} [get]
|
||||
// @Tags User APIs
|
||||
func (h *Handler) GetByID(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
id := c.Param("id")
|
||||
|
||||
// Parse the ID into a uint
|
||||
userID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.service.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, response.BaseResponse{
|
||||
Success: false,
|
||||
Status: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toUserResponse(res),
|
||||
})
|
||||
}
|
||||
|
||||
// Delete handles the deletion of a user by ID.
|
||||
// @Summary Delete a user by ID
|
||||
// @Description Delete a user based on the provided ID.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "JWT token"
|
||||
// @Param id path int64 true "User ID to delete"
|
||||
// @Success 200 {object} response.BaseResponse "User deleted successfully"
|
||||
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
||||
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
||||
// @Router /api/v1/user/{id} [delete]
|
||||
// @Tags User APIs
|
||||
func (h *Handler) Delete(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
id := c.Param("id")
|
||||
|
||||
// Parse the ID into a uint
|
||||
userID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.Delete(ctx, userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, response.BaseResponse{
|
||||
Success: false,
|
||||
Status: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) toUserResponse(resp *entity.User) response.User {
|
||||
return response.User{
|
||||
ID: resp.ID,
|
||||
Name: resp.Name,
|
||||
Email: resp.Email,
|
||||
Status: string(resp.Status),
|
||||
RoleID: int64(resp.RoleID),
|
||||
RoleName: resp.RoleName,
|
||||
PartnerID: resp.PartnerID,
|
||||
BranchName: resp.BranchName,
|
||||
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) toUserResponseList(resp []*entity.User, total int64, req request.UserParam) response.UserList {
|
||||
var users []response.User
|
||||
for _, b := range resp {
|
||||
users = append(users, h.toUserResponse(b))
|
||||
}
|
||||
|
||||
return response.UserList{
|
||||
Users: users,
|
||||
Total: total,
|
||||
Limit: req.Limit,
|
||||
Offset: req.Offset,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user