2 changed files with 219 additions and 17 deletions
@ -1,26 +1,82 @@ |
|||
package web |
|||
|
|||
import ( |
|||
"html/template" |
|||
"fmt" |
|||
"log" |
|||
root "marmic/servicetrade-toolbox" |
|||
"marmic/servicetrade-toolbox/internal/api" |
|||
"net/http" |
|||
"net/url" |
|||
) |
|||
|
|||
func JobsHandler(w http.ResponseWriter, r *http.Request) { |
|||
jobs := []string{"Job 1", "Job 2", "Job 3"} // Replace with actual data fetching
|
|||
|
|||
if r.Header.Get("HX-Request") == "true" { |
|||
// This is an HTMX request, return the jobs content wrapped in the content template
|
|||
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/partials/jobs.html")) |
|||
tmpl.ExecuteTemplate(w, "content", map[string]interface{}{ |
|||
"Title": "Jobs", |
|||
"Jobs": jobs, |
|||
}) |
|||
} else { |
|||
// This is a full page request
|
|||
tmpl := template.Must(template.ParseFiles("templates/layout.html", "templates/partials/jobs.html")) |
|||
tmpl.Execute(w, map[string]interface{}{ |
|||
"Title": "Jobs", |
|||
"Jobs": jobs, |
|||
}) |
|||
session, ok := r.Context().Value("session").(*api.Session) |
|||
if !ok { |
|||
http.Error(w, "Unauthorized", http.StatusUnauthorized) |
|||
return |
|||
} |
|||
|
|||
tmpl := root.WebTemplates |
|||
data := map[string]interface{}{ |
|||
"Title": "Jobs", |
|||
} |
|||
|
|||
switch r.URL.Path { |
|||
case "/jobs": |
|||
// Fetch and display search results only
|
|||
jobs, err := handleJobSearch(r, session) |
|||
if err != nil { |
|||
log.Printf("Error in job search: %v", err) |
|||
data["Error"] = true |
|||
data["ErrorMsg"] = "Failed to fetch jobs. Please try again later." |
|||
} else { |
|||
data["Jobs"] = jobs |
|||
} |
|||
err = tmpl.ExecuteTemplate(w, "job_search_results", data) |
|||
if err != nil { |
|||
log.Printf("Template execution error: %v", err) |
|||
http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
|||
} |
|||
|
|||
case "/jobs/documents": |
|||
// Fetch jobs and uploaded documents
|
|||
jobs, err := handleJobSearch(r, session) |
|||
if err != nil { |
|||
log.Printf("Error in job search: %v", err) |
|||
data["Error"] = true |
|||
data["ErrorMsg"] = "Failed to fetch jobs. Please try again later." |
|||
} else { |
|||
data["Jobs"] = jobs |
|||
} |
|||
|
|||
err = tmpl.ExecuteTemplate(w, "job_document_management", data) |
|||
if err != nil { |
|||
log.Printf("Template execution error: %v", err) |
|||
http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
|||
} |
|||
|
|||
default: |
|||
http.Error(w, "Not Found", http.StatusNotFound) |
|||
} |
|||
} |
|||
|
|||
func handleJobSearch(r *http.Request, session *api.Session) ([]api.Job, error) { |
|||
queryParams := r.URL.Query() |
|||
cleanedParams := url.Values{} |
|||
log.Printf("Searching jobs with filters: %v", queryParams) |
|||
|
|||
for key, values := range queryParams { |
|||
for _, value := range values { |
|||
if value != "" { |
|||
cleanedParams.Add(key, value) |
|||
} |
|||
} |
|||
} |
|||
|
|||
jobs, err := session.SearchJobs(cleanedParams) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("error fetching jobs: %w", err) |
|||
} |
|||
|
|||
return jobs, nil |
|||
} |
|||
|
|||
Loading…
Reference in new issue