153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
func (a *App) renderTemplate(w http.ResponseWriter, path string, data any) {
|
|
tmpl, err := template.ParseFiles(path)
|
|
if err != nil {
|
|
http.Error(w, "template parse failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if err := tmpl.Execute(w, data); err != nil {
|
|
http.Error(w, "template render failed", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func (a *App) handleDashboard(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
settings, err := a.getSettings()
|
|
if err != nil {
|
|
http.Error(w, "settings read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
plans, err := a.listPlans()
|
|
if err != nil {
|
|
http.Error(w, "plans read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := DashboardPage{
|
|
BasePage: BasePage{
|
|
ActivePath: "/",
|
|
Error: r.URL.Query().Get("error"),
|
|
Info: r.URL.Query().Get("info"),
|
|
},
|
|
Settings: settings,
|
|
CurrentMonth: monthNames[settings.CurrentMonth-1],
|
|
TodayTasks: buildTasksForDay(plans, settings.CurrentMonth, settings.CurrentDay),
|
|
Calendar: buildCalendar(plans, settings.CurrentMonth, settings.CurrentDay, settings.DaysPerMonth, 14),
|
|
PlanningCount: len(plans),
|
|
}
|
|
a.renderTemplate(w, "templates/dashboard.html", data)
|
|
}
|
|
|
|
func (a *App) handleFieldsPage(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
fields, err := a.listFields()
|
|
if err != nil {
|
|
http.Error(w, "fields read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := FieldsPage{
|
|
BasePage: BasePage{
|
|
ActivePath: "/fields",
|
|
Error: r.URL.Query().Get("error"),
|
|
Info: r.URL.Query().Get("info"),
|
|
},
|
|
Fields: fields,
|
|
Groups: buildFieldGroups(fields),
|
|
}
|
|
a.renderTemplate(w, "templates/fields.html", data)
|
|
}
|
|
|
|
func (a *App) handleCropsPage(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
crops, err := a.listCrops()
|
|
if err != nil {
|
|
http.Error(w, "crops read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := CropsPage{
|
|
BasePage: BasePage{
|
|
ActivePath: "/crops",
|
|
Error: r.URL.Query().Get("error"),
|
|
Info: r.URL.Query().Get("info"),
|
|
},
|
|
Crops: crops,
|
|
}
|
|
a.renderTemplate(w, "templates/crops.html", data)
|
|
}
|
|
|
|
func (a *App) handlePlanningPage(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
settings, err := a.getSettings()
|
|
if err != nil {
|
|
http.Error(w, "settings read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
crops, err := a.listCrops()
|
|
if err != nil {
|
|
http.Error(w, "crops read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
fields, err := a.listFields()
|
|
if err != nil {
|
|
http.Error(w, "fields read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
plans, err := a.listPlans()
|
|
if err != nil {
|
|
http.Error(w, "plans read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := PlanningPage{
|
|
BasePage: BasePage{
|
|
ActivePath: "/planning",
|
|
Error: r.URL.Query().Get("error"),
|
|
Info: r.URL.Query().Get("info"),
|
|
},
|
|
Settings: settings,
|
|
Months: monthOptions(),
|
|
Crops: crops,
|
|
Plans: plans,
|
|
PlanningTargets: buildPlanningTargets(fields),
|
|
}
|
|
a.renderTemplate(w, "templates/planning.html", data)
|
|
}
|
|
|
|
func (a *App) handleGeneralPage(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
settings, err := a.getSettings()
|
|
if err != nil {
|
|
http.Error(w, "settings read failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := GeneralPage{
|
|
BasePage: BasePage{
|
|
ActivePath: "/general",
|
|
Error: r.URL.Query().Get("error"),
|
|
Info: r.URL.Query().Get("info"),
|
|
},
|
|
Settings: settings,
|
|
Months: monthOptions(),
|
|
}
|
|
a.renderTemplate(w, "templates/general.html", data)
|
|
}
|