Skip to content

Commit

Permalink
feat: GetCourseGroupV3 api and search course_group by code
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Oct 3, 2023
1 parent d319e91 commit ae27ab2
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 12 deletions.
50 changes: 50 additions & 0 deletions internal/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,56 @@ const docTemplate = `{
}
}
}
},
"/v3/course_groups/{id}": {
"get": {
"description": "get a course group, v3 version",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CourseGroup"
],
"summary": "/v3/course_groups/{group_id}",
"parameters": [
{
"type": "string",
"description": "course group id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/schema.CourseGroupV3Response"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/schema.HttpError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/schema.HttpBaseError"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/schema.HttpBaseError"
}
}
}
}
}
},
"definitions": {
Expand Down
64 changes: 57 additions & 7 deletions internal/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,56 @@
}
}
}
},
"/v3/course_groups/{id}": {
"get": {
"description": "get a course group, v3 version",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"CourseGroup"
],
"summary": "/v3/course_groups/{group_id}",
"parameters": [
{
"type": "string",
"description": "course group id",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/schema.CourseGroupV3Response"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/schema.HttpError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/schema.HttpBaseError"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/schema.HttpBaseError"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -1007,10 +1057,10 @@
"description": "课程组编号",
"type": "string"
},
"course_count": {
"description": "课程数量",
"type": "integer"
},
"course_count": {
"description": "课程数量",
"type": "integer"
},
"course_list": {
"description": "课程组下的课程,slices 必须非空",
"type": "array",
Expand All @@ -1037,9 +1087,9 @@
"description": "课程组名称",
"type": "string"
},
"review_count": {
"description": "评价数量",
"type": "integer"
"review_count": {
"description": "评价数量",
"type": "integer"
}
}
},
Expand Down
33 changes: 33 additions & 0 deletions internal/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,39 @@ paths:
summary: get random review
tags:
- Review
/v3/course_groups/{id}:
get:
consumes:
- application/json
description: get a course group, v3 version
parameters:
- description: course group id
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/schema.CourseGroupV3Response'
"400":
description: Bad Request
schema:
$ref: '#/definitions/schema.HttpError'
"404":
description: Not Found
schema:
$ref: '#/definitions/schema.HttpBaseError'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/schema.HttpBaseError'
summary: /v3/course_groups/{group_id}
tags:
- CourseGroup
/v3/course_groups/search:
get:
consumes:
Expand Down
34 changes: 34 additions & 0 deletions internal/handler/course_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (h *courseGroupHandler) RegisterRoute(router fiber.Router) {

// v3
router.Get("/v3/course_groups/search", h.SearchCourseGroupV3)
router.Get("/v3/course_groups/:id<int>", h.GetCourseGroupV3)

// static
router.Get("/static/cedict_ts.u8", func(c *fiber.Ctx) error {
Expand Down Expand Up @@ -169,3 +170,36 @@ func (h *courseGroupHandler) SearchCourseGroupV3(c *fiber.Ctx) (err error) {

return c.JSON(response)
}

// GetCourseGroupV3 godoc
// @Summary /v3/course_groups/{group_id}
// @Description get a course group, v3 version
// @Tags CourseGroup
// @Accept json
// @Produce json
// @Router /v3/course_groups/{id} [get]
// @Param id path string true "course group id"
// @Success 200 {object} schema.CourseGroupV3Response
// @Failure 400 {object} schema.HttpError
// @Failure 404 {object} schema.HttpBaseError
// @Failure 500 {object} schema.HttpBaseError
func (h *courseGroupHandler) GetCourseGroupV3(c *fiber.Ctx) (err error) {
c.Context().SetUserValue("FiberCtx", c)

user, err := h.accountRepository.GetCurrentUser(c.Context())
if err != nil {
return
}

groupID, err := c.ParamsInt("id")
if err != nil {
return
}

response, err := h.courseGroupService.GetGroupByIDV3(c.Context(), user, groupID)
if err != nil {
return
}

return c.JSON(response)
}
5 changes: 4 additions & 1 deletion internal/model/course_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"regexp"
"time"
)

Expand All @@ -10,11 +11,13 @@ type CourseGroup struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name" gorm:"not null"` // 课程组名称
Code string `json:"code" gorm:"not null"` // 课程组编号
Code string `json:"code" gorm:"not null;index:,length:6"` // 课程组编号
Credits []float64 `json:"credits" gorm:"serializer:json"` // 学分
Department string `json:"department" gorm:"not null"` // 开课学院
CampusName string `json:"campus_name" gorm:"not null"` // 开课校区
CourseCount int `json:"course_count" gorm:"not null;default:0"` // 课程数量
ReviewCount int `json:"review_count" gorm:"not null;default:0"` // 评价数量
Courses []*Course `json:"courses"`
}

var CourseCodeRegexp = regexp.MustCompile(`^([A-Z]{3,})([0-9]{2,})`)
6 changes: 5 additions & 1 deletion internal/repository/course_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func NewCourseGroupRepository(repository Repository) CourseGroupRepository {

func (r *courseGroupRepository) FindGroups(ctx context.Context, condition func(db *gorm.DB) *gorm.DB) (groups []*model.CourseGroup, err error) {
groups = make([]*model.CourseGroup, 5)
err = condition(r.GetDB(ctx)).Find(&groups).Error
db := r.GetDB(ctx)
if condition != nil {
db = condition(db)
}
err = db.Find(&groups).Error
return
}

Expand Down
Loading

0 comments on commit ae27ab2

Please sign in to comment.