Skip to content

Commit

Permalink
fix: fix issues mentioned by second code review
Browse files Browse the repository at this point in the history
- python script optimization
- refactor some structure
- add more tests
  • Loading branch information
Ayanami1314 committed Aug 14, 2024
1 parent 778d27c commit d24d52c
Show file tree
Hide file tree
Showing 25 changed files with 207,926 additions and 192 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions dal/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/DATA-DOG/go-sqlmock"
_ "github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"jcourse_go/util"
Expand Down Expand Up @@ -60,3 +61,11 @@ func InitMockDBClient() (sqlmock.Sqlmock, error) {
}
return mock, nil
}

func InitTestMemDBClient() {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic(err)
}
dbClient = db
}
7,394 changes: 7,394 additions & 0 deletions data/teachers.json

Large diffs are not rendered by default.

200,191 changes: 200,191 additions & 0 deletions data/trainingPlan.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/stretchr/testify v1.9.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.10
)

Expand Down Expand Up @@ -53,6 +54,7 @@ require (
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -229,6 +231,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
Expand Down
2 changes: 1 addition & 1 deletion handler/teacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func GetTeacherDetailHandler(c *gin.Context) {

func SearchTeacherListHandler(c *gin.Context) {
var request dto.TeacherListRequest
if err := c.ShouldBind(&request); err != nil {
if err := c.ShouldBindQuery(&request); err != nil {
c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"})
return
}
Expand Down
2 changes: 1 addition & 1 deletion handler/teacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSearchTeacherListHandler(t *testing.T) {
"invalid-value-req": {"name=你谁", http.StatusOK},
"invalid-page-req": {"page=-1&page_size=10086", http.StatusOK},
"overflow-page-req": {"page=10086&page_size=1", http.StatusOK},
"invalid-key-req": {"hello=1", http.StatusBadRequest},
"invalid-key-req": {"hello=1", http.StatusOK}, // no validation now, will return empty list
}

for name, s := range querys {
Expand Down
1 change: 0 additions & 1 deletion handler/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func SearchTrainingPlanHandler(c *gin.Context) {
c.JSON(http.StatusBadRequest, dto.BaseResponse{Message: "参数错误"})
return
}

filter := domain.TrainingPlanFilter{
Major: request.MajorName,
EntryYear: fmt.Sprintf("%d", request.EntryYear),
Expand Down
2 changes: 1 addition & 1 deletion handler/trainingplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func prettyJsonLog(w *httptest.ResponseRecorder) {
json.Indent(&prettyJSON, data, "", " ")

// 打印格式化的 JSON 字符串
prettyJSON.WriteTo(os.Stdout)
fmt.Println(string(prettyJSON.Bytes()))
}
func baseConfig() (*httptest.ResponseRecorder, *gin.Engine) {
gin.SetMode(gin.TestMode)
Expand Down
34 changes: 1 addition & 33 deletions model/converter/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func ConvertBaseCourseDomainToPO(course domain.BaseCourse) po.BaseCoursePO {

func ConvertBaseCoursePOToDomain(course po.BaseCoursePO) domain.BaseCourse {
return domain.BaseCourse{
ID: int64(course.ID),
Code: course.Code,
Name: course.Name,
Credit: course.Credit,
Expand Down Expand Up @@ -123,7 +124,6 @@ func ConvertOfferedCoursePOToDomain(offeredCourse po.OfferedCoursePO) domain.Off
}
}

// FIXME:endless recursion?
func ConvertOfferedCourseDomainToDTO(offeredCourse domain.OfferedCourse) dto.OfferedCourseDTO {
offeredCourseDTO := dto.OfferedCourseDTO{
ID: offeredCourse.ID,
Expand Down Expand Up @@ -155,35 +155,3 @@ func ConvertCourseDomainToDetailDTO(course domain.Course) dto.CourseDetailDTO {
}
return courseDetailDTO
}

func PackTrainingPlanWithCourses(trainingPlan *domain.TrainingPlan, courses []domain.BaseCourse) {
if courses == nil {
return
}
if len(courses) == 0 {
courses = make([]domain.BaseCourse, 0)
}
trainingPlan.Courses = courses
}
func PackTrainingPlanDetailWithCourses(trainingPlan *domain.TrainingPlanDetail, courses []domain.TrainingPlanCourse) {
if courses == nil {
return
}
if len(courses) == 0 {
courses = make([]domain.TrainingPlanCourse, 0)
}
trainingPlan.Courses = courses
}

func ConvertTrainingPlanPOToDomain(trainingPlan po.TrainingPlanPO) domain.TrainingPlanDetail {
return domain.TrainingPlanDetail{
ID: int64(trainingPlan.ID),
Major: trainingPlan.Major,
Department: trainingPlan.Department,
EntryYear: trainingPlan.EntryYear,
MajorCode: trainingPlan.MajorCode,
MajorClass: trainingPlan.MajorClass,
MinCredits: trainingPlan.MinCredits,
TotalYear: int(trainingPlan.TotalYear),
}
}
38 changes: 19 additions & 19 deletions model/converter/teacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func ConvertTeacherPOToDomain(teacher *po.TeacherPO) *domain.Teacher {
}

return &domain.Teacher{
ID: int64(teacher.ID),
Name: teacher.Name,
Email: teacher.Email,
Code: teacher.Code,
Department: teacher.Department,
Title: teacher.Title,
Picture: teacher.Picture,
ProfileURL: teacher.ProfileURL,
ProfileDesc: teacher.Biography,
ID: int64(teacher.ID),
Name: teacher.Name,
Email: teacher.Email,
Code: teacher.Code,
Department: teacher.Department,
Title: teacher.Title,
Picture: teacher.Picture,
ProfileURL: teacher.ProfileURL,
Biography: teacher.Biography,
}
}

Expand All @@ -38,16 +38,16 @@ func PackTeacherWithCourses(teacher *domain.Teacher, courses []po.OfferedCourseP

func ConvertTeacherDomainToDTO(teacher domain.Teacher) dto.TeacherDTO {
teacherDTO := dto.TeacherDTO{
ID: teacher.ID,
Email: teacher.Email,
Code: teacher.Code,
Name: teacher.Name,
Department: teacher.Department,
Title: teacher.Title,
Picture: teacher.Picture,
ProfileURL: teacher.ProfileURL,
ProfileDesc: teacher.ProfileDesc,
Courses: make([]dto.OfferedCourseDTO, 0),
ID: teacher.ID,
Email: teacher.Email,
Code: teacher.Code,
Name: teacher.Name,
Department: teacher.Department,
Title: teacher.Title,
Picture: teacher.Picture,
ProfileURL: teacher.ProfileURL,
Biography: teacher.Biography,
Courses: make([]dto.OfferedCourseDTO, 0),
}
for _, offeredCourse := range teacher.Courses {
offeredCourseDTO := ConvertOfferedCourseDomainToDTO(offeredCourse)
Expand Down
32 changes: 32 additions & 0 deletions model/converter/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,35 @@ func ConvertTrainingPlanCoursePOToDomain(coursePO po.TrainingPlanCoursePO, baseC
Department: coursePO.Department,
}
}

func PackTrainingPlanWithCourses(trainingPlan *domain.TrainingPlan, courses []domain.BaseCourse) {
if courses == nil {
return
}
if len(courses) == 0 {
courses = make([]domain.BaseCourse, 0)
}
trainingPlan.Courses = courses
}
func PackTrainingPlanDetailWithCourses(trainingPlan *domain.TrainingPlanDetail, courses []domain.TrainingPlanCourse) {
if courses == nil {
return
}
if len(courses) == 0 {
courses = make([]domain.TrainingPlanCourse, 0)
}
trainingPlan.Courses = courses
}

func ConvertTrainingPlanPOToDomain(trainingPlan po.TrainingPlanPO) domain.TrainingPlanDetail {
return domain.TrainingPlanDetail{
ID: int64(trainingPlan.ID),
Major: trainingPlan.Major,
Department: trainingPlan.Department,
EntryYear: trainingPlan.EntryYear,
MajorCode: trainingPlan.MajorCode,
MajorClass: trainingPlan.MajorClass,
MinCredits: trainingPlan.MinCredits,
TotalYear: trainingPlan.TotalYear,
}
}
2 changes: 1 addition & 1 deletion model/domain/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Review struct {
Course Course
User User
Comment string
Rating int64
Rate int64
Semester string
IsAnonymous bool
CreatedAt time.Time
Expand Down
20 changes: 10 additions & 10 deletions model/domain/teacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package domain

// FIXME: Domain层的Teacher结构体中暂时还没有存Review相关的信息
type Teacher struct {
ID int64
Name string
Email string
Code string
Department string
Title string
Picture string
ProfileURL string
ProfileDesc string
Courses []OfferedCourse
ID int64
Name string
Email string
Code string
Department string
Title string
Picture string
ProfileURL string
Biography string
Courses []OfferedCourse
}

type TeacherListFilter struct {
Expand Down
31 changes: 10 additions & 21 deletions model/dto/teacher.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dto

type TeacherDTO struct {
ID int64 `json:"id"`
Email string `json:"email"`
Code string `json:"code"`
Name string `json:"name"`
Department string `json:"department"`
Title string `json:"title"`
Picture string `json:"picture"`
ProfileURL string `json:"profileURL"`
ProfileDesc string `json:"profileDesc"`
Courses []OfferedCourseDTO `json:"courses"`
ID int64 `json:"id"`
Email string `json:"email"`
Code string `json:"code"`
Name string `json:"name"`
Department string `json:"department"`
Title string `json:"title"`
Picture string `json:"picture"`
ProfileURL string `json:"profile_url"`
Biography string `json:"biography"`
Courses []OfferedCourseDTO `json:"courses"`
}

type TeacherDetailRequest struct {
Expand All @@ -28,15 +28,4 @@ type TeacherListRequest struct {
PinyinAbbr string `json:"pinyin_abbr" form:"pinyin_abbr"`
}

type TeacherDetailResponse = TeacherDTO

type TeacherQueryRequest struct {
Text string `json:"text"`
Department string `json:"department"`
MajorName string `json:"major_name"`
MajorCode string `json:"major_code"`
SortDirection string `json:"sort_direction"`
SortBy string `json:"sort_by"`
}

type TeacherListResponse = BasePaginateResponse[TeacherDTO]
20 changes: 10 additions & 10 deletions model/dto/trainingplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ type TrainingPlanDetailRequest struct {
TrainingPlanID int64 `uri:"trainingPlanID" binding:"required"`
}
type TrainingPlanListQueryRequest struct {
EntryYear int64 `form:"entry_year"`
Department string `form:"department"`
MajorName string `form:"major_name"`
MajorCode string `form:"major_code"`
SortDirection string `form:"sort_direction"`
SortBy string `form:"sort_by"`
Page int `form:"page" binding:"required"`
PageSize int `form:"page_size" binding:"required"`
EntryYear int64 `json:"entry_year" form:"entry_year"`
Department string `json:"department" form:"department"`
MajorName string `json:"major_name" form:"major_name"`
MajorCode string `json:"major_code" form:"major_code"`
SortDirection string `json:"sort_direction" form:"sort_direction"`
SortBy string `json:"sort_by" form:"sort_by"`
Page int `json:"page" binding:"required" form:"page"`
PageSize int `json:"page_size" binding:"required" form:"page_size"`
}
type TrainingPlanListRequest struct {
Page int `form:"page" binding:"required"`
PageSize int `form:"page_size" binding:"required"`
Page int `json:"page" binding:"required" form:"page"`
PageSize int `json:"page_size" binding:"required" form:"page_size"`
}
30 changes: 0 additions & 30 deletions model/po/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,6 @@ func (po *OfferedCourseTeacherPO) TableName() string {
return "offered_courses_teachers"
}

type TrainingPlanPO struct {
gorm.Model
// 联合唯一索引
Degree string `gorm:"index;index:uniq_training_plan,unique"`
Major string `gorm:"index;index:uniq_training_plan,unique"`
Department string `gorm:"index;index:uniq_training_plan,unique"`
EntryYear string `gorm:"index;index:uniq_training_plan,unique"` //==Grade,年级
MajorCode string `gorm:"index;index:uniq_training_plan,unique"`
TotalYear int `gorm:"index;index:uniq_training_plan,unique"`
MinCredits float64 `gorm:"index;index:uniq_training_plan,unique"`
MajorClass string `gorm:"index;index:uniq_training_plan,unique"` // 专业类
}

func (po *TrainingPlanPO) TableName() string {
return "training_plans"
}

type TrainingPlanCoursePO struct {
gorm.Model
CourseID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
TrainingPlanID int64 `gorm:"index;index:uniq_training_plan_course,unique"`
// SuggestSemester:学年+学期,如 2023-2024-2
SuggestSemester string `gorm:"index;index:uniq_training_plan_course,unique"`
Department string `gorm:"index;"`
}

func (po *TrainingPlanCoursePO) TableName() string {
return "training_plan_courses"
}

type CourseReviewInfo struct {
CourseID int64
Average float64
Expand Down
Loading

0 comments on commit d24d52c

Please sign in to comment.