218 lines
6.9 KiB
Go
218 lines
6.9 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"archi_folio/internal/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *Server) adminUpdateContent(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if err := parseAdminForm(w, r, maxFormBytes); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", "content form failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
current, err := s.store.SiteContent(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", "content could not be loaded")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
content := store.SiteContent{
|
||
|
|
HeroTitle: strings.TrimSpace(r.FormValue("hero_title")),
|
||
|
|
HeroSubtitle: strings.TrimSpace(r.FormValue("hero_subtitle")),
|
||
|
|
IntroTitle: strings.TrimSpace(r.FormValue("intro_title")),
|
||
|
|
IntroText: strings.TrimSpace(r.FormValue("intro_text")),
|
||
|
|
AboutName: strings.TrimSpace(r.FormValue("about_name")),
|
||
|
|
AboutRole: strings.TrimSpace(r.FormValue("about_role")),
|
||
|
|
AboutBio: strings.TrimSpace(r.FormValue("about_bio")),
|
||
|
|
Email: current.Email,
|
||
|
|
Phone: current.Phone,
|
||
|
|
Location: current.Location,
|
||
|
|
HeroImage: r.FormValue("hero_image_current"),
|
||
|
|
AboutImage: r.FormValue("about_image_current"),
|
||
|
|
}
|
||
|
|
if err := validateContent(content); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if path, ok, err := s.saveUpload(r, "hero_image"); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", "hero image "+err.Error())
|
||
|
|
return
|
||
|
|
} else if ok {
|
||
|
|
content.HeroImage = path
|
||
|
|
}
|
||
|
|
if path, ok, err := s.saveUpload(r, "about_image"); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", "about image "+err.Error())
|
||
|
|
return
|
||
|
|
} else if ok {
|
||
|
|
content.AboutImage = path
|
||
|
|
}
|
||
|
|
if err := s.store.UpdateSiteContent(r.Context(), content); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "main", "content could not be saved")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "main", "content saved")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminUpdateContactDetails(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if err := r.ParseForm(); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "contact-details", "contact details form failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
content, err := s.store.SiteContent(r.Context())
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "contact-details", "contact details could not be loaded")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
content.Email = strings.TrimSpace(r.FormValue("email"))
|
||
|
|
content.Phone = strings.TrimSpace(r.FormValue("phone"))
|
||
|
|
content.Location = strings.TrimSpace(r.FormValue("location"))
|
||
|
|
if err := validateContactDetails(content); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "contact-details", err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := s.store.UpdateSiteContent(r.Context(), content); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "contact-details", "contact details could not be saved")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "contact-details", "contact details saved")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminCreateProject(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if err := parseAdminForm(w, r, maxFormBytes); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "project form failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
cover := "/static/placeholders/project-1.svg"
|
||
|
|
if path, ok, err := s.saveUpload(r, "cover_image"); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "cover upload "+err.Error())
|
||
|
|
return
|
||
|
|
} else if ok {
|
||
|
|
cover = path
|
||
|
|
}
|
||
|
|
slug := strings.TrimSpace(r.FormValue("slug"))
|
||
|
|
if slug == "" {
|
||
|
|
slug = store.SlugFromTitle(r.FormValue("title"))
|
||
|
|
} else {
|
||
|
|
slug = store.SlugFromTitle(slug)
|
||
|
|
}
|
||
|
|
project := store.Project{
|
||
|
|
Slug: slug,
|
||
|
|
Title: strings.TrimSpace(r.FormValue("title")),
|
||
|
|
Location: strings.TrimSpace(r.FormValue("location")),
|
||
|
|
Year: strings.TrimSpace(r.FormValue("year")),
|
||
|
|
Category: strings.TrimSpace(r.FormValue("category")),
|
||
|
|
Description: strings.TrimSpace(r.FormValue("description")),
|
||
|
|
CoverImage: cover,
|
||
|
|
Featured: r.FormValue("featured") == "on",
|
||
|
|
}
|
||
|
|
if err := validateProject(project); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
id, err := s.store.CreateProject(r.Context(), project)
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "project could not be created")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
_ = s.store.AddProjectImage(r.Context(), id, cover, r.FormValue("title"))
|
||
|
|
s.redirectAdmin(w, r, "projects", "project created")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminUpdateProject(w http.ResponseWriter, r *http.Request) {
|
||
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||
|
|
if err != nil {
|
||
|
|
http.NotFound(w, r)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := parseAdminForm(w, r, maxFormBytes); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "project form failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
cover := r.FormValue("cover_image_current")
|
||
|
|
if path, ok, err := s.saveUpload(r, "cover_image"); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "cover upload "+err.Error())
|
||
|
|
return
|
||
|
|
} else if ok {
|
||
|
|
cover = path
|
||
|
|
}
|
||
|
|
slug := strings.TrimSpace(r.FormValue("slug"))
|
||
|
|
if slug == "" {
|
||
|
|
slug = store.SlugFromTitle(r.FormValue("title"))
|
||
|
|
} else {
|
||
|
|
slug = store.SlugFromTitle(slug)
|
||
|
|
}
|
||
|
|
project := store.Project{
|
||
|
|
ID: id,
|
||
|
|
Slug: slug,
|
||
|
|
Title: strings.TrimSpace(r.FormValue("title")),
|
||
|
|
Location: strings.TrimSpace(r.FormValue("location")),
|
||
|
|
Year: strings.TrimSpace(r.FormValue("year")),
|
||
|
|
Category: strings.TrimSpace(r.FormValue("category")),
|
||
|
|
Description: strings.TrimSpace(r.FormValue("description")),
|
||
|
|
CoverImage: cover,
|
||
|
|
Featured: r.FormValue("featured") == "on",
|
||
|
|
}
|
||
|
|
if err := validateProject(project); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = s.store.UpdateProject(r.Context(), project)
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "project could not be saved")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "projects", "project saved")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminDeleteProject(w http.ResponseWriter, r *http.Request) {
|
||
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||
|
|
if err == nil {
|
||
|
|
err = s.store.DeleteProject(r.Context(), id)
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "project could not be deleted")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "projects", "project deleted")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminAddProjectImage(w http.ResponseWriter, r *http.Request) {
|
||
|
|
projectID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||
|
|
if err != nil {
|
||
|
|
http.NotFound(w, r)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := parseAdminForm(w, r, maxFormBytes); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "image form failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
path, ok, err := s.saveUpload(r, "image")
|
||
|
|
if err != nil || !ok {
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "image upload "+err.Error())
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "projects", "image upload failed")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := s.store.AddProjectImage(r.Context(), projectID, path, r.FormValue("caption")); err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "image could not be added")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "projects", "image added")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) adminDeleteProjectImage(w http.ResponseWriter, r *http.Request) {
|
||
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||
|
|
if err == nil {
|
||
|
|
err = s.store.DeleteProjectImage(r.Context(), id)
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
s.redirectAdmin(w, r, "projects", "image could not be deleted")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
s.redirectAdmin(w, r, "projects", "image deleted")
|
||
|
|
}
|