完成剩余模块 Context 超时控制改造
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package announcement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -17,9 +18,9 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
}
|
||||
|
||||
// List 获取公告列表(前台用户)
|
||||
func (r *Repository) List(query AnnouncementListQuery) (*PaginatedResult, error) {
|
||||
func (r *Repository) List(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
tx := r.db.Model(&model.Announcement{}).Where("status = ?", "published")
|
||||
tx := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("status = ?", "published")
|
||||
|
||||
if query.Category != "" {
|
||||
tx = tx.Where("category = ?", query.Category)
|
||||
@@ -52,23 +53,24 @@ func (r *Repository) List(query AnnouncementListQuery) (*PaginatedResult, error)
|
||||
}
|
||||
|
||||
// GetByID 获取公告详情
|
||||
func (r *Repository) GetByID(id uint64) (*AnnouncementDTO, error) {
|
||||
func (r *Repository) GetByID(ctx context.Context, id uint64) (*AnnouncementDTO, error) {
|
||||
var announcement model.Announcement
|
||||
if err := r.db.Where("id = ? AND status = ?", id, "published").First(&announcement).Error; err != nil {
|
||||
db := r.db.WithContext(ctx)
|
||||
if err := db.Where("id = ? AND status = ?", id, "published").First(&announcement).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 增加查看次数
|
||||
r.db.Model(&model.Announcement{}).Where("id = ?", id).UpdateColumn("view_count", gorm.Expr("view_count + ?", 1))
|
||||
db.Model(&model.Announcement{}).Where("id = ?", id).UpdateColumn("view_count", gorm.Expr("view_count + ?", 1))
|
||||
|
||||
dto := toAnnouncementDTO(announcement)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// AdminList 管理员获取公告列表
|
||||
func (r *Repository) AdminList(query AnnouncementListQuery) (*PaginatedResult, error) {
|
||||
func (r *Repository) AdminList(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
tx := r.db.Model(&model.Announcement{})
|
||||
tx := r.db.WithContext(ctx).Model(&model.Announcement{})
|
||||
|
||||
if query.Status != "" {
|
||||
tx = tx.Where("status = ?", query.Status)
|
||||
@@ -104,9 +106,9 @@ func (r *Repository) AdminList(query AnnouncementListQuery) (*PaginatedResult, e
|
||||
}
|
||||
|
||||
// AdminGetByID 管理员获取公告详情
|
||||
func (r *Repository) AdminGetByID(id uint64) (*AnnouncementDTO, error) {
|
||||
func (r *Repository) AdminGetByID(ctx context.Context, id uint64) (*AnnouncementDTO, error) {
|
||||
var announcement model.Announcement
|
||||
if err := r.db.Where("id = ?", id).First(&announcement).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&announcement).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -115,7 +117,7 @@ func (r *Repository) AdminGetByID(id uint64) (*AnnouncementDTO, error) {
|
||||
}
|
||||
|
||||
// Create 创建公告
|
||||
func (r *Repository) Create(req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
|
||||
func (r *Repository) Create(ctx context.Context, req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
|
||||
announcement := model.Announcement{
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
@@ -127,7 +129,7 @@ func (r *Repository) Create(req CreateAnnouncementRequest, createdBy uint64) (*A
|
||||
CreatedBy: &createdBy,
|
||||
}
|
||||
|
||||
if err := r.db.Create(&announcement).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Create(&announcement).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -136,7 +138,7 @@ func (r *Repository) Create(req CreateAnnouncementRequest, createdBy uint64) (*A
|
||||
}
|
||||
|
||||
// Update 更新公告
|
||||
func (r *Repository) Update(id uint64, req UpdateAnnouncementRequest) (*AnnouncementDTO, error) {
|
||||
func (r *Repository) Update(ctx context.Context, id uint64, req UpdateAnnouncementRequest) (*AnnouncementDTO, error) {
|
||||
updates := make(map[string]interface{})
|
||||
|
||||
if req.Title != "" {
|
||||
@@ -152,17 +154,17 @@ func (r *Repository) Update(id uint64, req UpdateAnnouncementRequest) (*Announce
|
||||
updates["is_pinned"] = req.IsPinned
|
||||
updates["is_important"] = req.IsImportant
|
||||
|
||||
if err := r.db.Model(&model.Announcement{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.AdminGetByID(id)
|
||||
return r.AdminGetByID(ctx, id)
|
||||
}
|
||||
|
||||
// Publish 发布公告
|
||||
func (r *Repository) Publish(id uint64) error {
|
||||
func (r *Repository) Publish(ctx context.Context, id uint64) error {
|
||||
now := time.Now()
|
||||
return r.db.Model(&model.Announcement{}).
|
||||
return r.db.WithContext(ctx).Model(&model.Announcement{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": "published",
|
||||
@@ -171,15 +173,15 @@ func (r *Repository) Publish(id uint64) error {
|
||||
}
|
||||
|
||||
// Archive 归档公告
|
||||
func (r *Repository) Archive(id uint64) error {
|
||||
return r.db.Model(&model.Announcement{}).
|
||||
func (r *Repository) Archive(ctx context.Context, id uint64) error {
|
||||
return r.db.WithContext(ctx).Model(&model.Announcement{}).
|
||||
Where("id = ?", id).
|
||||
Update("status", "archived").Error
|
||||
}
|
||||
|
||||
// Delete 删除公告
|
||||
func (r *Repository) Delete(id uint64) error {
|
||||
return r.db.Where("id = ?", id).Delete(&model.Announcement{}).Error
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.Announcement{}).Error
|
||||
}
|
||||
|
||||
func toAnnouncementDTO(a model.Announcement) AnnouncementDTO {
|
||||
|
||||
Reference in New Issue
Block a user