creating stacks using a user request
This commit is contained in:
@@ -3,6 +3,7 @@ package stacks
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"screenmark/screenmark/middleware"
|
||||
@@ -10,7 +11,6 @@ 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) {
|
||||
@@ -30,26 +30,36 @@ type StackHandler struct {
|
||||
stackModel models.ListModel
|
||||
}
|
||||
|
||||
func (h *StackHandler) withUserID(
|
||||
fn func(userID uuid.UUID, w http.ResponseWriter, r *http.Request),
|
||||
func withValidatedPost[K any](
|
||||
fn func(request K, w http.ResponseWriter, r *http.Request),
|
||||
) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
request := new(K)
|
||||
|
||||
userID, err := middleware.GetUserID(ctx)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
h.logger.Warn("could not get users in get all stacks", "err", err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fn(userID, w, r)
|
||||
err = json.Unmarshal(body, request)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fn(*request, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *StackHandler) getAllStacks(userID uuid.UUID, w http.ResponseWriter, r *http.Request) {
|
||||
func (h *StackHandler) getAllStacks(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userID, err := middleware.GetUserID(ctx, h.logger, w)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
lists, err := h.stackModel.List(ctx, userID)
|
||||
if err != nil {
|
||||
h.logger.Warn("could not get stacks", "err", err)
|
||||
@@ -60,26 +70,21 @@ func (h *StackHandler) getAllStacks(userID uuid.UUID, w http.ResponseWriter, r *
|
||||
writeJsonOrError(h.logger, lists, w)
|
||||
}
|
||||
|
||||
func (h *StackHandler) getStackItems(userID uuid.UUID, w http.ResponseWriter, r *http.Request) {
|
||||
func (h *StackHandler) getStackItems(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)
|
||||
_, err := middleware.GetUserID(ctx, h.logger, w)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uuidListID, err := uuid.Parse(listID)
|
||||
listID, err := middleware.GetPathParamID(h.logger, "listID", w, r)
|
||||
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)
|
||||
lists, err := h.stackModel.ListItems(ctx, listID)
|
||||
if err != nil {
|
||||
h.logger.Warn("could not get list items", "err", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
@@ -89,6 +94,38 @@ func (h *StackHandler) getStackItems(userID uuid.UUID, w http.ResponseWriter, r
|
||||
writeJsonOrError(h.logger, lists, w)
|
||||
}
|
||||
|
||||
type EditStack struct {
|
||||
Hello string `json:"hello"`
|
||||
}
|
||||
|
||||
func (h *StackHandler) editStack(req EditStack, w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
type CreateStackBody struct {
|
||||
Title string `json:"title"`
|
||||
|
||||
// We want a regular string because AI will take care of creating these for us.
|
||||
Fields string `json:"fields"`
|
||||
}
|
||||
|
||||
func (h *StackHandler) createStack(body CreateStackBody, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
userID, err := middleware.GetUserID(ctx, h.logger, w)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = h.stackModel.SaveProcessing(ctx, userID, body.Title, body.Fields)
|
||||
if err != nil {
|
||||
h.logger.Warn("could not save processing", "err", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *StackHandler) CreateRoutes(r chi.Router) {
|
||||
h.logger.Info("Mounting stack router")
|
||||
|
||||
@@ -96,8 +133,12 @@ func (h *StackHandler) CreateRoutes(r chi.Router) {
|
||||
r.Use(middleware.ProtectedRoute)
|
||||
r.Use(middleware.SetJson)
|
||||
|
||||
r.Get("/", h.withUserID(h.getAllStacks))
|
||||
r.Get("/{listID}", h.withUserID(h.getStackItems))
|
||||
r.Get("/", h.getAllStacks)
|
||||
r.Get("/{listID}", h.getStackItems)
|
||||
|
||||
r.Post("/", withValidatedPost(h.createStack))
|
||||
|
||||
r.Patch("/{listID}", withValidatedPost(h.editStack))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user