-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.go
192 lines (160 loc) · 5.12 KB
/
notes.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
type Note struct {
ID int
UserID int
Title string
Content string
ContentHTML template.HTML
CreatedAt string
}
//Functions
// noteRouter returns a router with the handlers for the "/notes" path
func (app *App) noteRouter() http.Handler {
router := chi.NewRouter()
//Routes
router.Get("/", app.handleGetAllNotes)
router.Get("/{id}", app.handleGetNoteByID)
router.Post("/", app.handleNewNote)
router.Post("/{id}", app.handleUpdateNote)
router.Delete("/{id}", app.handleDeleteNote)
return router
}
// getAllNotes gets all notes from the db connection and returns them as a list of notes
func (app *App) getAllNotes(userID int) []Note {
var notes []Note
rows, err := app.db.Query("SELECT * FROM notes WHERE user_id = $1", userID)
if err != nil {
fmt.Println(err.Error())
}
for rows.Next() {
var note Note
rows.Scan(¬e.ID, ¬e.UserID, ¬e.Title, ¬e.Content, ¬e.CreatedAt)
notes = append(notes, note)
}
return notes
}
// getNoteByID takes an id as an argument and queries the db connection for a Note matching that id
// and then returns a Note object
func (app *App) getNoteByID(id, userID int) Note {
var note Note
row := app.db.QueryRow("SELECT * FROM notes WHERE id = $1 AND user_id = $2", id, userID)
err := row.Scan(¬e.ID, ¬e.UserID, ¬e.Title, ¬e.Content, ¬e.CreatedAt)
if err != nil {
fmt.Println(err)
}
return note
}
// postNote takes a user id, title and content as arguments and inserts a new note into the database
// and then returns a Note object
func (app *App) postNote(userID int, title, content string) Note {
var note Note
row, err := app.db.Query("INSERT INTO notes(user_id, title, content) VALUES($1, $2, $3) RETURNING *", userID, title, content)
if err != nil {
fmt.Println(err.Error())
}
if row.Next() {
err = row.Scan(¬e.ID, ¬e.UserID, ¬e.Title, ¬e.Content, ¬e.CreatedAt)
if err != nil {
fmt.Println(err)
}
}
return note
}
func (app *App) updateNote(id, userID int, title, content string) {
row := app.db.QueryRow("UPDATE notes SET title = $1, content = $2 WHERE id = $3 AND user_id = $4", title, content, id, userID)
err := row.Scan()
if err != nil {
fmt.Println(err.Error())
}
}
func (app *App) deleteNote(id, userID int) {
row := app.db.QueryRow("DELETE FROM notes WHERE id = $1 AND user_id = $2 RETURNING id", id, userID)
err := row.Scan()
if err != nil {
fmt.Println(err.Error())
}
}
//Handlers
// handleGetAllNotes calls the queryAllNotes function and renders the returned notes to the ResponseWriter
func (app *App) handleGetAllNotes(w http.ResponseWriter, r *http.Request) {
userID := getUserIDFromContext(r)
if userID == 0 {
w.WriteHeader(http.StatusUnauthorized)
return
}
notes := app.getAllNotes(userID)
app.templates.ExecuteTemplate(w, "notes", notes)
}
// handleGetNoteByID calls the queryNoteByID function and renders the returned note to the ResponseWriter
func (app *App) handleGetNoteByID(w http.ResponseWriter, r *http.Request) {
userID := getUserIDFromContext(r)
if userID == 0 {
w.WriteHeader(http.StatusUnauthorized)
return
}
requestedId := chi.URLParam(r, "id")
id, err := strconv.Atoi(requestedId)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
note := app.getNoteByID(id, userID)
if note.UserID != userID {
app.templates.ExecuteTemplate(w, "error_toast", "Note not found")
return
}
if r.URL.Query().Get("edit") == "true" {
app.templates.ExecuteTemplate(w, "edit_note", note)
} else {
safeHTMLString := mdToHTML(note.Content)
safeHTML := template.HTML(safeHTMLString)
note.ContentHTML = safeHTML
app.templates.ExecuteTemplate(w, "individual_note", note)
}
}
// handleNewNote calls postNote with a default title and content.
// It then redirects the user to the page to edit the new note
func (app *App) handleNewNote(w http.ResponseWriter, r *http.Request) {
userID := getUserIDFromContext(r)
title := "New Note"
content := "Lorem ipsum..."
note := app.postNote(userID, title, content)
redirectURL := fmt.Sprintf("/notes/%d", note.ID)
w.Header().Add("HX-Redirect", redirectURL)
w.WriteHeader(http.StatusOK)
}
// handleUpdateNote gathers the title and content fields from the request form data and calls updateNote with them
// It then redirects the user to the notes page
func (app *App) handleUpdateNote(w http.ResponseWriter, r *http.Request) {
idString := chi.URLParam(r, "id")
id, err := strconv.Atoi(idString)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
userID := getUserIDFromContext(r)
title := r.FormValue("title")
content := r.FormValue("content")
app.updateNote(id, userID, title, content)
w.Header().Add("HX-Redirect", fmt.Sprintf("/notes/%d", id))
w.WriteHeader(http.StatusOK)
}
func (app *App) handleDeleteNote(w http.ResponseWriter, r *http.Request) {
idString := chi.URLParam(r, "id")
id, err := strconv.Atoi(idString)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
userID := getUserIDFromContext(r)
app.deleteNote(id, userID)
w.Header().Add("HX-Redirect", "/notes")
w.WriteHeader(http.StatusOK)
}