2020-11-22 09:11:57 +01:00
|
|
|
package main
|
|
|
|
|
2021-07-17 09:33:44 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
2020-11-22 09:11:57 +01:00
|
|
|
|
2021-03-22 08:20:56 +01:00
|
|
|
const taxonomyContextKey = "taxonomy"
|
|
|
|
|
2021-06-06 14:39:42 +02:00
|
|
|
func (a *goBlog) serveTaxonomy(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 14:00:11 +01:00
|
|
|
blog, _ := a.getBlog(r)
|
2021-07-12 16:19:28 +02:00
|
|
|
tax := r.Context().Value(taxonomyContextKey).(*configTaxonomy)
|
2021-06-06 14:39:42 +02:00
|
|
|
allValues, err := a.db.allTaxonomyValues(blog, tax.Name)
|
2021-03-22 08:20:56 +01:00
|
|
|
if err != nil {
|
2021-06-06 14:39:42 +02:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 08:20:56 +01:00
|
|
|
return
|
2020-11-22 09:11:57 +01:00
|
|
|
}
|
2022-01-30 16:40:53 +01:00
|
|
|
a.render(w, r, a.renderTaxonomy, &renderData{
|
2021-12-20 14:00:11 +01:00
|
|
|
Canonical: a.getFullAddress(r.URL.Path),
|
2022-01-20 18:22:10 +01:00
|
|
|
Data: &taxonomyRenderData{
|
|
|
|
taxonomy: tax,
|
|
|
|
valueGroups: groupStrings(allValues),
|
2021-03-22 08:20:56 +01:00
|
|
|
},
|
|
|
|
})
|
2020-11-22 09:11:57 +01:00
|
|
|
}
|
2021-07-17 09:33:44 +02:00
|
|
|
|
|
|
|
func (a *goBlog) serveTaxonomyValue(w http.ResponseWriter, r *http.Request) {
|
2021-12-20 14:00:11 +01:00
|
|
|
_, bc := a.getBlog(r)
|
2021-07-17 09:33:44 +02:00
|
|
|
tax := r.Context().Value(taxonomyContextKey).(*configTaxonomy)
|
|
|
|
taxValueParam := chi.URLParam(r, "taxValue")
|
|
|
|
if taxValueParam == "" {
|
|
|
|
a.serve404(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get value from DB
|
2022-08-09 17:25:22 +02:00
|
|
|
row, err := a.db.QueryRow(
|
2021-07-17 09:33:44 +02:00
|
|
|
"select value from post_parameters where parameter = @tax and urlize(value) = @taxValue limit 1",
|
|
|
|
sql.Named("tax", tax.Name), sql.Named("taxValue", taxValueParam),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var taxValue string
|
|
|
|
err = row.Scan(&taxValue)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
a.serve404(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Serve index
|
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2021-12-20 14:00:11 +01:00
|
|
|
path: bc.getRelativePath(fmt.Sprintf("/%s/%s", tax.Name, taxValueParam)),
|
2021-07-17 09:33:44 +02:00
|
|
|
tax: tax,
|
|
|
|
taxValue: taxValue,
|
|
|
|
})))
|
|
|
|
}
|