quiz count

This commit is contained in:
JongYeob Sheen 2024-02-22 21:11:18 +09:00
parent 9e5f24949d
commit b6d1d7f44e
8 changed files with 85 additions and 16 deletions

View File

@ -24,12 +24,14 @@ type QuizController interface {
}
type quizController struct {
service services.QuizService
service services.QuizService
quizPaperService services.QuizPaperService
}
func NewQuizController(service services.QuizService) QuizController {
func NewQuizController(service services.QuizService, quizPaperService services.QuizPaperService) QuizController {
return &quizController{
service: service,
service: service,
quizPaperService: quizPaperService,
}
}
@ -174,6 +176,24 @@ func (controller *quizController) Create(c *gin.Context) {
return
}
count, err := controller.service.QuizCount(request.QuizPaperID)
if 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
}
quizpaper.QuizCount = int(count)
_, err = controller.quizPaperService.Update(quizpaper)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}
@ -264,7 +284,7 @@ func (controller *quizController) Delete(c *gin.Context) {
return
}
_, err = controller.service.Find(id)
quiz, err := controller.service.Find(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
@ -276,5 +296,23 @@ func (controller *quizController) Delete(c *gin.Context) {
return
}
count, err := controller.service.QuizCount(quiz.QuizPaperID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
quizpaper, err := controller.quizPaperService.Find(quiz.QuizPaperID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
quizpaper.QuizCount = int(count)
_, err = controller.quizPaperService.Update(quizpaper)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "success"})
}

View File

@ -285,6 +285,7 @@ func (controller *quizPaperController) Copy(c *gin.Context) {
Content: template.Content,
Category: template.Category,
Tag: template.Tag,
QuizCount: 0,
Status: template.Status,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),

View File

