diff --git a/cmd/cromps/handlers.go b/cmd/cromps/handlers.go index 2f7f9fe..c31c4e8 100644 --- a/cmd/cromps/handlers.go +++ b/cmd/cromps/handlers.go @@ -83,10 +83,58 @@ func AddEntry(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(entry) } +// UpdateEntries handles requests to /etries/update +func UpdateEntries(w http.ResponseWriter, r *http.Request) { + var params db.UpdateEntryParams + + err := json.NewDecoder(r.Body).Decode(¶ms) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + user, err := getUser(r) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + params.UserID = user.UserID + + entry, err := base.UpdateEntry(ctx, params) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + json.NewEncoder(w).Encode(entry) +} + // Entries handles requests to /entries func Entries(w http.ResponseWriter, r *http.Request) { - // Print secret message - fmt.Fprintln(w, "The cake is a lie!") + var params db.GetEntryParams + + err := json.NewDecoder(r.Body).Decode(¶ms) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + user, err := getUser(r) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + params.UserID = user.UserID + + entry, err := base.GetEntry(ctx, params) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + json.NewEncoder(w).Encode(entry) } // SimilarEntries are entries that match some text diff --git a/cmd/cromps/main.go b/cmd/cromps/main.go index e911c37..4cfcea5 100644 --- a/cmd/cromps/main.go +++ b/cmd/cromps/main.go @@ -75,8 +75,7 @@ func main() { http.HandleFunc("/entries/list", checkAuth(logger(ListEntries))) http.HandleFunc("/entries/delete", checkAuth(logger(Entries))) http.HandleFunc("/entries/get", checkAuth(logger(Entries))) - http.HandleFunc("/entries/update", checkAuth(logger(Entries))) + http.HandleFunc("/entries/update", checkAuth(logger(UpdateEntries))) http.HandleFunc("/entries/similar", checkAuth(logger(SimilarEntries))) - log.Fatalln(http.ListenAndServe(":8080", nil)) }