fix: cascade deleting of image properties
This commit is contained in:
@ -145,7 +145,7 @@ func (h *ImageHandler) uploadImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *ImageHandler) deleteImage(w http.ResponseWriter, r *http.Request) {
|
func (h *ImageHandler) deleteImage(w http.ResponseWriter, r *http.Request) {
|
||||||
stringImageID := chi.URLParam(r, "name")
|
stringImageID := chi.URLParam(r, "image-id")
|
||||||
imageID, err := uuid.Parse(stringImageID)
|
imageID, err := uuid.Parse(stringImageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
@ -168,6 +168,7 @@ func (h *ImageHandler) deleteImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
err = h.imageModel.Delete(ctx, imageID)
|
err = h.imageModel.Delete(ctx, imageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
h.logger.Warn("cannot delete image", "error", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -188,6 +189,7 @@ func (h *ImageHandler) CreateRoutes(r chi.Router) {
|
|||||||
|
|
||||||
r.Get("/", h.listImages)
|
r.Get("/", h.listImages)
|
||||||
r.Post("/{name}", middleware.WithLimit(h.logger, h.limitsManager.HasReachedImageLimit, h.uploadImage))
|
r.Post("/{name}", middleware.WithLimit(h.logger, h.limitsManager.HasReachedImageLimit, h.uploadImage))
|
||||||
|
r.Delete("/{image-id}", h.deleteImage)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,7 +223,7 @@ func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, use
|
|||||||
userImage := model.UserImages{}
|
userImage := model.UserImages{}
|
||||||
err := getImageUserId.QueryContext(ctx, m.dbPool, &userImage)
|
err := getImageUserId.QueryContext(ctx, m.dbPool, &userImage)
|
||||||
|
|
||||||
return err != nil && userImage.UserID.String() == userId.String()
|
return err == nil && userImage.UserID.String() == userId.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewImageModel(db *sql.DB) ImageModel {
|
func NewImageModel(db *sql.DB) ImageModel {
|
||||||
|
@ -23,13 +23,13 @@ CREATE TABLE haystack.image (
|
|||||||
CREATE TABLE haystack.user_images_to_process (
|
CREATE TABLE haystack.user_images_to_process (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
status haystack.progress NOT NULL DEFAULT 'not-started',
|
status haystack.progress NOT NULL DEFAULT 'not-started',
|
||||||
image_id uuid NOT NULL UNIQUE REFERENCES haystack.image (id),
|
image_id uuid NOT NULL UNIQUE REFERENCES haystack.image (id) ON DELETE CASCADE,
|
||||||
user_id uuid NOT NULL REFERENCES haystack.users (id)
|
user_id uuid NOT NULL REFERENCES haystack.users (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE haystack.user_images (
|
CREATE TABLE haystack.user_images (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
image_id uuid NOT NULL UNIQUE REFERENCES haystack.image (id),
|
image_id uuid NOT NULL UNIQUE REFERENCES haystack.image (id) ON DELETE CASCADE,
|
||||||
user_id uuid NOT NULL REFERENCES haystack.users (id),
|
user_id uuid NOT NULL REFERENCES haystack.users (id),
|
||||||
|
|
||||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
|
||||||
@ -37,7 +37,7 @@ CREATE TABLE haystack.user_images (
|
|||||||
|
|
||||||
CREATE TABLE haystack.logs (
|
CREATE TABLE haystack.logs (
|
||||||
log TEXT NOT NULL,
|
log TEXT NOT NULL,
|
||||||
image_id UUID NOT NULL REFERENCES haystack.image (id),
|
image_id UUID NOT NULL REFERENCES haystack.image (id) ON DELETE CASCADE,
|
||||||
|
|
||||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
|
||||||
);
|
);
|
||||||
@ -67,7 +67,7 @@ CREATE TABLE haystack.processing_lists (
|
|||||||
CREATE TABLE haystack.image_lists (
|
CREATE TABLE haystack.image_lists (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
|
||||||
image_id UUID NOT NULL REFERENCES haystack.image (id),
|
image_id UUID NOT NULL REFERENCES haystack.image (id) ON DELETE CASCADE,
|
||||||
list_id UUID NOT NULL REFERENCES haystack.lists (id)
|
list_id UUID NOT NULL REFERENCES haystack.lists (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ CREATE TABLE haystack.image_schema_items (
|
|||||||
value TEXT,
|
value TEXT,
|
||||||
|
|
||||||
schema_item_id UUID NOT NULL REFERENCES haystack.schema_items (id),
|
schema_item_id UUID NOT NULL REFERENCES haystack.schema_items (id),
|
||||||
image_id UUID NOT NULL REFERENCES haystack.image (id)
|
image_id UUID NOT NULL REFERENCES haystack.image (id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
/* -----| Indexes |----- */
|
/* -----| Indexes |----- */
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import { ImageComponentFullHeight } from "@components/image";
|
import { ImageComponentFullHeight } from "@components/image";
|
||||||
import { useSearchImageContext } from "@contexts/SearchImageContext";
|
import { useSearchImageContext } from "@contexts/SearchImageContext";
|
||||||
import { useParams } from "@solidjs/router";
|
import { useNavigate, useParams } from "@solidjs/router";
|
||||||
import { For, type Component } from "solid-js";
|
import { For, type Component } from "solid-js";
|
||||||
import SolidjsMarkdown from "solidjs-markdown";
|
import SolidjsMarkdown from "solidjs-markdown";
|
||||||
import { ListCard } from "@components/list-card";
|
import { ListCard } from "@components/list-card";
|
||||||
import { deleteImage } from "@network/index";
|
|
||||||
|
|
||||||
export const ImagePage: Component = () => {
|
export const ImagePage: Component = () => {
|
||||||
const { imageId } = useParams<{ imageId: string }>();
|
const { imageId } = useParams<{ imageId: string }>();
|
||||||
|
const nav = useNavigate();
|
||||||
|
|
||||||
const { userImages, lists, onDeleteImage } = useSearchImageContext();
|
const { userImages, lists, onDeleteImage } = useSearchImageContext();
|
||||||
|
|
||||||
@ -16,7 +16,10 @@ export const ImagePage: Component = () => {
|
|||||||
return (
|
return (
|
||||||
<main class="flex flex-col items-center gap-4">
|
<main class="flex flex-col items-center gap-4">
|
||||||
<div class="w-full bg-white rounded-xl p-4">
|
<div class="w-full bg-white rounded-xl p-4">
|
||||||
<ImageComponentFullHeight ID={imageId} onDelete={onDeleteImage()} />
|
<ImageComponentFullHeight ID={imageId} onDelete={(id) => {
|
||||||
|
onDeleteImage(id);
|
||||||
|
nav("/");
|
||||||
|
}} />
|
||||||
</div>
|
</div>
|
||||||
<div class="w-full bg-white rounded-xl p-4 flex flex-col gap-4">
|
<div class="w-full bg-white rounded-xl p-4 flex flex-col gap-4">
|
||||||
<h2 class="font-bold text-2xl">Description</h2>
|
<h2 class="font-bold text-2xl">Description</h2>
|
||||||
|
Reference in New Issue
Block a user