memos/api/memo.go

115 lines
2.3 KiB
Go
Raw Normal View History

2021-12-08 18:43:14 +03:00
package api
import (
"encoding/json"
2021-12-09 17:02:57 +03:00
"memos/api/e"
2021-12-08 18:43:14 +03:00
"memos/store"
"net/http"
"github.com/gorilla/mux"
)
func handleGetMyMemos(w http.ResponseWriter, r *http.Request) {
2021-12-10 08:41:17 +03:00
userId, _ := GetUserIdInSession(r)
2021-12-09 17:02:57 +03:00
urlParams := r.URL.Query()
deleted := urlParams.Get("deleted")
onlyDeletedFlag := deleted == "true"
2021-12-08 18:43:14 +03:00
2021-12-09 17:02:57 +03:00
memos, err := store.GetMemosByUserId(userId, onlyDeletedFlag)
2021-12-08 18:43:14 +03:00
if err != nil {
2021-12-09 17:02:57 +03:00
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
2021-12-08 18:43:14 +03:00
return
}
2021-12-09 17:02:57 +03:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: memos,
})
2021-12-08 18:43:14 +03:00
}
type CreateMemo struct {
Content string `json:"content"`
}
func handleCreateMemo(w http.ResponseWriter, r *http.Request) {
2021-12-10 08:41:17 +03:00
userId, _ := GetUserIdInSession(r)
2021-12-08 18:43:14 +03:00
2021-12-09 17:02:57 +03:00
createMemo := CreateMemo{}
2021-12-08 18:43:14 +03:00
err := json.NewDecoder(r.Body).Decode(&createMemo)
if err != nil {
2021-12-09 17:02:57 +03:00
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
2021-12-08 18:43:14 +03:00
return
}
memo, err := store.CreateNewMemo(createMemo.Content, userId)
if err != nil {
2021-12-09 17:02:57 +03:00
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
2021-12-08 18:43:14 +03:00
return
}
2021-12-09 17:02:57 +03:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: memo,
})
2021-12-08 18:43:14 +03:00
}
func handleUpdateMemo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
memoId := vars["id"]
2021-12-09 17:02:57 +03:00
memoPatch := store.MemoPatch{}
err := json.NewDecoder(r.Body).Decode(&memoPatch)
2021-12-08 18:43:14 +03:00
2021-12-09 17:02:57 +03:00
if err != nil {
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
return
}
memo, err := store.UpdateMemo(memoId, &memoPatch)
2021-12-08 18:43:14 +03:00
if err != nil {
2021-12-09 17:02:57 +03:00
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
2021-12-08 18:43:14 +03:00
return
}
2021-12-09 17:02:57 +03:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: memo,
})
}
func handleDeleteMemo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
memoId := vars["id"]
_, err := store.DeleteMemo(memoId)
2021-12-08 18:43:14 +03:00
if err != nil {
2021-12-09 17:02:57 +03:00
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
2021-12-08 18:43:14 +03:00
return
}
2021-12-09 17:02:57 +03:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: nil,
})
2021-12-08 18:43:14 +03:00
}
func RegisterMemoRoutes(r *mux.Router) {
memoRouter := r.PathPrefix("/api/memo").Subrouter()
2021-12-10 08:41:17 +03:00
memoRouter.Use(AuthCheckerMiddleWare)
2021-12-08 18:43:14 +03:00
memoRouter.HandleFunc("/all", handleGetMyMemos).Methods("GET")
memoRouter.HandleFunc("/", handleCreateMemo).Methods("PUT")
memoRouter.HandleFunc("/{id}", handleUpdateMemo).Methods("PATCH")
2021-12-09 17:02:57 +03:00
memoRouter.HandleFunc("/{id}", handleDeleteMemo).Methods("DELETE")
2021-12-08 18:43:14 +03:00
}