package main import ( "errors" "fmt" "html/template" "net/http" "strconv" "github.com/yulqen/ded-go-core/internal/models" ) func (app *application) listOperations(w http.ResponseWriter, r *http.Request) { ops, err := app.operations.ListAll() if err != nil { app.serverError(w, r, err) return } var newOps []models.Operation for _, op := range ops { esses, err := app.engagement_strategies.GetForOperation(op.ID) // TODO: Check what kind of error this is, don't just continue if err != nil { continue } if len(esses) > 0 { op.EngagementStrategies = esses newOps = append(newOps, op) } else { newOps = append(newOps, op) } } app.render(w, r, http.StatusOK, "operations_list.tmpl.html", templateData{ Operations: newOps, }) } func (app *application) listOrganisations(w http.ResponseWriter, r *http.Request) { files := []string{ "./ui/html/base.tmpl.html", "./ui/html/pages/organisations_list.tmpl.html", "./ui/html/partials/nav.tmpl.html", } ts, err := template.ParseFiles(files...) if err != nil { app.serverError(w, r, err) // Use the serverError() helper return } err = ts.ExecuteTemplate(w, "base", nil) if err != nil { app.serverError(w, r, err) // Use the serverError() helper } } func (app *application) home(w http.ResponseWriter, r *http.Request) { // Check if the current request URL path exactly matches "/". If it doesn't, use // the http.NotFound() function to send a 404 response to the client. // Importantly, we then return from the handler. If we don't return the handler // would keep executing. if r.URL.Path != "/" { app.notFound(w) return } organisations, err := app.organisations.Latest() if err != nil { app.serverError(w, r, err) return } for _, organisation := range organisations { fmt.Fprintf(w, "%+v\n", organisation) } // files := []string{ // "./ui/html/base.tmpl.html", // "./ui/html/pages/home.tmpl.html", // "./ui/html/partials/nav.tmpl.html", // } // // ts, err := template.ParseFiles(files...) // if err != nil { // app.serverError(w, r, err) // Use the serverError() helper // return // } // // err = ts.ExecuteTemplate(w, "base", nil) // if err != nil { // app.serverError(w, r, err) // Use the serverError() helper // } } func (app *application) organisationView(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get("id")) if err != nil || id < 1 { app.notFound(w) return } o, err := app.organisations.Get(id) if err != nil { if errors.Is(err, models.ErrNoRecord) { app.notFound(w) } else { app.serverError(w, r, err) } return } fmt.Fprintf(w, "%+v", o) } func (app *application) organisationCreate(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { w.Header().Set("Allow", http.MethodPost) app.clientError(w, http.StatusMethodNotAllowed) return } // Dummy data name := "Test Organisation" id, err := app.organisations.Insert(name) if err != nil { app.serverError(w, r, err) return } http.Redirect(w, r, fmt.Sprintf("/organisation/view?id=%d", id), http.StatusSeeOther) }