summaryrefslogtreecommitdiffstats
path: root/cmd/web/handlers.go
diff options
context:
space:
mode:
authorYulqen <246857+yulqen@users.noreply.github.com>2024-04-18 13:58:11 +0100
committerGitHub <noreply@github.com>2024-04-18 13:58:11 +0100
commit69017cc225e2754466b8444ca42cb1122208425d (patch)
tree6a7ac8d150b95c1fad9229d124a23737abc17964 /cmd/web/handlers.go
parent530f08071fc1295fabdaedf9724b8cda48780927 (diff)
parentf08ac7b887e0bae0cb67362eec90a575faec07f1 (diff)
Merge pull request #4 from defencedigital/changes
Changes
Diffstat (limited to 'cmd/web/handlers.go')
-rw-r--r--cmd/web/handlers.go144
1 files changed, 144 insertions, 0 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
new file mode 100644
index 0000000..0827ea1
--- /dev/null
+++ b/cmd/web/handlers.go
@@ -0,0 +1,144 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+ "strconv"
+
+ "github.com/defencedigital/ded-web/internal/models"
+)
+
+func (app *application) listPersons(w http.ResponseWriter, r *http.Request) {
+ ps, err := app.persons.ListAll()
+ if err != nil {
+ app.serverError(w, r, err)
+ return
+ }
+
+ app.render(w, r, http.StatusOK, "persons_list.tmpl.html", templateData{
+ Persons: ps,
+ })
+}
+
+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.engagementStrategies.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) {
+
+ latest, err := app.organisations.Latest()
+ if err != nil {
+ app.serverError(w, r, err)
+ return
+ }
+
+ // We are putting latest in the Organisations field of templateData and leaving
+ // templateData.Operations as the nil value because we don't need Operations in this
+ // page, of course.
+ app.render(w, r, http.StatusOK, "organisations_list.tmpl.html", templateData{
+ Organisations: latest,
+ })
+}
+
+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
+ // }
+ app.render(w, r, http.StatusOK, "home.tmpl.html", templateData{})
+}
+
+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)
+}