2024-02-07 01:18:17 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1f349/lavender/database"
|
2024-05-13 20:33:25 +01:00
|
|
|
"github.com/1f349/lavender/logger"
|
2024-02-07 01:18:17 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DbTx wraps a database transaction with http error messages and a simple action
|
|
|
|
// function. If the action function returns an error the transaction will be
|
|
|
|
// rolled back. If there is no error then the transaction is committed.
|
2024-05-17 21:40:31 +01:00
|
|
|
func (h *HttpServer) DbTx(rw http.ResponseWriter, action func(db *database.Queries) error) bool {
|
|
|
|
err := action(h.db)
|
2024-02-07 01:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "Database error", http.StatusInternalServerError)
|
2024-05-17 21:40:31 +01:00
|
|
|
logger.Logger.Helper()
|
|
|
|
logger.Logger.Warn("Database action error", "err", err)
|
2024-02-07 01:18:17 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|