You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
package task
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
"task-track-backend/model"
|
|
"task-track-backend/pkg/database"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (h *TaskHandler) DeleteTaskAttachment(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
|
|
attachmentIDStr := c.Param("attachment_id")
|
|
attachmentID, err := strconv.ParseUint(attachmentIDStr, 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 existingAttachment model.TaskAttachment
|
|
if err := db.Where("id = ? AND task_id = ?", uint(attachmentID), uint(taskID)).
|
|
First(&existingAttachment).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 := os.Remove(existingAttachment.FilePath); err != nil {
|
|
// 即使文件删除失败,也继续删除数据库记录
|
|
// 记录日志但不返回错误
|
|
}
|
|
|
|
// 删除数据库记录
|
|
if err := db.Delete(&existingAttachment).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除附件失败"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "附件删除成功"})
|
|
}
|
|
|