mirror of
https://github.com/1f349/tulip.git
synced 2024-11-09 22:42:53 +00:00
31 lines
677 B
Go
31 lines
677 B
Go
package server
|
|
|
|
import (
|
|
"github.com/1f349/tulip/database"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func (h *HttpServer) dbTx(rw http.ResponseWriter, action func(tx *database.Tx) error) bool {
|
|
tx, err := h.db.Begin()
|
|
if err != nil {
|
|
http.Error(rw, "Failed to begin database transaction", http.StatusInternalServerError)
|
|
return true
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
err = action(tx)
|
|
if err != nil {
|
|
http.Error(rw, "Database error", http.StatusInternalServerError)
|
|
log.Println("Database action error:", err)
|
|
return true
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
http.Error(rw, "Database error", http.StatusInternalServerError)
|
|
log.Println("Database commit error:", err)
|
|
}
|
|
|
|
return false
|
|
}
|