4.20.6: Исправлено удаление фото в желаниях
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m28s

This commit is contained in:
poignatov
2026-02-05 12:59:16 +03:00
parent 0463c237c0
commit 59d376b999
4 changed files with 112 additions and 4 deletions

View File

@@ -3982,6 +3982,7 @@ func main() {
protected.HandleFunc("/api/wishlist/{id}", app.updateWishlistHandler).Methods("PUT", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}", app.deleteWishlistHandler).Methods("DELETE", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}/image", app.uploadWishlistImageHandler).Methods("POST", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}/image", app.deleteWishlistImageHandler).Methods("DELETE", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}/complete", app.completeWishlistHandler).Methods("POST", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}/uncomplete", app.uncompleteWishlistHandler).Methods("POST", "OPTIONS")
protected.HandleFunc("/api/wishlist/{id}/copy", app.copyWishlistHandler).Methods("POST", "OPTIONS")
@@ -11866,6 +11867,88 @@ func (a *App) uploadWishlistImageHandler(w http.ResponseWriter, r *http.Request)
})
}
// deleteWishlistImageHandler удаляет картинку желания
func (a *App) deleteWishlistImageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
setCORSHeaders(w)
w.WriteHeader(http.StatusOK)
return
}
setCORSHeaders(w)
userID, ok := getUserIDFromContext(r)
if !ok {
sendErrorWithCORS(w, "Unauthorized", http.StatusUnauthorized)
return
}
vars := mux.Vars(r)
wishlistID, err := strconv.Atoi(vars["id"])
if err != nil {
sendErrorWithCORS(w, "Invalid wishlist ID", http.StatusBadRequest)
return
}
// Проверяем доступ к желанию
hasAccess, _, _, err := a.checkWishlistAccess(wishlistID, userID)
if err == sql.ErrNoRows {
sendErrorWithCORS(w, "Wishlist item not found", http.StatusNotFound)
return
}
if err != nil {
log.Printf("Error checking wishlist access: %v", err)
sendErrorWithCORS(w, fmt.Sprintf("Error checking wishlist access: %v", err), http.StatusInternalServerError)
return
}
if !hasAccess {
sendErrorWithCORS(w, "Access denied", http.StatusForbidden)
return
}
// Получаем текущий путь к изображению из БД
var currentImagePath sql.NullString
err = a.DB.QueryRow(`
SELECT image_path
FROM wishlist_items
WHERE id = $1
`, wishlistID).Scan(&currentImagePath)
if err != nil {
log.Printf("Error getting image path: %v", err)
sendErrorWithCORS(w, "Error getting image path", http.StatusInternalServerError)
return
}
// Удаляем файл, если он существует
if currentImagePath.Valid && currentImagePath.String != "" {
filePath := filepath.Join("/app", currentImagePath.String)
err = os.Remove(filePath)
if err != nil && !os.IsNotExist(err) {
log.Printf("Error deleting image file: %v", err)
// Продолжаем выполнение даже если файл не найден
}
}
// Обновляем БД, устанавливая image_path в NULL
_, err = a.DB.Exec(`
UPDATE wishlist_items
SET image_path = NULL, updated_at = NOW()
WHERE id = $1
`, wishlistID)
if err != nil {
log.Printf("Error updating database: %v", err)
sendErrorWithCORS(w, "Error updating database", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"message": "Image deleted successfully",
})
}
// completeWishlistHandler помечает желание как завершённое
func (a *App) completeWishlistHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {