314 lines
7.9 KiB
Go
314 lines
7.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"learnsteam/cslms-api/internal/models"
|
|
"learnsteam/cslms-api/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserQuizPaperController interface {
|
|
List(*gin.Context)
|
|
Find(*gin.Context)
|
|
Create(*gin.Context)
|
|
Update(*gin.Context)
|
|
Patch(*gin.Context)
|
|
}
|
|
|
|
type userQuizPaperController struct {
|
|
service services.UserQuizPaperService
|
|
userService services.UserService
|
|
quizService services.QuizService
|
|
quizPaperService services.QuizPaperService
|
|
userQuizService services.UserQuizService
|
|
}
|
|
|
|
func NewUserQuizPaperController(
|
|
service services.UserQuizPaperService,
|
|
quizService services.QuizService,
|
|
quizPaperService services.QuizPaperService,
|
|
userQuizService services.UserQuizService,
|
|
userService services.UserService,
|
|
) UserQuizPaperController {
|
|
return &userQuizPaperController{
|
|
service: service,
|
|
userService: userService,
|
|
quizService: quizService,
|
|
quizPaperService: quizPaperService,
|
|
userQuizService: userQuizService,
|
|
}
|
|
}
|
|
|
|
// UserQuizPaper 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.UserQuizPaperListResponse
|
|
// @Router /userquizpaper [get]
|
|
func (controller *userQuizPaperController) List(c *gin.Context) {
|
|
q := c.DefaultQuery("q", "")
|
|
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
|
if err != nil {
|
|
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 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
totalPage := int64(totalNumber/int64(limit) + 1)
|
|
|
|
response := models.UserQuizPaperListResponse{
|
|
Data: *result,
|
|
Total: totalNumber,
|
|
Page: page,
|
|
TotalPage: totalPage,
|
|
PageSize: limit,
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// Get userQuizPaper
|
|
//
|
|
// @Summary 응시 정보 가져오기
|
|
// @Description ID로 응시 정보를 가져옵니다.
|
|
// @Tags 사용자퀴즈페이퍼
|
|
//
|
|
// @Accept json
|
|
// @Produce json
|
|
//
|
|
// @Security Bearer
|
|
//
|
|
// @Param id path string true "응시 ID"
|
|
//
|
|
// @Success 200 {object} models.UserQuizPaperResponse
|
|
// @Router /userquizpaper/{id} [get]
|
|
func (controller *userQuizPaperController) 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 UserQuizPaper
|
|
//
|
|
// @Summary 응시 매칭 생성
|
|
// @Description 응시 매칭을 만듭니다.
|
|
// @Tags 사용자퀴즈페이퍼
|
|
//
|
|
// @Accept json
|
|
// @Produce json
|
|
//
|
|
// @Security Bearer
|
|
//
|
|
// @Param userQuizPaperBody body models.UserQuizPaperRequest true "UserQuizPaper Body"
|
|
//
|
|
// @Success 200 {object} []models.UserQuizPaper
|
|
// @Router /userquizpaper [post]
|
|
func (controller *userQuizPaperController) Create(c *gin.Context) {
|
|
var request models.UserQuizPaperRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
quizPaper, err := controller.quizPaperService.Find(request.QuizPaperID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
quizzes, err := controller.quizService.List(request.QuizPaperID, 1, 1000)
|
|
if len(*quizzes) == 0 || err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "No quiz"})
|
|
return
|
|
}
|
|
|
|
templates := controller.service.Generate(request.Users, quizPaper)
|
|
userQuizPapers, err := controller.service.Insert(templates)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var userquizzes []models.UserQuiz
|
|
for _, userQuizPaper := range userQuizPapers {
|
|
items, err := controller.userQuizService.Generate(request.QuizPaperID, &userQuizPaper)
|
|
if err == nil {
|
|
userquizzes = append(userquizzes, items...)
|
|
}
|
|
}
|
|
|
|
_, err = controller.userQuizService.Insert(userquizzes)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"userQuizPaper": userQuizPapers})
|
|
}
|
|
|
|
// Update UserQuizPaper
|
|
//
|
|
// @Summary 응시 수정
|
|
// @Description 응시를 수정합니다.
|
|
// @Tags 사용자퀴즈페이퍼
|
|
//
|
|
// @Accept json
|
|
// @Produce json
|
|
//
|
|
// @Security Bearer
|
|
//
|
|
// @Param id path int64 true "UserQuizPaper ID"
|
|
// @Param userQuizPaperUpdateBody body models.UserQuizPaperUpdateRequest true "UserQuizPaper Update Body"
|
|
//
|
|
// @Success 200 {object} models.UserQuizPaperResponse
|
|
// @Router /userquizpaper/{id} [put]
|
|
func (controller *userQuizPaperController) 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.UserQuizPaperUpdateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
userQuizPaper, err := controller.service.Get(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
userQuizPaper.QuizPaperID = request.QuizPaperID
|
|
userQuizPaper.UserID = request.UserID
|
|
userQuizPaper.Status = request.Status
|
|
userQuizPaper.UserScore = request.UserScore
|
|
userQuizPaper.TotalScore = request.TotalScore
|
|
|
|
if request.StartAt != "" {
|
|
start_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.StartAt)
|
|
if err == nil {
|
|
userQuizPaper.StartAt = &start_at
|
|
}
|
|
}
|
|
|
|
if request.DoneAt != "" {
|
|
done_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.DoneAt)
|
|
if err == nil {
|
|
userQuizPaper.DoneAt = &done_at
|
|
}
|
|
}
|
|
|
|
result, err := controller.service.Update(userQuizPaper)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// Patch UserQuizPaper
|
|
//
|
|
// @Summary 응시 정보 변경
|
|
// @Description 응시 정보를 변경합니다.
|
|
// @Tags 사용자퀴즈페이퍼
|
|
//
|
|
// @Accept json
|
|
// @Produce json
|
|
//
|
|
// @Security Bearer
|
|
//
|
|
// @Param id path int64 true "UserQuizPaper ID"
|
|
// @Param userQuizPaperPatchBody body models.UserQuizPaperPatchRequest true "UserQuizPaper Patch Body (변경할 필드만 입력)"
|
|
//
|
|
// @Success 200 {object} models.UserQuizPaper
|
|
// @Router /userquizpaper/{id} [patch]
|
|
func (controller *userQuizPaperController) Patch(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.UserQuizPaperPatchRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
userQuizPaper, err := controller.service.Get(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if request.Status != "" {
|
|
userQuizPaper.Status = request.Status
|
|
}
|
|
|
|
if request.StartAt != "" {
|
|
start_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.StartAt)
|
|
if err == nil {
|
|
userQuizPaper.StartAt = &start_at
|
|
}
|
|
}
|
|
|
|
if request.DoneAt != "" {
|
|
done_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.DoneAt)
|
|
if err == nil {
|
|
userQuizPaper.DoneAt = &done_at
|
|
}
|
|
}
|
|
|
|
result, err := controller.service.Update(userQuizPaper)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|