@ -15,6 +15,7 @@ type QuizPaper struct {
Content string `json:"content" db:"content" example:"퀴즈 시트 설명" gorm:"column:content;size:512;"`
Category string `json:"category" db:"category" example:"파이썬기본" gorm:"column:category;size:255;"`
Tag datatypes.JSON `json:"tag" db:"tag" gorm:"column:tag;"`
QuizCount int `json:"quiz_count" db:"quiz_count" example:"5" gorm:"column:quiz_count;"`
Status string `json:"status" example:"on" gorm:"column:status;size:10;index;"`
UpdatedAt time.Time `json:"-" gorm:"column:updated_at;type:DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index;->:false"`
CreatedAt time.Time `json:"created_at" db:"created_at" example:"2023-11-10T13:10:00+09:00" gorm:"column:created_at;type:DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;index;"`
@ -42,6 +43,7 @@ type QuizPaperResponse struct {
Content string `json:"content" example:"퀴즈 시트 설명"`
Category string `json:"category" example:"파이썬기본"`
Tag []string `json:"tag" example:"Python,AI,Robot,파이썬"`
QuizCount int `json:"quiz_count" example:"5"`
Status string `json:"status" example:"on"`
CreatedAt time.Time `json:"created_at" example:"2023-11-10T13:10:00+09:00"`
}

View File

@ -11,8 +11,9 @@ type UserQuizPaper struct {
QuizPaperID int64 `json:"quiz_paper_id" db:"quiz_paper_id" example:"1000001" gorm:"column:quiz_paper_id;index;"`
UserID int64 `json:"user_id" db:"user_id" example:"1000002" gorm:"column:user_id;index;"`
UserScore float32 `json:"user_score" db:"user_score" example:"5" gorm:"column:user_score;"`
TotalScore float32 `json:"total_score" db:"total_score" example:"100" gorm:"column:total_score;"`
TotalScore float32 `json:"total_score" db:"total_score" example:"5" gorm:"column:total_score;"`
Status string `json:"status" example:"wating" gorm:"column:status;size:10;index;"`
QuizCount int `json:"quiz_count" db:"quiz_count" example:"5" gorm:"column:quiz_count;"`
StartAt *time.Time `json:"start_at" gorm:"column:start_at;index;"`
DoneAt *time.Time `json:"done_at" gorm:"column:done_at;index;"`
UpdatedAt time.Time `json:"-" gorm:"column:updated_at;type:DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index;->:false"`
@ -39,6 +40,7 @@ type UserQuizPaperResponse struct {
LastName string `json:"last_name" db:"last_name" example:"홍"`
UserScore float32 `json:"user_score" db:"user_score" example:"5"`
TotalScore float32 `json:"total_score" db:"total_score" example:"100"`
QuizCount int `json:"quiz_count" db:"quiz_count" example:"5"`
Status string `json:"status" example:"wating"`
Category string `json:"category" example:"파이썬기본"`
Title string `json:"title" example:"출력 Print"`

View File

@ -24,6 +24,7 @@ type QuizRepository interface {
Create(*models.Quiz) (*models.Quiz, error)
Update(*models.Quiz) (*models.Quiz, error)
Delete(int64) error
QuizCount(int64) (int64, error)
}
func (r *quizRepository) List(quiz_paper_id int64, page int, limit int) (*[]models.Quiz, error) {
@ -79,3 +80,14 @@ func (r *quizRepository) Delete(id int64) error {
err := r.DB.Delete(&quiz).Error
return err
}
func (r *quizRepository) QuizCount(quizpaper_id int64) (int64, error) {
var count int64
var err error
err = r.DB.Model(&models.Quiz{}).
Where("quiz_paper_id = ?", quizpaper_id).
Count(&count).
Error
return count, err
}

View File

@ -20,24 +20,30 @@ type QuizRouter interface {
}
type quizRouter struct {
db *gorm.DB
repository repositories.QuizRepository
service services.QuizService
controller controllers.QuizController
router *gin.Engine
db *gorm.DB
repository repositories.QuizRepository
quizPaperRepository repositories.QuizPaperRepository
service services.QuizService
quizPaperService services.QuizPaperService
controller controllers.QuizController
router *gin.Engine
}
func NewQuizRouter(db *gorm.DB, router *gin.Engine) *quizRouter {
repository := repositories.NewQuizRepository(db)
quizPaperRepository := repositories.NewQuizPaperRepository(db)
service := services.NewQuizService(repository)
controller := controllers.NewQuizController(service)
quizPaperService := services.NewQuizPaperService(quizPaperRepository)
controller := controllers.NewQuizController(service, quizPaperService)
return &quizRouter{
db: db,
repository: repository,
service: service,
controller: controller,
router: router,
db: db,
repository: repository,
quizPaperRepository: quizPaperRepository,
service: service,
quizPaperService: quizPaperService,
controller: controller,
router: router,
}
}

View File

@ -20,6 +20,7 @@ type QuizService interface {
Create(*models.Quiz) (*models.Quiz, error)
Update(*models.Quiz) (*models.Quiz, error)
Delete(int64) error
QuizCount(int64) (int64, error)
}
func NewQuizService(repository repositories.QuizRepository) QuizService {
@ -58,6 +59,10 @@ func (s *quizService) Delete(id int64) error {
return err
}
func (s *quizService) QuizCount(quizpaper_id int64) (int64, error) {
return s.repository.QuizCount(quizpaper_id)
}
func (s *quizService) Generate(quizpaper *models.QuizPaper) ([]models.Quiz, error) {
templates, err := s.repository.List(quizpaper.ID, 1, 1000)
if err != nil {

View File

@ -83,6 +83,9 @@ func (s *userQuizPaperService) Generate(users []int64, quizPaper *models.QuizPap
CenterID: quizPaper.CenterID,
UserID: user_id,
QuizPaperID: quizPaper.ID,
QuizCount: quizPaper.QuizCount,
UserScore: 0,
TotalScore: float32(quizPaper.QuizCount),
Status: "ready",
}