MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / GetPublishPostById

Method GetPublishPostById

internal/service/post.go:134–177  ·  view source on GitHub ↗

GetPublishPostById 获取已发布的帖子详细信息

(ctx context.Context, postId uint, uid int64)

Source from the content-addressed store, hash-verified

132
133// GetPublishPostById 获取已发布的帖子详细信息
134func (p *postService) GetPublishPostById(ctx context.Context, postId uint, uid int64) (domain.Post, error) {
135 dp, err := p.repo.GetPublishPostById(ctx, postId)
136 if err != nil {
137 p.l.Error("获取已发布帖子失败", zap.Error(err))
138 return domain.Post{}, fmt.Errorf("获取已发布帖子失败: %w", err)
139 }
140
141 // 设置超时上下文
142 ctx, cancel := context.WithTimeout(ctx, time.Second*5)
143 defer cancel()
144
145 // 异步处理阅读事件
146 asyncReadEvent := general.WithAsyncCancel(ctx, cancel, func() error {
147 if er := p.producer.ProduceReadEvent(post.ReadEvent{
148 PostId: postId,
149 Uid: uid,
150 Title: dp.Title,
151 Content: dp.Content,
152 }); er != nil {
153 p.l.Error("生成阅读事件失败", zap.Error(er))
154 return fmt.Errorf("生成阅读事件失败: %w", er)
155 }
156 return nil
157 })
158 asyncReadEvent()
159
160 inc, err := p.incRepo.Get(ctx, postId)
161 if err != nil && err != gorm.ErrRecordNotFound {
162 p.l.Error("获取互动数据失败", zap.Error(err))
163 return domain.Post{}, fmt.Errorf("获取互动数据失败: %w", err)
164 } else if err == gorm.ErrRecordNotFound {
165 inc = domain.Interactive{
166 LikeCount: 0,
167 ReadCount: 0,
168 CollectCount: 0,
169 }
170 }
171
172 dp.LikeCount = inc.LikeCount
173 dp.ReadCount = inc.ReadCount
174 dp.CollectCount = inc.CollectCount
175
176 return dp, nil
177}
178
179// ListPosts 列出帖子
180func (p *postService) ListPosts(ctx context.Context, pagination domain.Pagination) ([]domain.Post, error) {

Callers

nothing calls this directly

Calls 4

WithAsyncCancelFunction · 0.92
GetPublishPostByIdMethod · 0.65
ProduceReadEventMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected