cslms-api/internal/controllers/center.go

221 lines
4.9 KiB
Go
Raw Normal View History

2023-12-27 17:31:49 +09:00
package controllers
import (
"fmt"
"net/http"
"strconv"
"time"
"learnsteam/cslms-api/internal/models"
"learnsteam/cslms-api/internal/services"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type CenterController interface {
List(*gin.Context)
Find(*gin.Context)
Create(*gin.Context)
Update(*gin.Context)
}
type centerController struct {
service services.CenterService
tokenService services.TokenService
}
func NewCenterController(service services.CenterService, tokenService services.TokenService) CenterController {
return &centerController{
service: service,
tokenService: tokenService,
}
}
// Center List
//
// @Summary 센터 목록 가져오기
// @Description 센터 목록을 가져옵니다.
// @Tags 센터
//
// @Accept json
// @Produce json
//
// @Security Bearer
//
// @Param q query string false "검색어"
// @Param page query int false "페이지"
// @Param limit query int false "페이지 사이즈"
//
// @Success 200 {object} models.CenterListResponse
// @Router /center [get]
func (controller *centerController) 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.CenterListResponse{
Data: *result,
Total: totalPage,
Page: page,
TotalPage: totalPage,
PageSize: limit,
}
c.JSON(http.StatusOK, response)
c.JSON(http.StatusOK, response)
}
// Get Center
//
// @Summary 센터 정보 가져오기
// @Description ID로 센터 정보를 가져옵니다.
// @Tags 센터
//
// @Accept json
// @Produce json
//
// @Security Bearer
//
// @Param id path string true "센터 ID"
//
// @Success 200 {object} models.Center
// @Router /center/{id} [get]
func (controller *centerController) Find(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
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)
}
// Create Center
//
// @Summary 센터 생성
// @Description 센터를 만듭니다.
// @Tags 센터
//
// @Accept json
// @Produce json
//
// @Security Bearer
//
// @Param quizBody body models.CenterRequest true "Center Body"
//
// @Success 200 {object} models.Center
// @Router /center [post]
2023-12-27 17:31:49 +09:00
func (controller *centerController) Create(c *gin.Context) {
var request models.CenterRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
center := &models.Center{
GUID: uuid.NewString(),
Title: request.Title,
Name: request.Name,
OwnerID: request.OwnerID,
Content: request.Content,
Info: request.Info,
Memo: request.Memo,
Status: request.Status,
UpdatedAt: time.Now(),
CreatedAt: time.Now(),
}
result, err := controller.service.Create(center)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}
// Update Center
//
// @Summary 센터 수정
// @Description 센터를 수정합니다.
// @Tags 센터
//
// @Accept json
// @Produce json
//
// @Security Bearer
//
// @Param id path int64 true "Center ID"
// @Param centerBody body models.CenterRequest true "Center Body"
//
// @Success 200 {object} models.Center
// @Router /center/{id} [put]
func (controller *centerController) Update(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var request models.CenterRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
center, err := controller.service.Find(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
center.OwnerID = request.OwnerID
center.Title = request.Title
center.Name = request.Name
center.Content = request.Content
center.Info = request.Info
center.Memo = request.Memo
center.Status = request.Status
center.UpdatedAt = time.Now()
result, err := controller.service.Update(center)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}