4.13.0: Добавлено удаление записей в статистике
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m22s

This commit is contained in:
poignatov
2026-02-03 18:42:44 +03:00
parent 36dd96976f
commit df3cced995
6 changed files with 168 additions and 8 deletions

View File

@@ -3754,6 +3754,7 @@ func main() {
protected.HandleFunc("/project/create", app.createProjectHandler).Methods("POST", "OPTIONS")
protected.HandleFunc("/d2dc349a-0d13-49b2-a8f0-1ab094bfba9b", app.getFullStatisticsHandler).Methods("GET", "OPTIONS")
protected.HandleFunc("/api/today-entries", app.getTodayEntriesHandler).Methods("GET", "OPTIONS")
protected.HandleFunc("/api/entries/{id}", app.deleteEntryHandler).Methods("DELETE", "OPTIONS")
// Integrations
protected.HandleFunc("/api/integrations/telegram", app.getTelegramIntegrationHandler).Methods("GET", "OPTIONS")
@@ -6321,6 +6322,74 @@ func (a *App) getTodayEntriesHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(entries)
}
// deleteEntryHandler удаляет entry и каскадно удаляет связанные nodes
func (a *App) deleteEntryHandler(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)
entryIDStr := vars["id"]
entryID, err := strconv.Atoi(entryIDStr)
if err != nil {
sendErrorWithCORS(w, "Invalid entry ID", http.StatusBadRequest)
return
}
// Проверяем, что entry принадлежит пользователю
var entryUserID int
err = a.DB.QueryRow("SELECT user_id FROM entries WHERE id = $1", entryID).Scan(&entryUserID)
if err == sql.ErrNoRows {
sendErrorWithCORS(w, "Entry not found", http.StatusNotFound)
return
}
if err != nil {
log.Printf("Error checking entry ownership: %v", err)
sendErrorWithCORS(w, err.Error(), http.StatusInternalServerError)
return
}
// Проверяем права доступа
if entryUserID != userID {
sendErrorWithCORS(w, "Forbidden", http.StatusForbidden)
return
}
// Удаляем entry (nodes удалятся каскадно из-за ON DELETE CASCADE)
result, err := a.DB.Exec("DELETE FROM entries WHERE id = $1 AND user_id = $2", entryID, userID)
if err != nil {
log.Printf("Error deleting entry: %v", err)
sendErrorWithCORS(w, err.Error(), http.StatusInternalServerError)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
log.Printf("Error getting rows affected: %v", err)
sendErrorWithCORS(w, err.Error(), http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
sendErrorWithCORS(w, "Entry not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"message": "Entry deleted successfully",
})
}
// getTelegramIntegrationHandler возвращает текущую telegram интеграцию с deep link
func (a *App) getTelegramIntegrationHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {