完成剩余模块 Context 超时控制改造

This commit is contained in:
yml2213
2026-06-10 12:58:59 +08:00
parent d2858c529d
commit 6ae8f0e830
24 changed files with 370 additions and 323 deletions
@@ -1,6 +1,7 @@
package announcement
import (
"context"
"errors"
"gorm.io/gorm"
@@ -15,13 +16,13 @@ func NewService(repo *Repository) *Service {
}
var (
ErrNotFound = errors.New("公告不存在")
ErrInvalidRequest = errors.New("请求参数不正确")
ErrUnauthorized = errors.New("未授权")
ErrNotFound = errors.New("公告不存在")
ErrInvalidRequest = errors.New("请求参数不正确")
ErrUnauthorized = errors.New("未授权")
)
// List 获取公告列表(前台用户)
func (s *Service) List(query AnnouncementListQuery) (*PaginatedResult, error) {
func (s *Service) List(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
if query.Page < 1 {
query.Page = 1
}
@@ -32,12 +33,12 @@ func (s *Service) List(query AnnouncementListQuery) (*PaginatedResult, error) {
query.PageSize = 100
}
return s.repo.List(query)
return s.repo.List(ctx, query)
}
// GetByID 获取公告详情
func (s *Service) GetByID(id uint64) (*AnnouncementDTO, error) {
dto, err := s.repo.GetByID(id)
func (s *Service) GetByID(ctx context.Context, id uint64) (*AnnouncementDTO, error) {
dto, err := s.repo.GetByID(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
@@ -48,7 +49,7 @@ func (s *Service) GetByID(id uint64) (*AnnouncementDTO, error) {
}
// AdminList 管理员获取公告列表
func (s *Service) AdminList(query AnnouncementListQuery) (*PaginatedResult, error) {
func (s *Service) AdminList(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
if query.Page < 1 {
query.Page = 1
}
@@ -59,12 +60,12 @@ func (s *Service) AdminList(query AnnouncementListQuery) (*PaginatedResult, erro
query.PageSize = 100
}
return s.repo.AdminList(query)
return s.repo.AdminList(ctx, query)
}
// AdminGetByID 管理员获取公告详情
func (s *Service) AdminGetByID(id uint64) (*AnnouncementDTO, error) {
dto, err := s.repo.AdminGetByID(id)
func (s *Service) AdminGetByID(ctx context.Context, id uint64) (*AnnouncementDTO, error) {
dto, err := s.repo.AdminGetByID(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
@@ -75,26 +76,26 @@ func (s *Service) AdminGetByID(id uint64) (*AnnouncementDTO, error) {
}
// Create 创建公告
func (s *Service) Create(req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
return s.repo.Create(req, createdBy)
func (s *Service) Create(ctx context.Context, req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
return s.repo.Create(ctx, req, createdBy)
}
// Update 更新公告
func (s *Service) Update(id uint64, req UpdateAnnouncementRequest) (*AnnouncementDTO, error) {
return s.repo.Update(id, req)
func (s *Service) Update(ctx context.Context, id uint64, req UpdateAnnouncementRequest) (*AnnouncementDTO, error) {
return s.repo.Update(ctx, id, req)
}
// Publish 发布公告
func (s *Service) Publish(id uint64) error {
return s.repo.Publish(id)
func (s *Service) Publish(ctx context.Context, id uint64) error {
return s.repo.Publish(ctx, id)
}
// Archive 归档公告
func (s *Service) Archive(id uint64) error {
return s.repo.Archive(id)
func (s *Service) Archive(ctx context.Context, id uint64) error {
return s.repo.Archive(ctx, id)
}
// Delete 删除公告
func (s *Service) Delete(id uint64) error {
return s.repo.Delete(id)
func (s *Service) Delete(ctx context.Context, id uint64) error {
return s.repo.Delete(ctx, id)
}