2019-11-03 20:01:13 +00:00
|
|
|
package web
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-11-16 13:49:07 +00:00
|
|
|
"io/ioutil"
|
2019-11-03 20:01:24 +00:00
|
|
|
"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
|
|
|
|
2020-07-23 16:13:58 +00:00
|
|
|
func (web *Web) writeJSON(w http.ResponseWriter, code int, data interface{}) error {
|
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
resp, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
2020-02-12 22:42:03 +00:00
|
|
|
req := struct {
|
|
|
|
Content string
|
|
|
|
}{}
|
|
|
|
err := dec.Decode(&req)
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-02-12 22:42:03 +00:00
|
|
|
newEntry := entry.NewFromMd(web.db, req.Content)
|
2019-11-03 20:01:24 +00:00
|
|
|
|
|
|
|
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
|
2020-02-12 22:42:03 +00:00
|
|
|
oldEntry.Title = newEntry.Title
|
|
|
|
oldEntry.Slug = newEntry.UniqueSlug()
|
2019-11-08 01:54:00 +00:00
|
|
|
oldEntry.Tags = newEntry.Tags
|
|
|
|
oldEntry.Updated = time.Now()
|
|
|
|
|
|
|
|
err = oldEntry.Update()
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, oldEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (web *Web) newAdocEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newEntry := entry.NewFromAdoc(web.db, string(body))
|
|
|
|
err = newEntry.Create()
|
|
|
|
if err != nil {
|
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-08 01:54:00 +00:00
|
|
|
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, newEntry)
|
2019-11-03 20:01:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 13:49:07 +00:00
|
|
|
func (web *Web) newMarkdownEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
log.Error().Err(err).Msgf("could not read message body")
|
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-16 13:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
newEntry := entry.NewFromMd(web.db, string(body))
|
|
|
|
err = newEntry.Create()
|
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
log.Error().Err(err).Msgf("could not create entry")
|
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-16 13:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, newEntry)
|
2019-11-16 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 20:01:24 +00:00
|
|
|
func (web *Web) newEntry(w http.ResponseWriter, r *http.Request) {
|
2020-07-23 16:13:58 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("content-type", r.Header.Get("Content-Type")).
|
|
|
|
Msgf("newEntry")
|
2019-11-03 20:01:24 +00:00
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
newEntry := entry.New(web.db)
|
|
|
|
err := dec.Decode(&newEntry)
|
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
log.Error().Err(err).Msgf("could not decode entry")
|
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = newEntry.Create()
|
|
|
|
if err != nil {
|
2020-07-23 16:13:58 +00:00
|
|
|
log.Error().Err(err).Msgf("could not create raw entry")
|
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-23 16:13:58 +00:00
|
|
|
|
|
|
|
web.writeJSON(w, 200, newEntry)
|
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 := ""
|
2020-03-18 18:07:34 +00:00
|
|
|
tags := []string{}
|
|
|
|
if !web.AuthCheck(r) {
|
|
|
|
tags = append(tags, "public")
|
|
|
|
}
|
2019-11-08 04:39:16 +00:00
|
|
|
items, ok := r.URL.Query()["query"]
|
|
|
|
if ok {
|
|
|
|
query = items[0]
|
|
|
|
}
|
2020-03-18 18:07:34 +00:00
|
|
|
entries, err := entry.SearchByTag(web.db, query, tags)
|
2019-11-03 20:01:24 +00:00
|
|
|
if err != nil {
|
2020-03-18 18:07:34 +00:00
|
|
|
log.Error().Msgf("Error querying: %w", err)
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, entries)
|
2019-11-03 20:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-03 20:01:13 +00:00
|
|
|
|
2020-03-18 18:07:34 +00:00
|
|
|
if !web.AuthCheck(r) && !entry.HasTag("public") {
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 401, "not authorized")
|
2020-03-18 18:07:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, entry)
|
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 {
|
2019-11-08 05:26:02 +00:00
|
|
|
log.Error().Msgf("Error deleting: %s", err)
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 500, err)
|
2019-11-03 20:01:24 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-23 16:13:58 +00:00
|
|
|
web.writeJSON(w, 200, "success")
|
2019-11-03 20:01:13 +00:00
|
|
|
}
|