package task import ( "net/http" "strconv" "task-track-backend/model" "task-track-backend/pkg/database" "github.com/gin-gonic/gin" ) func (h *TaskHandler) DeleteTaskComment(c *gin.Context) { // 获取任务ID taskIDStr := c.Param("id") taskID, err := strconv.ParseUint(taskIDStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的任务ID"}) return } // 获取评论ID commentIDStr := c.Param("comment_id") commentID, err := strconv.ParseUint(commentIDStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的评论ID"}) return } db := database.GetDB() // 检查任务是否存在 var existingTask model.Task if err := db.First(&existingTask, uint(taskID)).Error; err != nil { if err.Error() == "record not found" { c.JSON(http.StatusNotFound, gin.H{"error": "任务不存在"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "查询任务失败"}) return } // 检查评论是否存在且属于该任务 var existingComment model.TaskComment if err := db.Where("id = ? AND task_id = ?", uint(commentID), uint(taskID)). First(&existingComment).Error; err != nil { if err.Error() == "record not found" { c.JSON(http.StatusNotFound, gin.H{"error": "评论不存在"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "查询评论失败"}) return } // 删除评论 if err := db.Delete(&existingComment).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "删除评论失败"}) return } c.JSON(http.StatusOK, gin.H{"message": "评论删除成功"}) }