2019-11-03 20:01:13 +00:00
|
|
|
package web
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-11-08 01:54:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2019-11-03 20:01:13 +00:00
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
"code.chrissexton.org/cws/cabinet/entry"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
2019-11-03 20:01:13 +00:00
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
func (web *Web) editEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
slug := vars["slug"]
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
newEntry := entry.New(web.db)
|
|
|
|
err := dec.Decode(&newEntry)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldEntry, err := entry.GetBySlug(web.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-08 01:54:00 +00:00
|
|
|
oldEntry.Content = newEntry.Content
|
|
|
|
oldEntry.Tags = newEntry.Tags
|
|
|
|
oldEntry.Updated = time.Now()
|
|
|
|
|
|
|
|
err = oldEntry.Update()
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 01:54:00 +00:00
|
|
|
resp, err := json.Marshal(oldEntry)
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 01:54:00 +00:00
|
|
|
|
|
|
|
log.Debug().Interface("oldEntry", oldEntry).Msg("Got a PUT")
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
fmt.Fprint(w, string(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (web *Web) newEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
newEntry := entry.New(web.db)
|
|
|
|
err := dec.Decode(&newEntry)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = newEntry.Create()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp, err := json.Marshal(newEntry)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
fmt.Fprint(w, string(resp))
|
2019-11-03 20:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
func (web *Web) allEntries(w http.ResponseWriter, r *http.Request) {
|
2019-11-08 04:39:16 +00:00
|
|
|
query := ""
|
|
|
|
items, ok := r.URL.Query()["query"]
|
|
|
|
if ok {
|
|
|
|
query = items[0]
|
|
|
|
}
|
2019-11-08 01:54:00 +00:00
|
|
|
entries, err := entry.Search(web.db, query)
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(entries)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
fmt.Fprint(w, string(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (web *Web) getEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
slug := vars["slug"]
|
|
|
|
|
|
|
|
entry, err := entry.GetBySlug(web.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-03 20:01:13 +00:00
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
resp, err := json.Marshal(entry)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
fmt.Fprint(w, string(resp))
|
2019-11-03 20:01:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
func (web *Web) removeEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
slug := vars["slug"]
|
2019-11-03 20:01:13 +00:00
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
err := entry.RemoveBySlug(web.db, slug)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
2019-11-03 20:01:13 +00:00
|
|
|
}
|