diff --git a/internal/common/pack/notice.go b/internal/common/pack/notice.go index 4a68284..1555456 100644 --- a/internal/common/pack/notice.go +++ b/internal/common/pack/notice.go @@ -21,13 +21,9 @@ import ( db "github.com/west2-online/fzuhelper-server/pkg/db/model" ) -func BuildNoticeList(notices *[]db.Notice) []*model.NoticeInfo { - if notices == nil { - return nil - } - rows := *notices - list := make([]*model.NoticeInfo, len(rows)) - for i, notice := range rows { +func BuildNoticeList(notices []db.Notice) []*model.NoticeInfo { + list := make([]*model.NoticeInfo, len(notices)) + for i, notice := range notices { list[i] = &model.NoticeInfo{ Title: ¬ice.Title, Url: ¬ice.URL, diff --git a/internal/common/service/get_notice.go b/internal/common/service/get_notice.go index ce14bf2..37d1cd4 100644 --- a/internal/common/service/get_notice.go +++ b/internal/common/service/get_notice.go @@ -24,7 +24,7 @@ import ( "github.com/west2-online/jwch" ) -func (s *CommonService) GetNotice(pageNum int) (list *[]model.Notice, total int, err error) { +func (s *CommonService) GetNotice(pageNum int) (list []model.Notice, total int, err error) { list, err = s.db.Notice.GetNoticeByPage(s.ctx, pageNum) if err != nil { return nil, 0, fmt.Errorf("CommonService.GetNotice get notice from database:%w", err) diff --git a/pkg/db/notice/get_notice.go b/pkg/db/notice/get_notice.go index 4b8c175..16ab6eb 100644 --- a/pkg/db/notice/get_notice.go +++ b/pkg/db/notice/get_notice.go @@ -24,11 +24,11 @@ import ( "github.com/west2-online/fzuhelper-server/pkg/errno" ) -func (d *DBNotice) GetNoticeByPage(ctx context.Context, pageNum int) (notices *[]model.Notice, err error) { - var list []model.Notice +func (d *DBNotice) GetNoticeByPage(ctx context.Context, pageNum int) (list []model.Notice, err error) { + // 不使用[]*的原因:Find 返回多个结果时,只能使用[] offset := (pageNum - 1) * constants.NoticePageSize if err := d.client.WithContext(ctx).Limit(constants.NoticePageSize).Offset(offset).Find(&list).Error; err != nil { return nil, errno.Errorf(errno.InternalDatabaseErrorCode, "dal.GetNoticeByPage error: %s", err) } - return &list, nil + return list, nil }