Skip to content

Commit

Permalink
feat: get course list
Browse files Browse the repository at this point in the history
  • Loading branch information
dujiajun committed Jul 6, 2024
1 parent 4b92bea commit a6c2d13
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 49 deletions.
3 changes: 3 additions & 0 deletions constant/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constant

const DefaultPageSize int64 = 20
15 changes: 14 additions & 1 deletion dal/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package dal

import (
"fmt"
"log"
"os"

"github.com/DATA-DOG/go-sqlmock"
_ "github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"jcourse_go/util"
)

var dbClient *gorm.DB
Expand Down Expand Up @@ -41,7 +44,17 @@ func InitMockDBClient() (sqlmock.Sqlmock, error) {
if err != nil {
return nil, err
}
dbClient, err = gorm.Open(postgres.New(postgres.Config{Conn: db}), &gorm.Config{})
config := &gorm.Config{}
if util.IsDebug() {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
LogLevel: logger.Silent, // Log level
},
)
config.Logger = newLogger
}
dbClient, err = gorm.Open(postgres.New(postgres.Config{Conn: db}), config)
if err != nil {
return nil, err
}
Expand Down
29 changes: 27 additions & 2 deletions handler/course.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package handler

import "github.com/gin-gonic/gin"
import (
"net/http"

"github.com/gin-gonic/gin"
"jcourse_go/model/converter"
"jcourse_go/model/dto"
"jcourse_go/service"
)

func GetCourseDetailHandler(c *gin.Context) {

}

func GetCourseListHandler(c *gin.Context) {

var request dto.CourseListRequest
if err := c.ShouldBind(&request); err != nil {
c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"})
return
}
courses, err := service.GetCourseList(c, request)
if err != nil {
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
return
}
total, err := service.GetCourseCount(c)

resp := dto.CourseListResponse{
Total: total,
Data: converter.ConvertCourseListDomainToDTO(courses),
Page: request.Page,
PageSize: int64(len(courses)),
}
c.JSON(http.StatusOK, resp)
}

func GetSuggestedCourseHandler(c *gin.Context) {
Expand Down
40 changes: 35 additions & 5 deletions model/converter/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package converter

import (
"jcourse_go/model/domain"
"jcourse_go/model/dto"
"jcourse_go/model/po"
)

Expand All @@ -28,16 +29,45 @@ func ConvertBaseCoursePOToDomain(course *po.BaseCoursePO) *domain.BaseCourse {
}
}

