diff --git a/app/internal/logic/createpostlogic.go b/app/internal/logic/createpostlogic.go index 787fc27..6b0d598 100644 --- a/app/internal/logic/createpostlogic.go +++ b/app/internal/logic/createpostlogic.go @@ -29,22 +29,67 @@ func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, err error) { var coverID sql.NullInt64 - // If cover image key is provided, get the file ID from files table + // If cover image key is provided, move it from tmpfiles to files table if req.CoverImageKey != "" { - query := `SELECT id FROM files WHERE file_key = $1` - err := l.svcCtx.DB.QueryRowContext(l.ctx, query, req.CoverImageKey).Scan(&coverID.Int64) + // 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() + + // Get the tmpfile record + var fileName string + tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1` + err = tx.QueryRowContext(l.ctx, tmpQuery, req.CoverImageKey).Scan(&fileName) if err != nil { if err == sql.ErrNoRows { - l.Errorf("Cover image file not found with key: %s", req.CoverImageKey) + l.Errorf("Cover image not found in tmpfiles with key: %s", req.CoverImageKey) return nil, fmt.Errorf("cover image not found") } - l.Errorf("Failed to get cover image file: %v", err) + l.Errorf("Failed to get tmpfile: %v", err) + return nil, err + } + + // Insert into files table + fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id` + err = tx.QueryRowContext(l.ctx, fileQuery, fileName, req.CoverImageKey).Scan(&coverID.Int64) + if err != nil { + l.Errorf("Failed to insert file record: %v", err) return nil, err } coverID.Valid = true + + // Delete from tmpfiles + deleteQuery := `DELETE FROM tmpfiles WHERE key = $1` + _, err = tx.ExecContext(l.ctx, deleteQuery, req.CoverImageKey) + if err != nil { + l.Errorf("Failed to delete tmpfile: %v", err) + return nil, err + } + + // 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 + } + + // 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 } - // Insert new post + // No cover image - just insert the post var postID int64 query := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id` err = l.svcCtx.DB.QueryRowContext(l.ctx, query, req.Title, req.Content, coverID).Scan(&postID) diff --git a/app/internal/logic/editpostlogic.go b/app/internal/logic/editpostlogic.go index 641dd08..5c0bdcc 100644 --- a/app/internal/logic/editpostlogic.go +++ b/app/internal/logic/editpostlogic.go @@ -29,24 +29,89 @@ func NewEditPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditPost func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostResp, err error) { var coverID sql.NullInt64 - // If cover image key is provided, get the file ID from files table + // If cover image key is provided, move it from tmpfiles to files table if req.CoverImageKey != "" { - query := `SELECT id FROM files WHERE file_key = $1` - err := l.svcCtx.DB.QueryRowContext(l.ctx, query, req.CoverImageKey).Scan(&coverID.Int64) + // 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() + + // Get the tmpfile record + var fileName string + tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1` + err = tx.QueryRowContext(l.ctx, tmpQuery, req.CoverImageKey).Scan(&fileName) if err != nil { if err == sql.ErrNoRows { - l.Errorf("Cover image file not found with key: %s", req.CoverImageKey) + l.Errorf("Cover image not found in tmpfiles with key: %s", req.CoverImageKey) return nil, fmt.Errorf("cover image not found") } - l.Errorf("Failed to get cover image file: %v", err) + l.Errorf("Failed to get tmpfile: %v", err) + return nil, err + } + + // Get old cover_id to delete later + var oldCoverID sql.NullInt64 + oldCoverQuery := `SELECT cover_id FROM posts WHERE id = $1` + err = tx.QueryRowContext(l.ctx, oldCoverQuery, req.PostId).Scan(&oldCoverID) + 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 old cover: %v", err) + return nil, err + } + + // Insert into files table + fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id` + err = tx.QueryRowContext(l.ctx, fileQuery, fileName, req.CoverImageKey).Scan(&coverID.Int64) + if err != nil { + l.Errorf("Failed to insert file record: %v", err) return nil, err } coverID.Valid = true + + // Delete from tmpfiles + deleteQuery := `DELETE FROM tmpfiles WHERE key = $1` + _, err = tx.ExecContext(l.ctx, deleteQuery, req.CoverImageKey) + if err != nil { + l.Errorf("Failed to delete tmpfile: %v", err) + return nil, err + } + + // Update post + updateQuery := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4` + _, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, coverID, req.PostId) + if err != nil { + l.Errorf("Failed to update post: %v", err) + return nil, err + } + + // Delete old cover file if exists + if oldCoverID.Valid { + deleteOldFileQuery := `DELETE FROM files WHERE id = $1` + _, err = tx.ExecContext(l.ctx, deleteOldFileQuery, oldCoverID.Int64) + if err != nil { + l.Errorf("Failed to delete old cover file: %v", err) + // Don't fail the whole operation for this + } + } + + // Commit transaction + if err = tx.Commit(); err != nil { + l.Errorf("Failed to commit transaction: %v", err) + return nil, err + } + + return &types.EditPostResp{}, nil } - // Update post with updated_at timestamp - query := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4` - result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Title, req.Content, coverID, req.PostId) + // No new cover image - just update the post + query := `UPDATE posts SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3` + result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Title, req.Content, req.PostId) if err != nil { l.Errorf("Failed to update post: %v", err) return nil, err