mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
Some checks failed
CI / checks (push) Has been cancelled
Refactor booking retrieval to return errors instead of nil values, enabling more robust error handling throughout the booking, payment, and PDF endpoints. Add custom HTTP error page rendering for not found and internal server errors. Update interfaces and tests to match new method signatures. This improves user feedback and code maintainability.
78 lines
2 KiB
Go
78 lines
2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
u "github.com/rjNemo/underscore"
|
|
|
|
"github.com/rjNemo/rentease/internal/config"
|
|
"github.com/rjNemo/rentease/internal/service/booking"
|
|
"github.com/rjNemo/rentease/internal/view"
|
|
)
|
|
|
|
func handleCreatePayment(bs *booking.Service, hc *config.Host) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
http.Error(w, "invalid booking id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
amount := 0.0
|
|
if v := r.FormValue("amount"); v != "" {
|
|
amount, err = strconv.ParseFloat(v, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid amount", http.StatusBadRequest)
|
|
return
|
|
}
|
|
}
|
|
|
|
b, err := bs.One(id)
|
|
if bookingLookupFailed(w, err) {
|
|
return
|
|
}
|
|
|
|
if _, err := bs.CreatePayment(b.ID, amount, r.FormValue("paymentMethod")); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
nb, err := bs.One(id)
|
|
if bookingLookupFailed(w, err) {
|
|
return
|
|
}
|
|
|
|
component := view.PaymentList(
|
|
u.Map(nb.Payments, func(p booking.Payment) *view.PaymentViewModel {
|
|
return paymentViewModelFromBookingPayment(p, hc.StripeAccountID)
|
|
}),
|
|
)
|
|
|
|
if err := renderTempl(w, http.StatusOK, component); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
func handlePaymentForm(bs *booking.Service, hc *config.Host) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
http.Error(w, "invalid payment id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
p := bs.OnePayment(id)
|
|
form := view.PaymentForm(paymentViewModelFromBookingPayment(*p, hc.StripeAccountID))
|
|
if err := renderTempl(w, http.StatusOK, form); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|