You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.6 KiB
55 lines
1.6 KiB
package handlers
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"marmic/servicetrade-toolbox/internal/api"
|
|
"net/http"
|
|
)
|
|
|
|
func InvoicesHandler(w http.ResponseWriter, r *http.Request) {
|
|
session, ok := r.Context().Value("session").(*api.Session)
|
|
if !ok {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// Handle the search request
|
|
if r.Method == "GET" && r.URL.Query().Get("search") != "" {
|
|
handleInvoiceSearch(w, r, session)
|
|
return
|
|
}
|
|
|
|
// Handle the initial page load
|
|
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/invoices.html"))
|
|
err := tmpl.Execute(w, nil)
|
|
if err != nil {
|
|
log.Printf("Error executing template: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func handleInvoiceSearch(w http.ResponseWriter, r *http.Request, session *api.Session) {
|
|
invoiceIdentifier := r.URL.Query().Get("search")
|
|
|
|
if invoiceIdentifier == "" {
|
|
w.Write([]byte(""))
|
|
return
|
|
}
|
|
|
|
invoice, err := session.GetInvoice(invoiceIdentifier)
|
|
if err != nil {
|
|
log.Printf("Error fetching invoice: %v", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html"))
|
|
tmpl.ExecuteTemplate(w, "invoice_search_results", map[string]interface{}{"Error": err.Error()})
|
|
return
|
|
}
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/partials/invoice_search_results.html"))
|
|
err = tmpl.ExecuteTemplate(w, "invoice_search_results", invoice)
|
|
if err != nil {
|
|
log.Printf("Error executing template: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|