Skip to content

Commit

Permalink
categoriies
Browse files Browse the repository at this point in the history
  • Loading branch information
mouuii committed Sep 14, 2023
1 parent f66cf09 commit 601aa6d
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 447 deletions.
900 changes: 464 additions & 436 deletions api/helloworld/v1/kubecit.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions api/helloworld/v1/kubecit.pb.validate.go

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

5 changes: 4 additions & 1 deletion api/helloworld/v1/kubecit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ message Metadata {
}

message MostNewReply {
repeated CourseInfo list = 9;
repeated CourseInfo list = 1;
int32 total = 2;
}

message MostNewReplyData {
Expand All @@ -320,6 +321,7 @@ message MostNewReplyData {

message CourseSearchReply {
repeated CourseInfo list = 1;
int32 total = 2;
}

enum CourseStatus {
Expand All @@ -341,6 +343,7 @@ message CourseInfo {
int32 categoryId = 9;
google.protobuf.Timestamp createdAt = 10;
google.protobuf.Timestamp updatedAt = 11;
int32 people = 12;
}


Expand Down
6 changes: 3 additions & 3 deletions internal/biz/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type CategoryRepo interface {

// CourseRepo is a Course repo.
type CourseRepo interface {
SearchCourse(ctx context.Context, pageNum, pageSize *int32, categoryIds []int, level *int32, order *int32) ([]*Course, error)
SearchCourse(ctx context.Context, pageNum, pageSize *int32, categoryIds []int, level *int32, order *int32) ([]*Course, int32, 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 Down Expand Up @@ -108,14 +108,14 @@ type SearchFilterParam struct {
Order *int32
}

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

var categoryIds []int
if filter.SecondCategoryId == nil {
if filter.FirstCategoryId != nil {
subCategories, err := uc.repo.ListSubCategories(ctx, *filter.FirstCategoryId)
if err != nil {
return nil, err
return nil, 0, err
}
for _, v := range subCategories {
categoryIds = append(categoryIds, int(v.Id))
Expand Down
10 changes: 7 additions & 3 deletions internal/data/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewCourseRepo(data *Data, logger log.Logger) biz.CourseRepo {
}
}

func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize *int32, categories []int, level *int32, order *int32) ([]*biz.Course, error) {
func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize *int32, categories []int, level *int32, order *int32) ([]*biz.Course, int32, error) {
cq := c.data.db.Course.Query()
if len(categories) != 0 {
cq.Where(course.CategoryIDIn(categories...))
Expand All @@ -34,6 +34,10 @@ func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize *int32,
} else {
cq.Order(ent.Desc(course.FieldCreatedAt))
}
total, err := cq.Count(ctx)
if err != nil {
return nil, 0, err
}
if pageNum != nil {
*pageNum--
cq.Offset(int(*pageNum))
Expand All @@ -49,7 +53,7 @@ func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize *int32,
result, err := cq.All(ctx)
if err != nil {
c.log.Errorf("search course errorf: %v\n", err)
return nil, err
return nil, 0, err
}
courses := make([]*biz.Course, 0, len(result))
for _, v := range result {
Expand All @@ -66,7 +70,7 @@ func (c *courseRepo) SearchCourse(ctx context.Context, pageNum, pageSize *int32,
CategoryId: v.CategoryID,
})
}
return courses, nil
return courses, int32(total), nil
}

func (c *courseRepo) GetCourse(ctx context.Context, id int) (*biz.Course, error) {
Expand Down
11 changes: 7 additions & 4 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, &biz.SearchFilterParam{})
courses, total, err := s.cc.SearchCourse(ctx, &biz.SearchFilterParam{})
if err != nil {
return nil, err
}
Expand All @@ -31,10 +31,11 @@ func (s *KubecitService) MostNew(ctx context.Context, req *pb.Empty) (*pb.MostNe
UpdatedAt: timestamppb.New(v.UpdatedAt),
Status: pb.CourseStatus(v.Status),
CategoryId: int32(v.CategoryId),
People: 102,
}
result = append(result, tmp)
}
return &pb.MostNewReply{List: result}, nil
return &pb.MostNewReply{List: result, Total: total}, nil
}

// TagsList 课程标签
Expand All @@ -50,7 +51,7 @@ func (s *KubecitService) TagsList(ctx context.Context, req *pb.TagsListRequest)
// SearchCourse 搜索课程
func (s *KubecitService) SearchCourse(ctx context.Context, req *pb.SearchCourseRequest) (*pb.CourseSearchReply, error) {

courses, err := s.cc.SearchCourse(ctx, &biz.SearchFilterParam{
courses, total, err := s.cc.SearchCourse(ctx, &biz.SearchFilterParam{
SecondCategoryId: req.SecondCategory,
FirstCategoryId: req.FirstCategory,
Level: req.Level,
Expand All @@ -75,10 +76,12 @@ func (s *KubecitService) SearchCourse(ctx context.Context, req *pb.SearchCourseR
UpdatedAt: timestamppb.New(course.UpdatedAt),
Status: pb.CourseStatus(course.Status),
CategoryId: int32(course.CategoryId),
People: 102,
})
}
return &pb.CourseSearchReply{
List: list,
List: list,
Total: total,
}, nil
}

Expand Down
9 changes: 9 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,19 @@ components:
updatedAt:
type: string
format: date-time
people:
type: integer
format: int32
helloworld.v1.CourseSearchReply:
type: object
properties:
list:
type: array
items:
$ref: '#/components/schemas/helloworld.v1.CourseInfo'
total:
type: integer
format: int32
helloworld.v1.CreateCourseReply:
type: object
properties:
Expand Down Expand Up @@ -589,6 +595,9 @@ components:
type: array
items:
$ref: '#/components/schemas/helloworld.v1.CourseInfo'
total:
type: integer
format: int32
helloworld.v1.RegisterUsernameReply:
type: object
properties:
Expand Down

0 comments on commit 601aa6d

Please sign in to comment.