|
|
|
@ -10,39 +10,83 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func writeJsonOrError[K any](logger *log.Logger, object K, w http.ResponseWriter) {
|
|
|
|
|
jsonObject, err := json.Marshal(object)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Warn("could not marshal json object", "err", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(jsonObject)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type StackHandler struct {
|
|
|
|
|
logger *log.Logger
|
|
|
|
|
stackModel models.ListModel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *StackHandler) getAllStacks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
func (h *StackHandler) withUserID(
|
|
|
|
|
fn func(userID uuid.UUID, w http.ResponseWriter, r *http.Request),
|
|
|
|
|
) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
userId, err := middleware.GetUserID(ctx)
|
|
|
|
|
userID, err := middleware.GetUserID(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Warn("could not get users in get all stacks", "err", err)
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lists, err := h.stackModel.List(ctx, userId)
|
|
|
|
|
fn(userID, w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *StackHandler) getAllStacks(userID uuid.UUID, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
lists, err := h.stackModel.List(ctx, userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Warn("could not get stacks", "err", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonLists, err := json.Marshal(lists)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Warn("could not marshal json lists", "err", err)
|
|
|
|
|
writeJsonOrError(h.logger, lists, w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *StackHandler) getStackItems(userID uuid.UUID, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
listID := r.PathValue("listID")
|
|
|
|
|
if len(listID) == 0 {
|
|
|
|
|
h.logger.Warn("listID is not present in path")
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(jsonLists)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
uuidListID, err := uuid.Parse(listID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Warn("could not parse list id uuid", "err", err)
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: must check for permission here.
|
|
|
|
|
|
|
|
|
|
lists, err := h.stackModel.ListItems(ctx, uuidListID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Warn("could not get list items", "err", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeJsonOrError(h.logger, lists, w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *StackHandler) CreateRoutes(r chi.Router) {
|
|
|
|
@ -52,7 +96,8 @@ func (h *StackHandler) CreateRoutes(r chi.Router) {
|
|
|
|
|
r.Use(middleware.ProtectedRoute)
|
|
|
|
|
r.Use(middleware.SetJson)
|
|
|
|
|
|
|
|
|
|
r.Get("/", h.getAllStacks)
|
|
|
|
|
r.Get("/", h.withUserID(h.getAllStacks))
|
|
|
|
|
r.Get("/{listID}", h.withUserID(h.getStackItems))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|