-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package converter | ||
|
||
import ( | ||
"jcourse_go/model/domain" | ||
"jcourse_go/model/po" | ||
) | ||
|
||
func ConvertTeacherPOToDomain(teacher *po.TeacherPO) *domain.Teacher { | ||
if teacher == nil { | ||
return nil | ||
} | ||
return &domain.Teacher{ | ||
ID: int64(teacher.ID), | ||
Code: teacher.Code, | ||
Name: teacher.Name, | ||
Department: teacher.Department, | ||
Title: teacher.Title, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
|
||
"jcourse_go/model/domain" | ||
) | ||
|
||
type IBaseCourseQuery interface { | ||
GetBaseCourse(ctx context.Context, opts ...DBOption) (*domain.BaseCourse, error) | ||
GetBaseCourseList(ctx context.Context, opts ...DBOption) ([]domain.BaseCourse, error) | ||
WithCode(code string) DBOption | ||
WithName(name string) DBOption | ||
WithCredit(credit float64) DBOption | ||
} | ||
|
||
type ICourseQuery interface { | ||
GetCourse(ctx context.Context, opts ...DBOption) (*domain.Course, error) | ||
GetCourseList(ctx context.Context, opts ...DBOption) ([]domain.Course, error) | ||
WithID(id int64) DBOption | ||
WithBaseCourseID(id int64) DBOption | ||
WithCode(code string) DBOption | ||
WithName(name string) DBOption | ||
WithCredit(credit float64) DBOption | ||
WithMainTeacherName(name string) DBOption | ||
WithMainTeacherID(id int64) DBOption | ||
} | ||
|
||
type IOfferedCourseQuery interface { | ||
GetOfferedCourse(ctx context.Context, opts ...DBOption) (*domain.OfferedCourse, error) | ||
GetOfferedCourseList(ctx context.Context, opts ...DBOption) ([]domain.OfferedCourse, error) | ||
WithID(id int64) DBOption | ||
WithCourseID(id int64) DBOption | ||
WithMainTeacherName(name string) DBOption | ||
WithMainTeacherID(id int64) DBOption | ||
WithSemester(semester string) DBOption | ||
WithDepartment(department string) DBOption | ||
WithCategory(category string) DBOption | ||
WithLanguage(language string) DBOption | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"gorm.io/gorm" | ||
"jcourse_go/dal" | ||
"jcourse_go/model/converter" | ||
"jcourse_go/model/domain" | ||
"jcourse_go/model/po" | ||
) | ||
|
||
type ITeacherQuery interface { | ||
GetTeacher(ctx context.Context, opts ...DBOption) (*domain.Teacher, error) | ||
GetTeacherList(ctx context.Context, opts ...DBOption) ([]domain.Teacher, error) | ||
WithID(id int64) DBOption | ||
WithCode(code string) DBOption | ||
WithName(name string) DBOption | ||
WithPinyin(pinyin string) DBOption | ||
WithPinyinAbbr(pinyin string) DBOption | ||
WithDepartment(department string) DBOption | ||
WithTitle(title string) DBOption | ||
} | ||
|
||
type TeacherQuery struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewTeacherQuery() ITeacherQuery { | ||
return &TeacherQuery{ | ||
db: dal.GetDBClient(), | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) optionDB(ctx context.Context, opts ...DBOption) *gorm.DB { | ||
db := q.db.WithContext(ctx).Model(po.TeacherPO{}) | ||
for _, opt := range opts { | ||
db = opt(db) | ||
} | ||
return db | ||
} | ||
|
||
func (q *TeacherQuery) GetTeacher(ctx context.Context, opts ...DBOption) (*domain.Teacher, error) { | ||
db := q.optionDB(ctx, opts...) | ||
teacher := po.TeacherPO{} | ||
result := db.Debug().First(&teacher) | ||
if errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return converter.ConvertTeacherPOToDomain(&teacher), nil | ||
} | ||
|
||
func (q *TeacherQuery) GetTeacherList(ctx context.Context, opts ...DBOption) ([]domain.Teacher, error) { | ||
db := q.optionDB(ctx, opts...) | ||
teacherPOs := make([]po.TeacherPO, 0) | ||
teachers := make([]domain.Teacher, 0) | ||
result := db.Debug().Find(&teacherPOs) | ||
if errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
for _, teacherPO := range teacherPOs { | ||
teacher := converter.ConvertTeacherPOToDomain(&teacherPO) | ||
teachers = append(teachers, *teacher) | ||
} | ||
return teachers, nil | ||
} | ||
|
||
func (q *TeacherQuery) WithID(id int64) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("id = ?", id) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithCode(code string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("code = ?", code) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithName(name string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("name = ?", name) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithPinyin(pinyin string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("pinyin = ?", pinyin) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithPinyinAbbr(pinyin string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("pinyin_abbr = ?", pinyin) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithDepartment(department string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("department = ?", department) | ||
} | ||
} | ||
|
||
func (q *TeacherQuery) WithTitle(title string) DBOption { | ||
return func(db *gorm.DB) *gorm.DB { | ||
return db.Where("title = ?", title) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters