quiz가 없을 경우, userquizpaper 생성 중단

This commit is contained in:
JongYeob Sheen 2024-01-25 20:28:29 +09:00
parent 6bb8dd3ada
commit a402f046aa
2 changed files with 23 additions and 36 deletions

View File

@ -1,7 +1,6 @@
package controllers
import (
"fmt"
"net/http"
"strconv"
"time"
@ -23,19 +22,22 @@ type UserQuizPaperController interface {
type userQuizPaperController struct {
service services.UserQuizPaperService
userService services.UserService
quizService services.QuizService
quizPaperService services.QuizPaperService
userQuizService services.UserQuizService
}
func NewUserQuizPaperController(
service services.UserQuizPaperService,
userService services.UserService,
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,
}
@ -62,13 +64,11 @@ func (controller *userQuizPaperController) 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
}
@ -81,7 +81,6 @@ func (controller *userQuizPaperController) List(c *gin.Context) {
totalNumber, err := controller.service.Total(q)
if err != nil {
fmt.Println("error : list")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@ -151,20 +150,17 @@ func (controller *userQuizPaperController) Create(c *gin.Context) {
return
}
fmt.Println("users", request.Users)
quizPaper, err := controller.quizPaperService.Find(request.QuizPaperID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// var users []int64
// err = json.Unmarshal(request.Users, &users)
// 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)
@ -219,7 +215,6 @@ func (controller *userQuizPaperController) Update(c *gin.Context) {
return
}
fmt.Println("request", request)
userQuizPaper, err := controller.service.Get(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@ -233,30 +228,25 @@ func (controller *userQuizPaperController) Update(c *gin.Context) {
userQuizPaper.TotalScore = request.TotalScore
if request.StartAt != "" {
fmt.Println("request.StartAt", request.StartAt)
start_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.StartAt)
if err == nil {
userQuizPaper.StartAt = &start_at
}
fmt.Println("start_at", start_at)
}
if request.DoneAt != "" {
fmt.Println("request.EndAt", request.DoneAt)
done_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.DoneAt)
if err == nil {
userQuizPaper.DoneAt = &done_at
}
fmt.Println("done_at", done_at)
}
fmt.Println("userQuizPaper", userQuizPaper)
result, err := controller.service.Update(userQuizPaper)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println("result", result)
c.JSON(http.StatusOK, result)
}
@ -289,7 +279,6 @@ func (controller *userQuizPaperController) Patch(c *gin.Context) {
return
}
fmt.Println("request", request)
userQuizPaper, err := controller.service.Get(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@ -301,21 +290,17 @@ func (controller *userQuizPaperController) Patch(c *gin.Context) {
}
if request.StartAt != "" {
fmt.Println("request.StartAt", request.StartAt)
start_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.StartAt)
if err == nil {
userQuizPaper.StartAt = &start_at
}
fmt.Println("start_at", start_at)
}
if request.DoneAt != "" {
fmt.Println("request.EndAt", request.DoneAt)
done_at, err := time.Parse("2006-01-02T15:04:05-07:00", request.DoneAt)
if err == nil {
userQuizPaper.DoneAt = &done_at
}
fmt.Println("done_at", done_at)
}
result, err := controller.service.Update(userQuizPaper)
@ -324,6 +309,5 @@ func (controller *userQuizPaperController) Patch(c *gin.Context) {
return
}
fmt.Println("result", result)
c.JSON(http.StatusOK, result)
}

View File

@ -20,10 +20,11 @@ type UserQuizPaperRouter interface {
}
type userQuizPaperRouter struct {
db *gorm.DB
repository repositories.UserQuizPaperRepository
service services.UserQuizPaperService
controller controllers.UserQuizPaperController
db *gorm.DB
repository repositories.UserQuizPaperRepository
quizRepository repositories.QuizRepository
service services.UserQuizPaperService
controller controllers.UserQuizPaperController
// quizPaperRepository repositories.QuizPaperRepository
// userRepository repositories.UserRepository
@ -42,18 +43,20 @@ func NewUserQuizPaperRouter(db *gorm.DB, router *gin.Engine) *userQuizPaperRoute
userQuizRepository := repositories.NewUserQuizRepository(db)
service := services.NewUserQuizPaperService(repository)
quizService := services.NewQuizService(quizRepository)
quizPaperService := services.NewQuizPaperService(quizPaperRepository)
userService := services.NewUserService(userRepository, tokenRepository)
userQuizService := services.NewUserQuizService(userQuizRepository, quizRepository)
controller := controllers.NewUserQuizPaperController(service, userService, quizPaperService, userQuizService)
controller := controllers.NewUserQuizPaperController(service, quizService, quizPaperService, userQuizService, userService)
return &userQuizPaperRouter{
db: db,
repository: repository,
service: service,
controller: controller,
router: router,
db: db,
repository: repository,
quizRepository: quizRepository,
service: service,
controller: controller,
router: router,
}
}