1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
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 esses []models.EngagementStrategy
for _, op := range ops {
es, err := app.engagement_strategies.GetForOperation(op.ID)
// TODO: Check what kind of error this is, don't just continue
if err != nil {
continue
}
esses = append(esses, es)
}
files := []string{
"./ui/html/base.tmpl.html",
"./ui/html/pages/operations/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
}
data := operationsTemplateData{
Operations: ops,
}
err = ts.ExecuteTemplate(w, "base", data)
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
}
}
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)
}
|