Skip to content

Commit

Permalink
seach course
Browse files Browse the repository at this point in the history
  • Loading branch information
mouuii committed Sep 13, 2023
1 parent 5e2ee2e commit 6c1769c
Show file tree
Hide file tree
Showing 10 changed files with 1,055 additions and 1,695 deletions.
2,092 changes: 887 additions & 1,205 deletions api/helloworld/v1/kubecit.pb.go

Large diffs are not rendered by default.

417 changes: 65 additions & 352 deletions api/helloworld/v1/kubecit.pb.validate.go

Large diffs are not rendered by default.

45 changes: 9 additions & 36 deletions api/helloworld/v1/kubecit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ service Kubecit {
tags: "course"
};
}
rpc SearchCourse (SearchCourseRequest) returns (SearchCourseReply) {
rpc SearchCourse (SearchCourseRequest) returns (CourseSearchReply) {
option (google.api.http) = {
post: "/api/course/search",
body: "*"
Expand Down Expand Up @@ -284,12 +284,6 @@ message HelloReply {
}



message PageRequest {
int32 pageNum = 1;
int32 pageSize = 2;
}

message Metadata {
string msg = 1;
string code = 2;
Expand All @@ -303,30 +297,11 @@ message MostNewReply {
}

message MostNewReplyData {
PageInfo pageInfo = 1;
}

message PageInfo {
int32 startRow = 1;
repeated int32 navigatepageNums = 2;
int32 lastPage = 3;
int32 prePage = 4;
bool hasNextPage = 5;
int32 nextPage = 6;
int32 pageSize = 7;
int32 endRow = 8;
repeated CourseInfo list = 9;
int32 pageNum = 10;
int32 navigatePages = 11;
int32 total = 12;
int32 navigateFirstPage = 13;
int32 pages = 14;
int32 size = 15;
int32 firstPage = 16;
bool isLastPage = 17;
bool hasPreviousPage = 18;
int32 navigateLastPage = 19;
bool isFirstPage = 20;
repeated CourseInfo list = 1;
}

message CourseSearchReply {
repeated CourseInfo list = 1;
}

enum CourseStatus {
Expand Down Expand Up @@ -369,13 +344,11 @@ message Tag {
message SearchCourseRequest {
int32 pageNum = 1;
int32 pageSize = 2;
int32 category = 3;
int32 firstCategory = 3;
int32 secondCategory = 4;
int32 level = 5;
}

message SearchCourseReply {
Metadata meta = 1;
PageInfo data = 2;
}

message UpdateCourseRequest {
int32 id = 1;
Expand Down
10 changes: 5 additions & 5 deletions api/helloworld/v1/kubecit_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions api/helloworld/v1/kubecit_http.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 30 additions & 4 deletions internal/biz/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ type CategoryRepo interface {
ListAll(ctx context.Context) ([]*Category, error)
ListByLevel(ctx context.Context, level int32) ([]*Category, error)
Create(context.Context, *Category) error
ListSubCategories(ctx context.Context, parentID int32) ([]*Category, error)
Delete(ctx context.Context, id int32) error
Update(ctx context.Context, id int, name string) error
}

// CourseRepo is a Course repo.
type CourseRepo interface {
SearchCourse(ctx context.Context, pageNum, pageSize int, categoryId *int32, level *int32, reverse *bool) ([]*Course, error)
SearchCourse(ctx context.Context, pageNum, pageSize int, categoryIds []int, level int32, reverse *bool) ([]*Course, error)
UpdateCourse(ctx context.Context, id int, course *Course) (*Course, error)
ReviewCourse(ctx context.Context, id int, status int32) (*Course, error)
CreateCourse(ctx context.Context, course *Course) (*Course, error)
Expand All @@ -52,7 +53,8 @@ type CourseRepo interface {
type CourseUsecase struct {
repo CategoryRepo
courseRepo CourseRepo
log *log.Helper

log *log.Helper
}

// NewCourseUsecase new a Category usecase.
Expand Down Expand Up @@ -80,8 +82,32 @@ func (uc *CourseUsecase) UpdateCategory(ctx context.Context, id int, name string
return uc.repo.Update(ctx, id, name)
}

func (uc *CourseUsecase) SearchCourse(ctx context.Context, pageNum, pageSize int, categoryId *int32, level *int32, reverse *bool) ([]*Course, error) {
return uc.courseRepo.SearchCourse(ctx, pageNum, pageSize, categoryId, level, reverse)
type SearchFilterParam struct {
SecondCategoryId int32
FirstCategoryId int32
Level int32
Reverse bool
}

func (uc *CourseUsecase) SearchCourse(ctx context.Context, pageNum, pageSize int, filter *SearchFilterParam) ([]*Course, error) {

var categoryIds []int
if filter.SecondCategoryId == 0 {
if filter.FirstCategoryId != 0 {
subCategories, err := uc.repo.ListSubCategories(ctx, filter.FirstCategoryId)
if err != nil {
return nil, err
}
for _, v := range subCategories {
categoryIds = append(categoryIds, int(v.Id))
}
}

} else {
categoryIds = append(categoryIds, int(filter.SecondCategoryId))
}

return uc.courseRepo.SearchCourse(ctx, pageNum, pageSize, categoryIds, filter.Level, &filter.Reverse)
}

func (uc *CourseUsecase) UpdateCourse(ctx context.Context, id int, course *Course) (*Course, error) {
Expand Down
23 changes: 23 additions & 0 deletions internal/data/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@ func (c *categoryRepo) ListByLevel(ctx context.Context, level int32) ([]*biz.Cat
return categoryResult, nil

}

func (c *categoryRepo) ListSubCategories(ctx context.Context, parentID int32) ([]*biz.Category, error) {

ca, err := c.data.db.Category.Query().Where(category.ID(int(parentID))).First(ctx)
if err != nil {
return nil, err
}
categories, err := c.data.db.Category.QueryChildren(ca).All(context.Background())
if err != nil {
return nil, err
}
var categoryResult []*biz.Category

for _, v := range categories {
categoryResult = append(categoryResult, &biz.Category{
CategoryName: v.Name,
Id: int32(v.ID),
ParentId: int32(v.ParentID),
Level: v.Level,
})
}
return categoryResult, nil
}
11 changes: 6 additions & 5 deletions internal/data/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func NewCourseRepo(data *Data, logger log.Logger) biz.CourseRepo {
}
}

func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize int, categoryId *int32, level *int32, reverse *bool) ([]*biz.Course, error) {
func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize int, categories []int, level int32, reverse *bool) ([]*biz.Course, error) {
cq := c.data.db.Course.Query()
if categoryId != nil {
cq.Where(course.CategoryIDEQ(int(*categoryId)))
if len(categories) != 0 {
cq.Where(course.CategoryIDIn(categories...))
}
if level != nil {
cq.Where(course.LevelEQ(*level))

if level != 0 {
cq.Where(course.LevelEQ(level))
}
if reverse != nil {
if !*reverse {
Expand Down
22 changes: 10 additions & 12 deletions internal/service/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// MostNew 最新好课
func (s *KubecitService) MostNew(ctx context.Context, req *pb.Empty) (*pb.MostNewReply, error) {

courses, err := s.cc.SearchCourse(ctx, 0, 20, nil, nil, nil)
courses, err := s.cc.SearchCourse(ctx, 0, 20, &biz.SearchFilterParam{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -48,16 +48,16 @@ func (s *KubecitService) TagsList(ctx context.Context, req *pb.TagsListRequest)
}

// SearchCourse 搜索课程
func (s *KubecitService) SearchCourse(ctx context.Context, req *pb.SearchCourseRequest) (*pb.SearchCourseReply, error) {
func (s *KubecitService) SearchCourse(ctx context.Context, req *pb.SearchCourseRequest) (*pb.CourseSearchReply, error) {
pageNum := req.GetPageNum()
pageSize := req.GetPageSize()

category := req.GetCategory()
categoryID := &category
var level *int32
var reverse *bool

courses, err := s.cc.SearchCourse(ctx, int(pageNum), int(pageSize), categoryID, level, reverse)
courses, err := s.cc.SearchCourse(ctx, int(pageNum), int(pageSize), &biz.SearchFilterParam{
SecondCategoryId: req.GetSecondCategory(),
FirstCategoryId: req.GetFirstCategory(),
Level: req.Level,
Reverse: false,
})
if err != nil {
return nil, err
}
Expand All @@ -77,10 +77,8 @@ func (s *KubecitService) SearchCourse(ctx context.Context, req *pb.SearchCourseR
CategoryId: int32(course.CategoryId),
})
}
return &pb.SearchCourseReply{
Data: &pb.PageInfo{
List: list,
},
return &pb.CourseSearchReply{
List: list,
}, nil
}

Expand Down
Loading

0 comments on commit 6c1769c

Please sign in to comment.