feat: handling image delete
This commit is contained in:
@ -16,6 +16,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImageHandler struct {
|
type ImageHandler struct {
|
||||||
@ -143,6 +144,37 @@ func (h *ImageHandler) uploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
middleware.WriteJsonOrError(h.logger, userImage, w)
|
middleware.WriteJsonOrError(h.logger, userImage, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *ImageHandler) deleteImage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
stringImageID := chi.URLParam(r, "name")
|
||||||
|
imageID, err := uuid.Parse(stringImageID)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := r.Context()
|
||||||
|
|
||||||
|
userID, err := middleware.GetUserID(ctx, h.logger, w)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isAuthorized := h.imageModel.IsUserAuthorized(ctx, imageID, userID)
|
||||||
|
if !isAuthorized {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.imageModel.Delete(ctx, imageID)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *ImageHandler) CreateRoutes(r chi.Router) {
|
func (h *ImageHandler) CreateRoutes(r chi.Router) {
|
||||||
h.logger.Info("Mounting image router")
|
h.logger.Info("Mounting image router")
|
||||||
|
|
||||||
|
@ -208,6 +208,15 @@ func (m ImageModel) AddDescription(ctx context.Context, imageId uuid.UUID, descr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m ImageModel) Delete(ctx context.Context, imageID uuid.UUID) error {
|
||||||
|
deleteImageStmt := Image.DELETE().
|
||||||
|
WHERE(Image.ID.EQ(UUID(imageID)))
|
||||||
|
|
||||||
|
_, err := deleteImageStmt.ExecContext(ctx, m.dbPool)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, userId uuid.UUID) bool {
|
func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, userId uuid.UUID) bool {
|
||||||
getImageUserId := UserImages.SELECT(UserImages.UserID).WHERE(UserImages.ImageID.EQ(UUID(imageId)))
|
getImageUserId := UserImages.SELECT(UserImages.UserID).WHERE(UserImages.ImageID.EQ(UUID(imageId)))
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ import {
|
|||||||
type BaseRequestParams = Partial<{
|
type BaseRequestParams = Partial<{
|
||||||
path: string;
|
path: string;
|
||||||
body: RequestInit["body"];
|
body: RequestInit["body"];
|
||||||
method: "GET" | "POST";
|
method: "GET" | "POST" | "DELETE";
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
// export const base = "https://haystack.johncosta.tech";
|
// export const base = "https://haystack.johncosta.tech";
|
||||||
@ -68,6 +68,17 @@ export const sendImageFile = async (
|
|||||||
return parse(sendImageResponseValidator, res);
|
return parse(sendImageResponseValidator, res);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteImage = async (
|
||||||
|
imageID: string
|
||||||
|
): Promise<void> => {
|
||||||
|
const request = getBaseAuthorizedRequest({
|
||||||
|
path: `images/${imageID}`,
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetch(request);
|
||||||
|
}
|
||||||
|
|
||||||
export class ImageLimitReached extends Error {
|
export class ImageLimitReached extends Error {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
Reference in New Issue
Block a user