package web import ( "encoding/json" "fmt" "io/ioutil" "net/http" "time" "github.com/rs/zerolog/log" "code.chrissexton.org/cws/cabinet/entry" "github.com/gorilla/mux" ) 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 } func (web *Web) editEntry(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) slug := vars["slug"] dec := json.NewDecoder(r.Body) req := struct { Content string }{} err := dec.Decode(&req) if err != nil { w.WriteHeader(500) fmt.Fprint(w, err) return } newEntry := entry.NewFromMd(web.db, req.Content) oldEntry, err := entry.GetBySlug(web.db, slug) if err != nil { w.WriteHeader(500) fmt.Fprint(w, err) return } oldEntry.Content = newEntry.Content oldEntry.Title = newEntry.Title oldEntry.Slug = newEntry.UniqueSlug() oldEntry.Tags = newEntry.Tags oldEntry.Updated = time.Now() err = oldEntry.Update() if err != nil { web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, oldEntry) } func (web *Web) newAdocEntry(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { web.writeJSON(w, 500, err) return } newEntry := entry.NewFromAdoc(web.db, string(body)) err = newEntry.Create() if err != nil { web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, newEntry) } func (web *Web) newMarkdownEntry(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { log.Error().Err(err).Msgf("could not read message body") web.writeJSON(w, 500, err) return } newEntry := entry.NewFromMd(web.db, string(body)) err = newEntry.Create() if err != nil { log.Error().Err(err).Msgf("could not create entry") web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, newEntry) } func (web *Web) newEntry(w http.ResponseWriter, r *http.Request) { log.Debug(). Str("content-type", r.Header.Get("Content-Type")). Msgf("newEntry") dec := json.NewDecoder(r.Body) newEntry := entry.New(web.db) err := dec.Decode(&newEntry) if err != nil { log.Error().Err(err).Msgf("could not decode entry") web.writeJSON(w, 500, err) return } err = newEntry.Create() if err != nil { log.Error().Err(err).Msgf("could not create raw entry") web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, newEntry) } func (web *Web) allEntries(w http.ResponseWriter, r *http.Request) { query := "" tags := []string{} if !web.AuthCheck(r) { tags = append(tags, "public") } items, ok := r.URL.Query()["query"] if ok { query = items[0] } entries, err := entry.SearchByTag(web.db, query, tags) if err != nil { log.Error().Msgf("Error querying: %w", err) web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, entries) } 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 { web.writeJSON(w, 500, err) return } if !web.AuthCheck(r) && !entry.HasTag("public") { web.writeJSON(w, 401, "not authorized") return } web.writeJSON(w, 200, entry) } func (web *Web) removeEntry(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) slug := vars["slug"] err := entry.RemoveBySlug(web.db, slug) if err != nil { log.Error().Msgf("Error deleting: %s", err) web.writeJSON(w, 500, err) return } web.writeJSON(w, 200, "success") }