All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m11s
180 lines
5.2 KiB
Go
180 lines
5.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreatePostLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Create a new blog post
|
|
func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
|
|
return &CreatePostLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, err error) {
|
|
// Start a transaction
|
|
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
|
if err != nil {
|
|
l.Errorf("Failed to begin transaction: %v", err)
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
// Extract all image file keys from content
|
|
imageKeys, err := utils.ExtractImageKeysFromContent(req.Content)
|
|
if err != nil {
|
|
l.Errorf("Failed to parse content: %v", err)
|
|
return nil, fmt.Errorf("invalid content format")
|
|
}
|
|
|
|
// Extract all archive file keys from content
|
|
archiveKeys, err := utils.ExtractArchiveKeysFromContent(req.Content)
|
|
if err != nil {
|
|
l.Errorf("Failed to parse archive files from content: %v", err)
|
|
return nil, fmt.Errorf("invalid content format")
|
|
}
|
|
|
|
// Combine all file keys
|
|
allFileKeys := append(imageKeys, archiveKeys...)
|
|
|
|
// Extract all hashtags from content
|
|
hashtags, err := utils.ExtractHashtagsFromContent(req.Content)
|
|
if err != nil {
|
|
l.Errorf("Failed to parse hashtags from content: %v", err)
|
|
return nil, fmt.Errorf("invalid content format")
|
|
}
|
|
|
|
// Migrate cover image from tmpfiles to files if provided
|
|
var coverID sql.NullInt64
|
|
if req.CoverImageKey != "" {
|
|
fileID, err := l.migrateFileFromTmpToFiles(tx, req.CoverImageKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
coverID.Int64 = fileID
|
|
coverID.Valid = true
|
|
}
|
|
|
|
// Migrate all content files (images and archives) from tmpfiles to files
|
|
for _, fileKey := range allFileKeys {
|
|
_, err := l.migrateFileFromTmpToFiles(tx, fileKey)
|
|
if err != nil {
|
|
l.Errorf("Failed to migrate content file %s: %v", fileKey, err)
|
|
// Continue with other files, don't fail the whole operation
|
|
}
|
|
}
|
|
|
|
// Insert new post
|
|
var postID int64
|
|
postQuery := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id`
|
|
err = tx.QueryRowContext(l.ctx, postQuery, req.Title, req.Content, coverID).Scan(&postID)
|
|
if err != nil {
|
|
l.Errorf("Failed to create post: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Create or get hashtag IDs and link them to the post
|
|
for _, tagName := range hashtags {
|
|
var tagID int64
|
|
|
|
// Try to get existing hashtag
|
|
getTagQuery := `SELECT id FROM hashtags WHERE name = $1`
|
|
err := tx.QueryRowContext(l.ctx, getTagQuery, tagName).Scan(&tagID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
// Hashtag doesn't exist, create it
|
|
createTagQuery := `INSERT INTO hashtags (name) VALUES ($1) RETURNING id`
|
|
err = tx.QueryRowContext(l.ctx, createTagQuery, tagName).Scan(&tagID)
|
|
if err != nil {
|
|
l.Errorf("Failed to create hashtag %s: %v", tagName, err)
|
|
continue
|
|
}
|
|
} else if err != nil {
|
|
l.Errorf("Failed to get hashtag %s: %v", tagName, err)
|
|
continue
|
|
}
|
|
|
|
// Link hashtag to post
|
|
linkQuery := `INSERT INTO post_hashtags (post_id, hashtag_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`
|
|
_, err = tx.ExecContext(l.ctx, linkQuery, postID, tagID)
|
|
if err != nil {
|
|
l.Errorf("Failed to link hashtag %s to post: %v", tagName, err)
|
|
// Continue with other hashtags
|
|
}
|
|
}
|
|
|
|
// Commit transaction
|
|
if err = tx.Commit(); err != nil {
|
|
l.Errorf("Failed to commit transaction: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &types.CreatePostResp{
|
|
PostId: fmt.Sprintf("%d", postID),
|
|
}, nil
|
|
}
|
|
|
|
// migrateFileFromTmpToFiles moves a file from tmpfiles to files table
|
|
func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (int64, error) {
|
|
// Check if file already exists in files table
|
|
var existingID int64
|
|
checkQuery := `SELECT id FROM files WHERE file_key = $1`
|
|
err := tx.QueryRowContext(l.ctx, checkQuery, fileKey).Scan(&existingID)
|
|
if err == nil {
|
|
// File already exists, return its ID
|
|
return existingID, nil
|
|
}
|
|
if err != sql.ErrNoRows {
|
|
l.Errorf("Failed to check existing file: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Verify file exists in tmpfiles table
|
|
var fileName string
|
|
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
|
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
l.Errorf("File not found in tmpfiles with key: %s", fileKey)
|
|
return 0, fmt.Errorf("file not found")
|
|
}
|
|
l.Errorf("Failed to get tmpfile: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Insert into files table
|
|
var fileID int64
|
|
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
|
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, fileKey).Scan(&fileID)
|
|
if err != nil {
|
|
l.Errorf("Failed to insert file record: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Delete from tmpfiles
|
|
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
|
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
|
if err != nil {
|
|
l.Errorf("Failed to delete tmpfile: %v", err)
|
|
// Don't fail the operation for this
|
|
}
|
|
|
|
return fileID, nil
|
|
}
|