230 lines
6.2 KiB
Go
230 lines
6.2 KiB
Go
package pkg
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/jackc/pgx/v4"
|
|
"github.com/julienschmidt/httprouter"
|
|
"html/template"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type ProductEntry struct {
|
|
Slug string
|
|
Name string
|
|
Price string
|
|
}
|
|
|
|
type productsIndexPage struct {
|
|
Products []*ProductEntry
|
|
}
|
|
|
|
func IndexProducts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
conn := getConn(r)
|
|
company := mustGetCompany(r)
|
|
page := &productsIndexPage{
|
|
Products: mustGetProductEntries(r.Context(), conn, company),
|
|
}
|
|
mustRenderAppTemplate(w, r, "products/index.gohtml", page)
|
|
}
|
|
|
|
func GetProductForm(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
locale := getLocale(r)
|
|
conn := getConn(r)
|
|
company := mustGetCompany(r)
|
|
form := newProductForm(r.Context(), conn, locale, company)
|
|
slug := params[0].Value
|
|
if slug == "new" {
|
|
w.WriteHeader(http.StatusOK)
|
|
mustRenderNewProductForm(w, r, form)
|
|
return
|
|
}
|
|
var productId int
|
|
err := conn.QueryRow(r.Context(), "select product_id, product.name, product.description, to_price(price, decimal_digits) from product join company using (company_id) join currency using (currency_code) where product.slug = $1", slug).Scan(&productId, form.Name, form.Description, form.Price)
|
|
if err != nil {
|
|
if err == pgx.ErrNoRows {
|
|
http.NotFound(w, r)
|
|
return
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
rows, err := conn.Query(r.Context(), "select tax_id from product_tax where product_id = $1", productId)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
if err := rows.Scan(form.Tax); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
mustRenderEditProductForm(w, r, form)
|
|
}
|
|
|
|
func mustRenderNewProductForm(w http.ResponseWriter, r *http.Request, form *productForm) {
|
|
mustRenderAppTemplate(w, r, "products/new.gohtml", form)
|
|
}
|
|
|
|
func mustRenderEditProductForm(w http.ResponseWriter, r *http.Request, form *productForm) {
|
|
mustRenderAppTemplate(w, r, "products/edit.gohtml", form)
|
|
}
|
|
|
|
func HandleAddProduct(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
conn := getConn(r)
|
|
locale := getLocale(r)
|
|
company := mustGetCompany(r)
|
|
form := newProductForm(r.Context(), conn, locale, company)
|
|
if err := form.Parse(r); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := verifyCsrfTokenValid(r); err != nil {
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
if !form.Validate() {
|
|
mustRenderNewProductForm(w, r, form)
|
|
return
|
|
}
|
|
taxes := mustSliceAtoi(form.Tax.Selected)
|
|
conn.MustExec(r.Context(), "select add_product($1, $2, $3, $4, $5)", company.Id, form.Name, form.Description, form.Price, taxes)
|
|
http.Redirect(w, r, companyURI(company, "/products"), http.StatusSeeOther)
|
|
}
|
|
|
|
func sliceAtoi(s []string) ([]int, error) {
|
|
i := []int{}
|
|
for _, vs := range s {
|
|
vi, err := strconv.Atoi(vs)
|
|
if err != nil {
|
|
return i, err
|
|
}
|
|
i = append(i, vi)
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
func mustSliceAtoi(s []string) []int {
|
|
i, err := sliceAtoi(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
func HandleUpdateProduct(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
|
conn := getConn(r)
|
|
locale := getLocale(r)
|
|
company := mustGetCompany(r)
|
|
form := newProductForm(r.Context(), conn, locale, company)
|
|
if err := form.Parse(r); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := verifyCsrfTokenValid(r); err != nil {
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
if !form.Validate() {
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
mustRenderEditProductForm(w, r, form)
|
|
return
|
|
}
|
|
slug := params[0].Value
|
|
taxes := mustSliceAtoi(form.Tax.Selected)
|
|
if ok := conn.MustGetBool(r.Context(), "select edit_product($1, $2, $3, $4, $5)", slug, form.Name, form.Description, form.Price, taxes); !ok {
|
|
http.NotFound(w, r)
|
|
}
|
|
http.Redirect(w, r, companyURI(company, "/products/"+slug), http.StatusSeeOther)
|
|
}
|
|
|
|
func mustGetProductEntries(ctx context.Context, conn *Conn, company *Company) []*ProductEntry {
|
|
rows, err := conn.Query(ctx, "select product.slug, product.name, to_price(price, decimal_digits) from product join company using (company_id) join currency using (currency_code) where company_id = $1 order by name", company.Id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var entries []*ProductEntry
|
|
for rows.Next() {
|
|
entry := &ProductEntry{}
|
|
err = rows.Scan(&entry.Slug, &entry.Name, &entry.Price)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
if rows.Err() != nil {
|
|
panic(rows.Err())
|
|
}
|
|
|
|
return entries
|
|
}
|
|
|
|
type productForm struct {
|
|
locale *Locale
|
|
company *Company
|
|
Name *InputField
|
|
Description *InputField
|
|
Price *InputField
|
|
Tax *SelectField
|
|
}
|
|
|
|
func newProductForm(ctx context.Context, conn *Conn, locale *Locale, company *Company) *productForm {
|
|
return &productForm{
|
|
locale: locale,
|
|
company: company,
|
|
Name: &InputField{
|
|
Name: "name",
|
|
Label: pgettext("input", "Name", locale),
|
|
Type: "text",
|
|
Required: true,
|
|
},
|
|
Description: &InputField{
|
|
Name: "description",
|
|
Label: pgettext("input", "Description", locale),
|
|
Type: "textarea",
|
|
},
|
|
Price: &InputField{
|
|
Name: "price",
|
|
Label: pgettext("input", "Price", locale),
|
|
Type: "number",
|
|
Required: true,
|
|
Attributes: []template.HTMLAttr{
|
|
`min="0"`,
|
|
template.HTMLAttr(fmt.Sprintf(`step="%v"`, company.MinCents())),
|
|
},
|
|
},
|
|
Tax: &SelectField{
|
|
Name: "tax",
|
|
Label: pgettext("input", "Taxes", locale),
|
|
Multiple: true,
|
|
Options: mustGetTaxOptions(ctx, conn, company),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (form *productForm) Parse(r *http.Request) error {
|
|
if err := r.ParseForm(); err != nil {
|
|
return err
|
|
}
|
|
form.Name.FillValue(r)
|
|
form.Description.FillValue(r)
|
|
form.Price.FillValue(r)
|
|
form.Tax.FillValue(r)
|
|
return nil
|
|
}
|
|
|
|
func (form *productForm) Validate() bool {
|
|
validator := newFormValidator()
|
|
validator.CheckRequiredInput(form.Name, gettext("Name can not be empty.", form.locale))
|
|
if validator.CheckRequiredInput(form.Price, gettext("Price can not be empty.", form.locale)) {
|
|
validator.CheckValidDecimal(form.Price, form.company.MinCents(), math.MaxFloat64, gettext("Price must be a number greater than zero.", form.locale))
|
|
}
|
|
validator.CheckValidSelectOption(form.Tax, gettext("Selected tax is not valid.", form.locale))
|
|
return validator.AllOK()
|
|
}
|