Swag Docs
This commit is contained in:
@ -3,8 +3,8 @@ package controllers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"studioj/boilerplate_go/internal/models"
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
"learnsteam/learsteam-quiz-api/internal/models"
|
||||
"learnsteam/learsteam-quiz-api/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -15,15 +15,30 @@ type AuthController interface {
|
||||
}
|
||||
|
||||
type authController struct {
|
||||
service services.AuthService
|
||||
service services.AuthService
|
||||
tokenService services.TokenService
|
||||
}
|
||||
|
||||
func NewAuthController(service services.AuthService) AuthController {
|
||||
func NewAuthController(service services.AuthService, tokenService services.TokenService) AuthController {
|
||||
return &authController{
|
||||
service: service,
|
||||
service: service,
|
||||
tokenService: tokenService,
|
||||
}
|
||||
}
|
||||
|
||||
// Register
|
||||
//
|
||||
// @Summary 회원가입
|
||||
// @Description username, name, password 를 입력하여 회원가입
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
//
|
||||
// @Param username body string true "username"
|
||||
// @Param name body string true "이름"
|
||||
// @Param password body string true "비밀번호"
|
||||
//
|
||||
// @Success 200 {object} models.RegisterResponse
|
||||
// @Router /auth/register [post]
|
||||
func (controller *authController) Register(c *gin.Context) {
|
||||
var params models.RegisterRequest
|
||||
if c.BindJSON(¶ms) != nil {
|
||||
@ -37,7 +52,7 @@ func (controller *authController) Register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
token, err := controller.service.CreateToken(user.ID)
|
||||
token, err := controller.tokenService.Create(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@ -45,23 +60,36 @@ func (controller *authController) Register(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "token": token.Token, "refresh_token": token.RefreshToken})
|
||||
}
|
||||
|
||||
// Login
|
||||
//
|
||||
// @Summary 로그인
|
||||
// @Description username, password 를 입력하여 로그인
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
//
|
||||
// @Param username body string true "username"
|
||||
// @Param password body string true "비밀번호"
|
||||
//
|
||||
// @Success 200 {object} models.LoginResponse
|
||||
// @Router /auth/login [post]
|
||||
func (controller *authController) Login(c *gin.Context) {
|
||||
var params models.LoginRequest
|
||||
if c.BindJSON(¶ms) != nil {
|
||||
var request models.LoginRequest
|
||||
if c.BindJSON(&request) != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := controller.service.Login(¶ms)
|
||||
user, err := controller.service.Login(&request)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
token, err := controller.service.CreateToken(user.ID)
|
||||
token, err := controller.tokenService.Create(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "token": token.Token, "refresh_token": token.RefreshToken})
|
||||
}
|
||||
|
207
internal/controllers/program.go
Normal file
207
internal/controllers/program.go
Normal file
@ -0,0 +1,207 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"learnsteam/learsteam-quiz-api/internal/models"
|
||||
"learnsteam/learsteam-quiz-api/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ProgramController interface {
|
||||
List(*gin.Context)
|
||||
Find(*gin.Context)
|
||||
Create(*gin.Context)
|
||||
Update(*gin.Context)
|
||||
}
|
||||
|
||||
type programController struct {
|
||||
service services.ProgramService
|
||||
}
|
||||
|
||||
func NewProgramController(service services.ProgramService) ProgramController {
|
||||
return &programController{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Program List
|
||||
//
|
||||
// @Summary 프로그램 목록 가져오기
|
||||
// @Description 퀴즈 프로그램 목록을 가져옵니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tag query string false "태그"
|
||||
// @Param q query string false "검색어"
|
||||
// @Param page query int false "페이지"
|
||||
// @Param limit query int false "페이지 사이즈"
|
||||
// @Success 200 {object} models.ProgramListResponse
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /program [get]
|
||||
func (controller *programController) List(c *gin.Context) {
|
||||
tag := c.DefaultQuery("tag", "")
|
||||
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, tag, page, limit)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
totalNumber, err := controller.service.Total(q, tag)
|
||||
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.ProgramListResponse{
|
||||
Data: *result,
|
||||
Total: totalPage,
|
||||
Page: page,
|
||||
TotalPage: totalPage,
|
||||
PageSize: limit,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// Get program
|
||||
//
|
||||
// @Summary 퀴즈 프로그램 가져오기
|
||||
// @Description ID로 퀴즈 프로그램을 가져옵니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "퀴즈 프로그램 ID"
|
||||
// @Success 200 {object} models.ProgramResponse
|
||||
// @Failure 400
|
||||
// @Router /program/{id} [get]
|
||||
func (controller *programController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Create Program
|
||||
//
|
||||
// @Summary 퀴즈 프로그램 생성
|
||||
// @Description 퀴즈 프로그램을 만듭니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param subject body string true "프로그램 제목"
|
||||
// @Param course body string true "프로그램 코스"
|
||||
// @Param content body string true "프로그램 내용"
|
||||
// @Param tag body []string true "프로그램 태그"
|
||||
// @Param status body string true "프로그램 상태 on 또는 off"
|
||||
// @Param publish_at body string true "프로그램 발행 날짜"
|
||||
// @Router /program [post]
|
||||
// @Success 200 {object} models.ProgramResponse
|
||||
func (controller *programController) Create(c *gin.Context) {
|
||||
var request models.ProgramRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
publish_at, err := time.Parse("2006-01-02", request.PublishAt)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
program := &models.Program{
|
||||
ID: uuid.NewString(),
|
||||
Subject: request.Subject,
|
||||
Course: request.Course,
|
||||
Content: request.Content,
|
||||
Tag: request.Tag,
|
||||
Tags: request.Tag.String(),
|
||||
PublishAt: publish_at,
|
||||
Status: request.Status,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
fmt.Println("publish_at", program.PublishAt)
|
||||
result, err := controller.service.Create(program)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Update Program
|
||||
//
|
||||
// @Summary 퀴즈 프로그램 수정
|
||||
// @Description 퀴즈 프로그램을 수정합니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param subject body string true "프로그램 제목"
|
||||
// @Param course body string true "프로그램 코스"
|
||||
// @Param content body string true "프로그램 내용"
|
||||
// @Param tag body []string true "프로그램 태그"
|
||||
// @Param status body string true "프로그램 상태 on 또는 off"
|
||||
// @Param publish_at body string true "프로그램 발행 날짜"
|
||||
// @Router /program [put]
|
||||
// @Success 200 {object} models.ProgramResponse
|
||||
func (controller *programController) Update(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var request models.ProgramRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
program, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
publish_at, err := time.Parse("2006-01-02", request.PublishAt)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
program.Subject = request.Subject
|
||||
program.Course = request.Course
|
||||
program.Content = request.Content
|
||||
program.Tag = request.Tag
|
||||
program.Tags = request.Tag.String()
|
||||
program.PublishAt = publish_at
|
||||
program.Status = request.Status
|
||||
program.UpdatedAt = time.Now()
|
||||
|
||||
result, err := controller.service.Update(program)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
193
internal/controllers/quiz.go
Normal file
193
internal/controllers/quiz.go
Normal file
@ -0,0 +1,193 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"learnsteam/learsteam-quiz-api/internal/models"
|
||||
"learnsteam/learsteam-quiz-api/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type QuizController interface {
|
||||
List(*gin.Context)
|
||||
Find(*gin.Context)
|
||||
Create(*gin.Context)
|
||||
Update(*gin.Context)
|
||||
}
|
||||
|
||||
type quizController struct {
|
||||
service services.QuizService
|
||||
}
|
||||
|
||||
func NewQuizController(service services.QuizService) QuizController {
|
||||
return &quizController{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
// Quiz List
|
||||
//
|
||||
// @Summary 퀴즈 목록 가져오기
|
||||
// @Description 퀴즈 목록을 가져옵니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param program_id query string true "프로그램 ID"
|
||||
// @Param q query string false "검색어"
|
||||
// @Param page query int false "페이지"
|
||||
// @Param limit query int false "페이지 사이즈"
|
||||
// @Success 200 {object} models.QuizListResponse
|
||||
// @Router /quiz [get]
|
||||
func (controller *quizController) List(c *gin.Context) {
|
||||
program_id := c.DefaultQuery("program_id", "")
|
||||
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(program_id, page, limit)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
totalNumber, err := controller.service.Total(program_id)
|
||||
if err != nil {
|
||||
fmt.Println("error : list")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
totalPage := int64(totalNumber/int64(limit) + 1)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": result, "total": totalNumber, "page": page, "totalPage": totalPage, "pageSize": limit})
|
||||
}
|
||||
|
||||
// Get Quiz
|
||||
//
|
||||
// @Summary 퀴즈 가져오기
|
||||
// @Description ID로 퀴즈를 가져옵니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "퀴즈 ID"
|
||||
// @Success 200 {object} models.QuizResponse
|
||||
// @Failure 400
|
||||
// @Router /quiz/{id} [get]
|
||||
func (controller *quizController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Create Quiz
|
||||
//
|
||||
// @Summary 퀴즈 생성
|
||||
// @Description 퀴즈를 만듭니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param program_id body string true "프로그램 ID"
|
||||
// @Param sequence body int true "퀴즈 순서"
|
||||
// @Param quiz_type body string true "퀴즈 타입 : choice, check, ox, input"
|
||||
// @Param question body string true "퀴즈 문제"
|
||||
// @Param choice body []string true "퀴즈 선택지 : ['선택1','선택2','선택3', '선택4']"
|
||||
// @Param answer body []string true "퀴즈 정답 : [1,3]"
|
||||
// @Param hint body string true "퀴즈 힌트"
|
||||
// @Param comment body string true "퀴즈 해설"
|
||||
// @Success 200 {object} models.QuizResponse
|
||||
// @Router /quiz [post]
|
||||
func (controller *quizController) Create(c *gin.Context) {
|
||||
var request models.QuizRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
quiz := &models.Quiz{
|
||||
ID: uuid.NewString(),
|
||||
ProgramID: request.ProgramID,
|
||||
Sequence: request.Sequence,
|
||||
QuizType: request.QuizType,
|
||||
Question: request.Question,
|
||||
Choice: request.Choice,
|
||||
Answer: request.Answer,
|
||||
Hint: request.Hint,
|
||||
Comment: request.Comment,
|
||||
UpdatedAt: time.Now(),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
result, err := controller.service.Create(quiz)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Update Quiz
|
||||
//
|
||||
// @Summary 퀴즈 수정
|
||||
// @Description 퀴즈를 수정합니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "퀴즈 ID"
|
||||
// @Param program_id body string true "프로그램 ID"
|
||||
// @Param sequence body int true "퀴즈 순서"
|
||||
// @Param quiz_type body string true "퀴즈 타입 : choice, check, ox, input"
|
||||
// @Param question body string true "퀴즈 문제"
|
||||
// @Param choice body []string true "퀴즈 선택지 : ['선택1','선택2','선택3', '선택4']"
|
||||
// @Param answer body []string true "퀴즈 정답 : [1,3]"
|
||||
// @Param hint body string true "퀴즈 힌트"
|
||||
// @Param comment body string true "퀴즈 해설"
|
||||
// @Success 200 {object} models.QuizResponse
|
||||
// @Router /quiz [put]
|
||||
func (controller *quizController) Update(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var request models.QuizRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
quiz, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
quiz.ProgramID = request.ProgramID
|
||||
quiz.Sequence = request.Sequence
|
||||
quiz.QuizType = request.QuizType
|
||||
quiz.Question = request.Question
|
||||
quiz.Choice = request.Choice
|
||||
quiz.Answer = request.Answer
|
||||
quiz.Hint = request.Hint
|
||||
quiz.Comment = request.Comment
|
||||
quiz.UpdatedAt = time.Now()
|
||||
|
||||
result, err := controller.service.Update(quiz)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
22
internal/controllers/swagger.go
Normal file
22
internal/controllers/swagger.go
Normal file
@ -0,0 +1,22 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SwaggerController interface {
|
||||
Get(*gin.Context)
|
||||
}
|
||||
|
||||
type swaggerController struct {
|
||||
}
|
||||
|
||||
func NewSwaggerController() SwaggerController {
|
||||
return &swaggerController{}
|
||||
}
|
||||
|
||||
func (controller *swaggerController) Get(c *gin.Context) {
|
||||
c.Redirect(http.StatusFound, "/swagger/index.html")
|
||||
}
|
@ -3,13 +3,14 @@ package controllers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
"learnsteam/learsteam-quiz-api/internal/models"
|
||||
"learnsteam/learsteam-quiz-api/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TokenController interface {
|
||||
Find(*gin.Context)
|
||||
Refresh(*gin.Context)
|
||||
}
|
||||
|
||||
type tokenController struct {
|
||||
@ -22,41 +23,27 @@ func NewTokenController(service services.TokenService) TokenController {
|
||||
}
|
||||
}
|
||||
|
||||
func (controller *tokenController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("user_id")
|
||||
|
||||
if user_id != id {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func (controller *tokenController) List(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("user_id")
|
||||
|
||||
if user_id != id {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Refresh Token
|
||||
//
|
||||
// @Summary AccessToken Refresh
|
||||
// @Description AccessToken을 RefreshToken으로 갱신합니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param refresh_token body string true "RefreshToken"
|
||||
// @Router /token/refresh [post]
|
||||
// @Success 200 {object} models.ProgramResponse
|
||||
func (controller *tokenController) Refresh(c *gin.Context) {
|
||||
}
|
||||
var request models.RefreshTokenRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.Refresh(request.RefreshToken)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
@ -3,13 +3,16 @@ package controllers
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
"learnsteam/learsteam-quiz-api/internal/models"
|
||||
"learnsteam/learsteam-quiz-api/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserController interface {
|
||||
List(*gin.Context)
|
||||
Find(*gin.Context)
|
||||
}
|
||||
|
||||
@ -25,6 +28,71 @@ func NewUserController(service services.UserService, tokenService services.Token
|
||||
}
|
||||
}
|
||||
|
||||
// User List
|
||||
//
|
||||
// @Summary 사용자 목록 가져오기
|
||||
// @Description 사용자 목록을 가져옵니다.
|
||||
// @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
|
||||
//
|
||||
// @Summary 사용자 정보 가져오기
|
||||
// @Description ID로 사용자 정보를 가져옵니다.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "사용자 ID"
|
||||
// @Success 200 {object} models.User
|
||||
// @Failure 400
|
||||
// @Router /user/{id} [get]
|
||||
func (controller *userController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("sub")
|
||||
@ -36,7 +104,7 @@ func (controller *userController) Find(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.FindByID(id)
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
Reference in New Issue
Block a user