userquizpaper title, category 등 추가

This commit is contained in:
JongYeob Sheen 2024-01-20 17:40:22 +09:00
parent cb1cec3947
commit 6bb8dd3ada
10 changed files with 221 additions and 34 deletions

View File

@ -153,6 +153,43 @@ const docTemplate = `{
}
}
}
},
"post": {
"security": [
{
"Bearer": []
}
],
"description": "센터를 만듭니다.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"센터"
],
"summary": "센터 생성",
"parameters": [
{
"description": "Center Body",
"name": "quizBody",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.CenterRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.Center"
}
}
}
}
},
"/center/{id}": {
@ -1154,7 +1191,7 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse"
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper"
}
}
}
@ -1879,7 +1916,7 @@ const docTemplate = `{
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper"
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse"
}
},
"page": {
@ -1949,13 +1986,24 @@ const docTemplate = `{
"learnsteam_cslms-api_internal_models.UserQuizPaperResponse": {
"type": "object",
"properties": {
"category": {
"type": "string",
"example": "파이썬기본"
},
"center_id": {
"type": "integer",
"example": 1000015
},
"created_at": {
"type": "string"
},
"done_at": {
"type": "string"
},
"first_name": {
"type": "string",
"example": "길순"
},
"guid_id": {
"type": "string",
"example": "7f9329f5-2e36-4638-92d2-73064b7291a4"
@ -1964,6 +2012,10 @@ const docTemplate = `{
"type": "integer",
"example": 1000015
},
"last_name": {
"type": "string",
"example": "홍"
},
"quiz_paper_id": {
"type": "integer",
"example": 1000001
@ -1975,6 +2027,10 @@ const docTemplate = `{
"type": "string",
"example": "wating"
},
"title": {
"type": "string",
"example": "출력 Print"
},
"total_score": {
"type": "number",
"example": 100

View File

@ -145,6 +145,43 @@
}
}
}
},
"post": {
"security": [
{
"Bearer": []
}
],
"description": "센터를 만듭니다.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"센터"
],
"summary": "센터 생성",
"parameters": [
{
"description": "Center Body",
"name": "quizBody",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.CenterRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.Center"
}
}
}
}
},
"/center/{id}": {
@ -1146,7 +1183,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse"
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper"
}
}
}
@ -1871,7 +1908,7 @@
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper"
"$ref": "#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse"
}
},
"page": {
@ -1941,13 +1978,24 @@
"learnsteam_cslms-api_internal_models.UserQuizPaperResponse": {
"type": "object",
"properties": {
"category": {
"type": "string",
"example": "파이썬기본"
},
"center_id": {
"type": "integer",
"example": 1000015
},
"created_at": {
"type": "string"
},
"done_at": {
"type": "string"
},
"first_name": {
"type": "string",
"example": "길순"
},
"guid_id": {
"type": "string",
"example": "7f9329f5-2e36-4638-92d2-73064b7291a4"
@ -1956,6 +2004,10 @@
"type": "integer",
"example": 1000015
},
"last_name": {
"type": "string",
"example": "홍"
},
"quiz_paper_id": {
"type": "integer",
"example": 1000001
@ -1967,6 +2019,10 @@
"type": "string",
"example": "wating"
},
"title": {
"type": "string",
"example": "출력 Print"
},
"total_score": {
"type": "number",
"example": 100

View File

@ -510,7 +510,7 @@ definitions:
properties:
data:
items:
$ref: '#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper'
$ref: '#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse'
type: array
page:
example: 1
@ -560,17 +560,28 @@ definitions:
type: object
learnsteam_cslms-api_internal_models.UserQuizPaperResponse:
properties:
category:
example: 파이썬기본
type: string
center_id:
example: 1000015
type: integer
created_at:
type: string
done_at:
type: string
first_name:
example: 길순
type: string
guid_id:
example: 7f9329f5-2e36-4638-92d2-73064b7291a4
type: string
id:
example: 1000015
type: integer
last_name:
example:
type: string
quiz_paper_id:
example: 1000001
type: integer
@ -579,6 +590,9 @@ definitions:
status:
example: wating
type: string
title:
example: 출력 Print
type: string
total_score:
example: 100
type: number
@ -798,6 +812,29 @@ paths:
summary: 센터 목록 가져오기
tags:
- 센터
post:
consumes:
- application/json
description: 센터를 만듭니다.
parameters:
- description: Center Body
in: body
name: quizBody
required: true
schema:
$ref: '#/definitions/learnsteam_cslms-api_internal_models.CenterRequest'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/learnsteam_cslms-api_internal_models.Center'
security:
- Bearer: []
summary: 센터 생성
tags:
- 센터
/center/{id}:
get:
consumes:
@ -1397,7 +1434,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaperResponse'
$ref: '#/definitions/learnsteam_cslms-api_internal_models.UserQuizPaper'
security:
- Bearer: []
summary: 응시 정보 변경

2
go.mod
View File

@ -10,7 +10,6 @@ require (
github.com/stretchr/testify v1.8.4
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.2
golang.org/x/crypto v0.15.0
gorm.io/datatypes v1.2.0
gorm.io/driver/mysql v1.5.2
@ -49,6 +48,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/swaggo/swag v1.16.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.6.0 // indirect

View File

@ -136,7 +136,7 @@ func (controller *centerController) Find(c *gin.Context) {
// @Param quizBody body models.CenterRequest true "Center Body"
//
// @Success 200 {object} models.Center
// @Router /quiz [post]
// @Router /center [post]
func (controller *centerController) Create(c *gin.Context) {
var request models.CenterRequest
if err := c.ShouldBindJSON(&request); err != nil {

View File

@ -50,7 +50,7 @@ func NewQuizController(service services.QuizService) QuizController {
// @Param limit query int false "페이지 사이즈"
//
// @Success 200 {object} models.QuizListResponse
// @Router /quiz [get]
// @Router /quiz [get]
func (controller *quizController) List(c *gin.Context) {
quiz_paper_id, err := strconv.ParseInt(c.DefaultQuery("quiz_paper_id", ""), 10, 64)
if err != nil {

View File

@ -220,7 +220,7 @@ func (controller *userQuizPaperController) Update(c *gin.Context) {
}
fmt.Println("request", request)
userQuizPaper, err := controller.service.Find(id)
userQuizPaper, err := controller.service.Get(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
@ -274,7 +274,7 @@ func (controller *userQuizPaperController) Update(c *gin.Context) {
// @Param id path int64 true "UserQuizPaper ID"
// @Param userQuizPaperPatchBody body models.UserQuizPaperPatchRequest true "UserQuizPaper Patch Body (변경할 필드만 입력)"
//
// @Success 200 {object} models.UserQuizPaperResponse
// @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)
@ -290,7 +290,7 @@ func (controller *userQuizPaperController) Patch(c *gin.Context) {
}
fmt.Println("request", request)
userQuizPaper, err := controller.service.Find(id)
userQuizPaper, err := controller.service.Get(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
@ -318,7 +318,6 @@ func (controller *userQuizPaperController) Patch(c *gin.Context) {
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()})

View File

@ -35,11 +35,16 @@ type UserQuizPaperResponse struct {
CenterID int64 `json:"center_id" db:"center_id" example:"1000015"`
QuizPaperID int64 `json:"quiz_paper_id" db:"quiz_paper_id" example:"1000001"`
UserID int64 `json:"user_id" db:"user_id" example:"1000002"`
FirstName string `json:"first_name" db:"first_name" example:"길순"`
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"`
Status string `json:"status" example:"wating"`
Category string `json:"category" example:"파이썬기본"`
Title string `json:"title" example:"출력 Print"`
StartAt *time.Time `json:"start_at"`
DoneAt *time.Time `json:"done_at"`
CreatedAt *time.Time `json:"created_at"`
}
type UserQuizPaperUpdateRequest struct {
@ -61,9 +66,9 @@ type UserQuizPaperPatchRequest struct {
}
type UserQuizPaperListResponse struct {
Data []UserQuizPaper `json:"data"`
Total int64 `json:"total" example:"999"`
Page int `json:"page" example:"1"`
TotalPage int64 `json:"totalPage" example:"99"`
PageSize int `json:"pageSize" example:"10"`
Data []UserQuizPaperResponse `json:"data"`
Total int64 `json:"total" example:"999"`
Page int `json:"page" example:"1"`
TotalPage int64 `json:"totalPage" example:"99"`
PageSize int `json:"pageSize" example:"10"`
}

View File

@ -18,33 +18,49 @@ func NewUserQuizPaperRepository(db *gorm.DB) UserQuizPaperRepository {
}
type UserQuizPaperRepository interface {
List(string, int, int) (*[]models.UserQuizPaper, error)
List(string, int, int) (*[]models.UserQuizPaperResponse, error)
Total(string) (int64, error)
Find(int64) (*models.UserQuizPaper, error)
Find(int64) (*models.UserQuizPaperResponse, error)
Get(int64) (*models.UserQuizPaper, error)
Create(*models.UserQuizPaper) (*models.UserQuizPaper, error)
Insert([]models.UserQuizPaper) ([]models.UserQuizPaper, error)
Update(*models.UserQuizPaper) (*models.UserQuizPaper, error)
Delete(string) error
}
func (r *userQuizPaperRepository) List(q string, page int, limit int) (*[]models.UserQuizPaper, error) {
var userQuizPapers *[]models.UserQuizPaper
func (r *userQuizPaperRepository) List(q string, page int, limit int) (*[]models.UserQuizPaperResponse, error) {
var rows *[]models.UserQuizPaperResponse
var err error
offset := limit * (page - 1)
if q != "" {
err = r.DB.Offset(offset).Limit(limit).Order("created_at DESC").Where("subject LIKE ? OR program LIKE ? OR name LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").Find(&userQuizPapers).Error
err = r.DB.Offset(offset).Limit(limit).Order("UserQuizPapers.created_at DESC").
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("QuizPapers.title LIKE ? OR QuizPapers.category LIKE ? OR CONCAT(first_name, last_name) LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").
Find(&rows).Error
} else {
err = r.DB.Offset(offset).Limit(limit).Order("created_at DESC").Find(&userQuizPapers).Error
err = r.DB.Offset(offset).Limit(limit).Order("UserQuizPapers.created_at DESC").
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Find(&rows).Error
}
return userQuizPapers, err
return rows, err
}
func (r *userQuizPaperRepository) Total(q string) (int64, error) {
var total int64
var err error
if q != "" {
err = r.DB.Model(&models.UserQuizPaper{}).Where("subject LIKE ? OR program LIKE ? OR name LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").Count(&total).Error
err = r.DB.Model(&models.UserQuizPaper{}).
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("QuizPapers.title LIKE ? OR QuizPapers.category LIKE ? OR CONCAT(first_name, last_name) LIKE ?", "%"+q+"%", "%"+q+"%", "%"+q+"%").Count(&total).Error
} else {
err = r.DB.Model(&models.UserQuizPaper{}).Count(&total).Error
}
@ -52,10 +68,23 @@ func (r *userQuizPaperRepository) Total(q string) (int64, error) {
return total, err
}
func (r *userQuizPaperRepository) Find(id int64) (*models.UserQuizPaper, error) {
var userQuizPaper *models.UserQuizPaper
err := r.DB.Where("id = ?", id).First(&userQuizPaper).Error
return userQuizPaper, err
func (r *userQuizPaperRepository) Find(id int64) (*models.UserQuizPaperResponse, error) {
var row *models.UserQuizPaperResponse
err := r.DB.
Table("UserQuizPapers").
Select("UserQuizPapers.*", "Users.first_name", "Users.last_name", "QuizPapers.title", "QuizPapers.category").
Joins("LEFT JOIN Users ON Users.id = UserQuizPapers.user_id").
Joins("LEFT JOIN QuizPapers ON QuizPapers.id = UserQuizPapers.quiz_paper_id").
Where("UserQuizPapers.id = ?", id).First(&row).Error
return row, err
}
func (r *userQuizPaperRepository) Get(id int64) (*models.UserQuizPaper, error) {
var row *models.UserQuizPaper
if err := r.DB.Where("id=?", id).First(&row).Error; err != nil {
return nil, err
}
return row, nil
}
func (r *userQuizPaperRepository) Create(userQuizPaper *models.UserQuizPaper) (*models.UserQuizPaper, error) {

View File

@ -12,10 +12,11 @@ type userQuizPaperService struct {
}
type UserQuizPaperService interface {
List(string, int, int) (*[]models.UserQuizPaper, error)
List(string, int, int) (*[]models.UserQuizPaperResponse, error)
Total(string) (int64, error)
Find(int64) (*models.UserQuizPaper, error)
Find(int64) (*models.UserQuizPaperResponse, error)
Get(int64) (*models.UserQuizPaper, error)
Create(*models.UserQuizPaper) (*models.UserQuizPaper, error)
Insert([]models.UserQuizPaper) ([]models.UserQuizPaper, error)
Update(*models.UserQuizPaper) (*models.UserQuizPaper, error)
@ -29,7 +30,7 @@ func NewUserQuizPaperService(repository repositories.UserQuizPaperRepository) Us
}
}
func (s *userQuizPaperService) List(q string, page int, limit int) (*[]models.UserQuizPaper, error) {
func (s *userQuizPaperService) List(q string, page int, limit int) (*[]models.UserQuizPaperResponse, error) {
return s.repository.List(q, page, limit)
}
@ -37,10 +38,14 @@ func (s *userQuizPaperService) Total(q string) (int64, error) {
return s.repository.Total(q)
}
func (s *userQuizPaperService) Find(id int64) (*models.UserQuizPaper, error) {
func (s *userQuizPaperService) Find(id int64) (*models.UserQuizPaperResponse, error) {
return s.repository.Find(id)
}
func (s *userQuizPaperService) Get(id int64) (*models.UserQuizPaper, error) {
return s.repository.Get(id)
}
func (s *userQuizPaperService) Create(userQuizPaper *models.UserQuizPaper) (*models.UserQuizPaper, error) {
result, err := s.repository.Create(userQuizPaper)