func ConvertCoursePOToDomain(course *po.CoursePO) *domain.Course {
func ConvertCoursePOToDomain(course *po.CoursePO, categories []string) *domain.Course {
if course == nil {
return nil
}
return &domain.Course{
ID: int64(course.ID),
Code: course.Code,
Name: course.Name,
Credit: course.Credit,
ID: int64(course.ID),
Code: course.Code,
Name: course.Name,
Credit: course.Credit,
Department: course.Department,
MainTeacher: domain.Teacher{Name: course.MainTeacherName, ID: course.MainTeacherID},
Categories: categories,
}
}

func ConvertCourseDomainToListDTO(course *domain.Course) *dto.CourseListDTO {
if course == nil {
return nil
}
return &dto.CourseListDTO{
ID: course.ID,
Code: course.Code,
Name: course.Name,
Credit: course.Credit,
MainTeacherName: course.MainTeacher.Name,
Categories: course.Categories,
Department: course.Department,
}
}

func ConvertCourseListDomainToDTO(courses []domain.Course) []dto.CourseListDTO {
result := make([]dto.CourseListDTO, len(courses))
if len(courses) == 0 {
return result
}
for _, course := range courses {
result = append(result, *ConvertCourseDomainToListDTO(&course))
}
return result
}

func ConvertOfferedCoursePOToDomain(offeredCourse *po.OfferedCoursePO) *domain.OfferedCourse {
Expand Down
50 changes: 16 additions & 34 deletions model/domain/course.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package domain

type CourseLike interface {
GetCode() string
GetName() string
GetCredit() float32
}

type OfferedCourse struct {
ID int64
Course
MainTeacher Teacher
ID int64
Course *Course
TeacherGroup []Teacher
Semester string
Language string
Grade []string
}

type Course struct {
ID int64
Code string
Name string
Credit float64
MainTeacher Teacher
Department string
Categories []string
ID int64
Code string
Name string
Credit float64
MainTeacher Teacher
Department string
Categories []string
OfferedCourses []OfferedCourse
}

type TrainingPlan struct {
Expand All @@ -41,22 +35,10 @@ type BaseCourse struct {
Credit float64
}

func (c *BaseCourse) GetCode() string {
return c.Code
}

func (c *BaseCourse) GetName() string {
return c.Name
}

func (c *BaseCourse) GetCredit() float64 {
return c.Credit
}

type Teacher struct {
ID int64
Name string
Code string
Department string
Title string
type CourseListFilter struct {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
Departments []string `json:"departments"`
Categories []string `json:"categories"`
Credits []float64 `json:"credits"`
}
9 changes: 9 additions & 0 deletions model/domain/teacher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain

type Teacher struct {
ID int64
Name string
Code string
Department string
Title string
}
7 changes: 7 additions & 0 deletions model/dto/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ package dto
type BaseResponse struct {
Message string `json:"message"`
}

type BasePaginateResponse[T any] struct {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
Total int64 `json:"total"`
Data []T `json:"data"`
}
17 changes: 17 additions & 0 deletions model/dto/course.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dto

import "jcourse_go/model/domain"

type CourseListDTO struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
Credit float64 `json:"credit"`
MainTeacherName string `json:"main_teacher_name"`
Categories []string `json:"categories"`
Department string `json:"department"`
}

type CourseListRequest = domain.CourseListFilter

type CourseListResponse = BasePaginateResponse[CourseListDTO]
39 changes: 35 additions & 4 deletions repository/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func NewBaseCourseQuery() IBaseCourseQuery {
type ICourseQuery interface {
GetCourse(ctx context.Context, opts ...DBOption) (*po.CoursePO, error)
GetCourseList(ctx context.Context, opts ...DBOption) ([]po.CoursePO, error)
GetCourseCount(ctx context.Context, opts ...DBOption) (int64, error)
GetCourseCategories(ctx context.Context, courseIDs []int64) (map[int64][]string, error)
WithID(id int64) DBOption
WithCode(code string) DBOption
WithName(name string) DBOption
Expand Down Expand Up @@ -113,10 +115,29 @@ func (c *CourseQuery) optionDB(ctx context.Context, opts ...DBOption) *gorm.DB {
return db
}

func (c *CourseQuery) GetCourseCategories(ctx context.Context, courseIDs []int64) (map[int64][]string, error) {
db := c.db.WithContext(ctx).Model(po.CourseCategoryPO{})
courseCategoryPOs := make([]po.CourseCategoryPO, 0)
result := db.Where("course_id in ?", courseIDs).Find(&courseCategoryPOs)
if result.Error != nil {
return nil, result.Error
}
courseCategoryMap := make(map[int64][]string)
for _, courseCategoryPO := range courseCategoryPOs {
categories, ok := courseCategoryMap[courseCategoryPO.CourseID]
if !ok {
categories = make([]string, 0)
}
categories = append(categories, courseCategoryPO.Category)
courseCategoryMap[courseCategoryPO.CourseID] = categories
}
return courseCategoryMap, nil
}

func (c *CourseQuery) GetCourse(ctx context.Context, opts ...DBOption) (*po.CoursePO, error) {
db := c.optionDB(ctx, opts...)
course := po.CoursePO{}
result := db.Debug().First(&course)
result := db.First(&course)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -129,13 +150,23 @@ func (c *CourseQuery) GetCourse(ctx context.Context, opts ...DBOption) (*po.Cour
func (c *CourseQuery) GetCourseList(ctx context.Context, opts ...DBOption) ([]po.CoursePO, error) {
db := c.optionDB(ctx, opts...)
coursePOs := make([]po.CoursePO, 0)
result := db.Debug().Find(&coursePOs)
result := db.Find(&coursePOs)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
return coursePOs, nil
}

func (c *CourseQuery) GetCourseCount(ctx context.Context, opts ...DBOption) (int64, error) {
db := c.optionDB(ctx, opts...)
var count int64
result := db.Model(&po.CoursePO{}).Count(&count)
if result.Error != nil {
return 0, result.Error
}
return count, nil
}

func (c *CourseQuery) WithID(id int64) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("id = ?", id)
Expand Down Expand Up @@ -212,7 +243,7 @@ func (o *OfferedCourseQuery) optionDB(ctx context.Context, opts ...DBOption) *go
func (o *OfferedCourseQuery) GetOfferedCourse(ctx context.Context, opts ...DBOption) (*po.OfferedCoursePO, error) {
db := o.optionDB(ctx, opts...)
course := po.OfferedCoursePO{}
result := db.Debug().First(&course)
result := db.First(&course)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -225,7 +256,7 @@ func (o *OfferedCourseQuery) GetOfferedCourse(ctx context.Context, opts ...DBOpt
func (o *OfferedCourseQuery) GetOfferedCourseList(ctx context.Context, opts ...DBOption) ([]po.OfferedCoursePO, error) {
db := o.optionDB(ctx, opts...)
coursePOs := make([]po.OfferedCoursePO, 0)
result := db.Debug().Find(&coursePOs)
result := db.Find(&coursePOs)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions repository/teacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (q *TeacherQuery) optionDB(ctx context.Context, opts ...DBOption) *gorm.DB
func (q *TeacherQuery) GetTeacher(ctx context.Context, opts ...DBOption) (*po.TeacherPO, error) {
db := q.optionDB(ctx, opts...)
teacher := po.TeacherPO{}
result := db.Debug().First(&teacher)
result := db.First(&teacher)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -55,7 +55,7 @@ func (q *TeacherQuery) GetTeacher(ctx context.Context, opts ...DBOption) (*po.Te
func (q *TeacherQuery) GetTeacherList(ctx context.Context, opts ...DBOption) ([]po.TeacherPO, error) {
db := q.optionDB(ctx, opts...)
teacherPOs := make([]po.TeacherPO, 0)
result := db.Debug().Find(&teacherPOs)
result := db.Find(&teacherPOs)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (q *UserQuery) optionDB(ctx context.Context, opts ...DBOption) *gorm.DB {
func (q *UserQuery) GetUserDetail(ctx context.Context, opts ...DBOption) (*po.UserPO, error) {
db := q.optionDB(ctx, opts...)
user := po.UserPO{}
result := db.Debug().First(&user)
result := db.First(&user)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand Down
Loading

0 comments on commit a6c2d13

Please sign in to comment.