Add note title

This commit is contained in:
MrLetsplay 2023-08-24 22:53:38 +02:00
parent 73063316aa
commit 836fab0d27
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg

31
main.go
View File

@ -24,12 +24,13 @@ type config struct {
type note struct {
Id int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
func httpError(err error, statusCode int, message string, w http.ResponseWriter) bool {
if err != nil {
log.Println(err)
log.Println(message, err)
writeError(w, statusCode, message)
return true
}
@ -54,7 +55,7 @@ func writeError(w http.ResponseWriter, statusCode int, message string) {
}
func getNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
res, err := db.Query(`SELECT Id, Content FROM Notes`)
res, err := db.Query(`SELECT Id, Title, Content FROM Notes`)
if httpError(err, http.StatusInternalServerError, "Failed to load notes", w) {
return
}
@ -69,7 +70,7 @@ func getNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
}
var note note
res.Scan(&note.Id, &note.Content)
res.Scan(&note.Id, &note.Title, &note.Content)
notes = append(notes, note)
}
@ -94,19 +95,24 @@ func postNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
return
}
if len(note.Title) == 0 {
writeError(w, http.StatusBadRequest, "Missing note title")
return
}
if len(note.Content) == 0 {
writeError(w, http.StatusBadRequest, "Missing note content")
return
}
stmt, err := db.Prepare(`INSERT INTO Notes(Content) VALUES(?) RETURNING Id`)
stmt, err := db.Prepare(`INSERT INTO Notes(Title, Content) VALUES(?, ?) RETURNING Id`)
if httpError(err, http.StatusInternalServerError, "Failed to store note", w) {
return
}
defer stmt.Close()
res := stmt.QueryRow(note.Content)
res := stmt.QueryRow(note.Title, note.Content)
err = res.Scan(&note.Id)
if httpError(err, http.StatusInternalServerError, "Failed to store note", w) {
return
@ -128,7 +134,7 @@ func getNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
return
}
stmt, err := db.Prepare(`SELECT Content FROM Notes WHERE Id = ?`)
stmt, err := db.Prepare(`SELECT Title, Content FROM Notes WHERE Id = ?`)
if httpError(err, http.StatusInternalServerError, "Failed to get note", w) {
return
}
@ -139,7 +145,7 @@ func getNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
var note note = note{Id: id}
err = res.Scan(&note.Content)
err = res.Scan(&note.Title, &note.Content)
if err == sql.ErrNoRows {
writeError(w, http.StatusNotFound, "Note doesn't exist")
return
@ -175,19 +181,24 @@ func putNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
return
}
if len(note.Title) == 0 {
writeError(w, http.StatusBadRequest, "Missing note title")
return
}
if len(note.Content) == 0 {
writeError(w, http.StatusBadRequest, "Missing note content")
return
}
stmt, err := db.Prepare(`UPDATE Notes SET Content = ? WHERE Id = ?`)
stmt, err := db.Prepare(`UPDATE Notes SET Title = ?, Content = ? WHERE Id = ?`)
if httpError(err, http.StatusInternalServerError, "Failed to update note", w) {
return
}
defer stmt.Close()
res, err := stmt.Exec(note.Content, id)
res, err := stmt.Exec(note.Title, note.Content, id)
if httpError(err, http.StatusInternalServerError, "Failed to update note", w) {
return
}
@ -279,7 +290,7 @@ func main() {
}
log.Println("Creating missing tables")
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS Notes(Id INTEGER PRIMARY KEY AUTOINCREMENT, Content TEXT)`)
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS Notes(Id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT, Content TEXT)`)
if err != nil {
log.Fatalln("Failed to create table:", err)
}