Compare commits
2 Commits
main
...
standard-a
Author | SHA1 | Date | |
---|---|---|---|
f5e65524aa | |||
390a216260 |
@ -105,6 +105,29 @@ func (m ListModel) List(ctx context.Context, userId uuid.UUID) ([]ListWithItems,
|
|||||||
return lists, err
|
return lists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImageWithSchema struct {
|
||||||
|
model.ImageLists
|
||||||
|
|
||||||
|
Items []model.ImageSchemaItems
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m ListModel) ListItems(ctx context.Context, listID uuid.UUID) ([]ImageWithSchema, error) {
|
||||||
|
getListItems := SELECT(
|
||||||
|
ImageLists.AllColumns,
|
||||||
|
ImageSchemaItems.AllColumns,
|
||||||
|
).
|
||||||
|
FROM(
|
||||||
|
ImageLists.
|
||||||
|
INNER_JOIN(ImageSchemaItems, ImageSchemaItems.ImageID.EQ(ImageLists.ImageID)),
|
||||||
|
).
|
||||||
|
WHERE(ImageLists.ListID.EQ(UUID(listID)))
|
||||||
|
|
||||||
|
listItems := make([]ImageWithSchema, 0)
|
||||||
|
err := getListItems.QueryContext(ctx, m.dbPool, &listItems)
|
||||||
|
|
||||||
|
return listItems, err
|
||||||
|
}
|
||||||
|
|
||||||
type IDValue struct {
|
type IDValue struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
|
@ -10,39 +10,83 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/go-chi/chi/v5"
|
"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 {
|
type StackHandler struct {
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
stackModel models.ListModel
|
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)
|
||||||
|
if err != nil {
|
||||||
|
h.logger.Warn("could not get users in get all stacks", "err", err)
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fn(userID, w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *StackHandler) getAllStacks(userID uuid.UUID, w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
userId, err := middleware.GetUserID(ctx)
|
lists, err := h.stackModel.List(ctx, userID)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logger.Warn("could not get stacks", "err", err)
|
h.logger.Warn("could not get stacks", "err", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonLists, err := json.Marshal(lists)
|
writeJsonOrError(h.logger, lists, w)
|
||||||
if err != nil {
|
}
|
||||||
h.logger.Warn("could not marshal json lists", "err", err)
|
|
||||||
|
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)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(jsonLists)
|
uuidListID, err := uuid.Parse(listID)
|
||||||
w.WriteHeader(http.StatusOK)
|
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) {
|
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.ProtectedRoute)
|
||||||
r.Use(middleware.SetJson)
|
r.Use(middleware.SetJson)
|
||||||
|
|
||||||
r.Get("/", h.getAllStacks)
|
r.Get("/", h.withUserID(h.getAllStacks))
|
||||||
|
r.Get("/{listID}", h.withUserID(h.getStackItems))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user