102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package pkg
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type ContactEntry struct {
|
|
Name string
|
|
Email string
|
|
Phone string
|
|
}
|
|
|
|
type ContactsIndexPage struct {
|
|
Title string
|
|
Contacts []*ContactEntry
|
|
}
|
|
|
|
func ContactsHandler() http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
conn := getConn(r)
|
|
company := getCompany(r)
|
|
if r.Method == "POST" {
|
|
r.ParseForm()
|
|
page := &NewContactPage{
|
|
BusinessName: r.FormValue("business_name"),
|
|
VATIN: r.FormValue("vatin"),
|
|
TradeName: r.FormValue("trade_name"),
|
|
Phone: r.FormValue("phone"),
|
|
Email: r.FormValue("email"),
|
|
Web: r.FormValue("web"),
|
|
Address: r.FormValue("address"),
|
|
City: r.FormValue("city"),
|
|
Province: r.FormValue("province"),
|
|
PostalCode: r.FormValue("postal_code"),
|
|
CountryCode: r.FormValue("country"),
|
|
}
|
|
conn.MustExec(r.Context(), "insert into contact (company_id, business_name, vatin, trade_name, phone, email, web, address, province, city, postal_code, country_code) values ($1, $2, ($12 || $3)::vatin, $4, parse_packed_phone_number($5, $12), $6, $7, $8, $9, $10, $11, $12)", company.Id, page.BusinessName, page.VATIN, page.TradeName, page.Phone, page.Email, page.Web, page.Address, page.City, page.Province, page.PostalCode, page.CountryCode)
|
|
http.Redirect(w, r, "/company/"+company.Slug+"/contacts", http.StatusSeeOther)
|
|
} else {
|
|
locale := getLocale(r)
|
|
page := &ContactsIndexPage{
|
|
Title: pgettext("title", "Customers", locale),
|
|
Contacts: mustGetContactEntries(r.Context(), conn, company),
|
|
}
|
|
mustRenderAppTemplate(w, r, "contacts-index.html", page)
|
|
}
|
|
})
|
|
}
|
|
|
|
func mustGetContactEntries(ctx context.Context, conn *Conn, company *Company) []*ContactEntry {
|
|
rows, err := conn.Query(ctx, "select business_name, email, phone from contact where company_id = $1 order by business_name", company.Id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var entries []*ContactEntry
|
|
for rows.Next() {
|
|
entry := &ContactEntry{}
|
|
err = rows.Scan(&entry.Name, &entry.Email, &entry.Phone)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
if rows.Err() != nil {
|
|
panic(rows.Err())
|
|
}
|
|
|
|
return entries
|
|
}
|
|
|
|
type NewContactPage struct {
|
|
Title string
|
|
BusinessName string
|
|
VATIN string
|
|
TradeName string
|
|
Phone string
|
|
Email string
|
|
Web string
|
|
Address string
|
|
City string
|
|
Province string
|
|
PostalCode string
|
|
CountryCode string
|
|
Countries []CountryOption
|
|
}
|
|
|
|
func NewContactHandler() http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
locale := getLocale(r)
|
|
conn := getConn(r)
|
|
page := &NewContactPage{
|
|
Title: pgettext("title", "New Contact", locale),
|
|
CountryCode: "ES",
|
|
Countries: mustGetCountryOptions(r.Context(), conn, locale),
|
|
}
|
|
mustRenderAppTemplate(w, r, "contacts-new.html", page)
|
|
})
|
|
}
|