Files
Blog/app/internal/logic/getpostlogic.go
cialloo 0ae4d1705b
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
Refactor GetPost and ListPosts logic to use time.Time for timestamps; convert to Unix milliseconds and improve error handling
2025-10-26 17:44:49 +08:00

86 lines
2.2 KiB
Go

package logic
import (
"context"
"database/sql"
"fmt"
"time"
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Get a blog post by ID
func NewGetPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostLogic {
return &GetPostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetPostLogic) GetPost(req *types.GetPostReq) (resp *types.GetPostResp, err error) {
var post types.GetPostResp
var coverID sql.NullInt64
var createdAt, updatedAt time.Time
// Query post with cover image
query := `
SELECT p.id, p.title, p.content, p.created_at, p.updated_at, p.cover_id
FROM posts p
WHERE p.id = $1
`
err = l.svcCtx.DB.QueryRowContext(l.ctx, query, req.PostId).Scan(
&post.PostId, &post.Title, &post.Content, &createdAt, &updatedAt, &coverID,
)
if err != nil {
if err == sql.ErrNoRows {
l.Errorf("Post not found with id: %s", req.PostId)
return nil, fmt.Errorf("post not found")
}
l.Errorf("Failed to get post: %v", err)
return nil, err
}
// Convert timestamps to Unix milliseconds
post.CreatedAt = createdAt.UnixMilli()
post.UpdatedAt = updatedAt.UnixMilli()
// Convert timestamps to Unix milliseconds
post.CreatedAt = createdAt.UnixMilli()
post.UpdatedAt = updatedAt.UnixMilli()
// If cover image exists, get its URL
if coverID.Valid {
coverQuery := `SELECT file_key FROM files WHERE id = $1`
var fileKey string
err = l.svcCtx.DB.QueryRowContext(l.ctx, coverQuery, coverID.Int64).Scan(&fileKey)
if err == nil {
// Generate presigned URL for cover image
expiration := time.Duration(l.svcCtx.Config.S3.PresignedURLExpiration) * time.Second
presignClient := s3.NewPresignClient(l.svcCtx.S3Client)
getObjectInput := &s3.GetObjectInput{
Bucket: &l.svcCtx.Config.S3.Bucket,
Key: &fileKey,
}
presignedReq, err := presignClient.PresignGetObject(l.ctx, getObjectInput, func(opts *s3.PresignOptions) {
opts.Expires = expiration
})
if err == nil {
post.CoverImageUrl = presignedReq.URL
}
}
}
return &post, nil
}