Skip to content

Commit

Permalink
feat: query teacher
Browse files Browse the repository at this point in the history
  • Loading branch information
dujiajun committed Jul 1, 2024
1 parent 2b9dd83 commit 352cf0b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
19 changes: 19 additions & 0 deletions model/converter/teacher.go
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,
}
}
40 changes: 40 additions & 0 deletions repository/course.go
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
}
112 changes: 112 additions & 0 deletions repository/teacher.go
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)
}
}
1 change: 1 addition & 0 deletions repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DBOption func(*gorm.DB) *gorm.DB

type IUserQuery interface {
GetUserDetail(ctx context.Context, opts ...DBOption) (*domain.User, error)
GetUserList(ctx context.Context, opts ...DBOption) ([]domain.User, error)
WithID(id uint) DBOption
WithEmail(email string) DBOption
WithPassword(password string) DBOption
Expand Down

0 comments on commit 352cf0b

Please sign in to comment.