Each Inventory entry can hold its plans' prices: the plan, the price, what it's per, when it was checked and where. Comparison and alternative pages, and new sections, state a competitor's price only as entered, with the month it was checked; a missing one is left out and listed as a to-do. A price checked more than six months ago is flagged on the entry and adds a to-do to every page that names the competitor. Prices go out and come back with the Inventory's YAML. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
4.3 KiB
Go
142 lines
4.3 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/inventory"
|
|
"supermarket/internal/seo"
|
|
"supermarket/internal/sites"
|
|
)
|
|
|
|
// Adding a section to a page that's already on the site (Sites → Sections
|
|
// and pages → Add a section).
|
|
|
|
// pageNow reads a page's file from the site's working copy, as published.
|
|
func (s *Server) pageNow(ctx context.Context, cur siteRepo, path string) (raw []byte, title string, err error) {
|
|
ap := s.applier(seoSite{Index: cur.Index, URL: cur.URL})
|
|
wc, _, _, err := ap.WorkingCopy(ctx)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer wc.Release()
|
|
file, err := wc.FindContent(path)
|
|
if err != nil {
|
|
return nil, "", errors.New("the site has no page at " + path)
|
|
}
|
|
raw, err = os.ReadFile(filepath.Join(wc.Dir, filepath.FromSlash(file)))
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return raw, wc.FrontMatterValue(file, "title"), nil
|
|
}
|
|
|
|
// composeSection writes the section a site page of KindSection adds.
|
|
func (s *Server) composeSection(ctx context.Context, cur siteRepo, p *sites.SitePage) error {
|
|
client, prof, err := s.draftClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if prof == nil || !prof.IsSet() {
|
|
return errors.New("fill in the company profile under Settings → Profile first")
|
|
}
|
|
cat, err := s.catalog(ctx, cur, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
raw, title, err := s.pageNow(ctx, cur, p.Path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
existing, _, err := sites.PageSections(raw)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
in := sites.SectionInputs{Catalog: cat, PagePath: p.Path, PageTitle: title, Existing: existing, Type: p.Source.Type,
|
|
At: p.Source.At, Notes: p.Notes, SiteURL: cur.URL}
|
|
for _, st := range s.seoSites() {
|
|
if st.URL == cur.URL && st.Property != "" {
|
|
in.Searches, _ = seo.PageSearches(s.DB, s.scope(st), p.Path, 10)
|
|
}
|
|
}
|
|
if entries, err := inventory.List(s.DB, inventory.Competitor); err == nil {
|
|
for i := range entries {
|
|
if len(entries[i].Prices) > 0 {
|
|
in.Competitors = append(in.Competitors, &entries[i])
|
|
}
|
|
}
|
|
}
|
|
out, err := (&sites.Composer{AI: client, Profile: prof}).ComposeSection(ctx, in)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.Title, p.Needs, p.Sections = or(title, p.Path), out.Needs, out.Sections
|
|
return s.checkPage(cat, p, s.campaignOf(p))
|
|
}
|
|
|
|
// sitesAddPage is the form: which section, where, and what it's for.
|
|
func (s *Server) sitesAddPage(w http.ResponseWriter, r *http.Request) {
|
|
s.renderSitesAdd(w, r, "")
|
|
}
|
|
|
|
func (s *Server) renderSitesAdd(w http.ResponseWriter, r *http.Request, errMsg string) {
|
|
_, cur := s.currentSite(r)
|
|
path := sites.CleanPath(r.FormValue("page"))
|
|
data := map[string]any{"Current": cur, "Path": path, "Type": r.FormValue("type"), "Notes": r.FormValue("notes")}
|
|
if cur == nil || cur.Repo == "" {
|
|
errMsg = or(errMsg, "Set this website's Hugo repository in Settings → Profile first.")
|
|
} else {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
|
|
defer cancel()
|
|
cat, err := s.catalog(ctx, *cur, false)
|
|
if err != nil {
|
|
errMsg = or(errMsg, err.Error())
|
|
} else {
|
|
data["Types"] = cat.Types
|
|
for _, pg := range cat.Pages {
|
|
if pg.Path == path {
|
|
data["Page"] = pg
|
|
}
|
|
}
|
|
if data["Page"] == nil {
|
|
errMsg = or(errMsg, "The site has no page built from sections at "+path+".")
|
|
}
|
|
}
|
|
}
|
|
s.render(w, r, "sites-add", page{Title: "Add a section", Nav: "sites/site", Error: errMsg, Data: data})
|
|
}
|
|
|
|
// sitesAdd writes the section and opens it for review, like a new page.
|
|
func (s *Server) sitesAdd(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
_, cur := s.currentSite(r)
|
|
if cur == nil || cur.Repo == "" {
|
|
s.renderSitesAdd(w, r, "")
|
|
return
|
|
}
|
|
at, _ := strconv.Atoi(r.PostFormValue("at"))
|
|
p := &sites.SitePage{Site: cur.URL, Repo: cur.Repo, Kind: sites.KindSection, Path: sites.CleanPath(r.PostFormValue("page")),
|
|
Notes: strings.TrimSpace(r.PostFormValue("notes"))}
|
|
p.Source.Type, p.Source.At = r.PostFormValue("type"), at
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
|
defer cancel()
|
|
if err := s.composeSection(ctx, *cur, p); err != nil {
|
|
s.renderSitesAdd(w, r, err.Error())
|
|
return
|
|
}
|
|
if err := sites.SavePage(s.DB, p); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/sites/pages/"+strconv.FormatInt(p.ID, 10), http.StatusSeeOther)
|
|
}
|