learnsteam-quiz-api/internal/controllers/user.go

118 lines
2.6 KiB
Go
Raw Normal View History

2023-10-19 22:47:12 +09:00
package controllers
import (
"fmt"
"net/http"
2023-11-17 01:38:01 +09:00
"strconv"
2023-10-19 22:47:12 +09:00
2023-11-17 01:38:01 +09:00
"learnsteam/learsteam-quiz-api/internal/models"
"learnsteam/learsteam-quiz-api/internal/services"
2023-10-19 22:47:12 +09:00
"github.com/gin-gonic/gin"
)
type UserController interface {
2023-11-17 01:38:01 +09:00
List(*gin.Context)
2023-10-19 22:47:12 +09:00
Find(*gin.Context)
}
type userController struct {
service services.UserService
tokenService services.TokenService
}
func NewUserController(service services.UserService, tokenService services.TokenService) UserController {
return &userController{
service: service,
tokenService: tokenService,
}
}
2023-11-17 01:38:01 +09:00
// User List
//
2023-11-17 14:27:42 +09:00
// @Summary 사용자 목록 가져오기
2023-11-17 01:38:01 +09:00
// @Description 사용자 목록을 가져옵니다.
2023-11-17 14:27:42 +09:00
// @Tags User
2023-11-17 01:38:01 +09:00
// @Accept json
// @Produce json
//
// @Param q query string false "검색어"
// @Param page query int false "페이지"
// @Param limit query int false "페이지 사이즈"
//
// @Success 200 {object} models.UserListResponse
// @Router /user [get]
func (controller *userController) List(c *gin.Context) {
q := c.DefaultQuery("q", "")
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
fmt.Println("error : page")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
if err != nil {
fmt.Println("error : limit")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
result, err := controller.service.List(q, page, limit)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
totalNumber, err := controller.service.Total(q)
if err != nil {
fmt.Println("error : list")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
totalPage := int64(totalNumber/int64(limit) + 1)
response := models.UserListResponse{
Data: *result,
Total: totalPage,
Page: page,
TotalPage: totalPage,
PageSize: limit,
}
c.JSON(http.StatusOK, response)
c.JSON(http.StatusOK, response)
}
// Get User
//
2023-11-17 14:27:42 +09:00
// @Summary 사용자 정보 가져오기
// @Description ID로 사용자 정보를 가져옵니다.
// @Tags User
2023-11-17 01:38:01 +09:00
// @Accept json
// @Produce json
2023-11-17 14:27:42 +09:00
//
2023-11-17 01:38:01 +09:00
// @Param id path string true "사용자 ID"
2023-11-17 14:27:42 +09:00
//
2023-11-17 01:38:01 +09:00
// @Success 200 {object} models.User
// @Router /user/{id} [get]
2023-10-19 22:47:12 +09:00
func (controller *userController) Find(c *gin.Context) {
id := c.Param("id")
user_id := c.GetString("sub")
fmt.Println("id", id)
fmt.Println("user_id", user_id)
if user_id != id {
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
return
}
2023-11-17 01:38:01 +09:00
result, err := controller.service.Find(id)
2023-10-19 22:47:12 +09:00
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}