diff --git a/backend/routes/app/database/storage.database.js b/backend/routes/app/database/storage.database.js index 242d3a6..035d191 100644 --- a/backend/routes/app/database/storage.database.js +++ b/backend/routes/app/database/storage.database.js @@ -48,3 +48,16 @@ export const updateStorage = async (uuid, values) => { return { code: "es003" }; } }; + +export const deleteStorage = async (uuid) => { + const [result] = await pool.query( + "DELETE FROM storage_locations WHERE uuid = UUID_TO_BIN(?);", + [uuid], + ); + + if (result.affectedRows > 0) { + return { code: "ss004" }; + } else { + return { code: "es004" }; + } +}; diff --git a/backend/routes/app/storage.route.js b/backend/routes/app/storage.route.js index 5cebd5c..d2fb690 100644 --- a/backend/routes/app/storage.route.js +++ b/backend/routes/app/storage.route.js @@ -5,6 +5,7 @@ import { allStorages, newStorage, updateStorage, + deleteStorage, } from "./database/storage.database.js"; dotenv.config(); const router = express.Router(); @@ -34,7 +35,9 @@ router.get("/all-storages", authenticate, async (req, res) => { router.post("/new-storage", authenticate, async (req, res) => { const { name, description } = req.body; - if (!name || !description) { + let desc = description; + + if (!name) { res.status(400).json({ success: false, code: "es000", @@ -44,7 +47,11 @@ router.post("/new-storage", authenticate, async (req, res) => { return; } - const result = await newStorage(name, description); + if (description == "") { + desc = null; + } + + const result = await newStorage(name, desc); if (result.code === "es002") { res.status(500).json({ @@ -90,4 +97,28 @@ router.post("/update-storage", authenticate, async (req, res) => { } }); +router.delete("/delete", authenticate, async (req, res) => { + const uuid = req.query.uuid; + + const result = await deleteStorage(uuid); + + if (result.code === "es004") { + res.status(500).json({ + success: false, + code: "es004", + data: null, + message: "unexpected server error", + }); + } + + if (result.code === "ss004") { + res.status(201).json({ + success: true, + code: "ss004", + data: null, + message: "", + }); + } +}); + export default router; diff --git a/frontend/src/components/StorageRow.tsx b/frontend/src/components/StorageRow.tsx index f641f0e..42acf29 100644 --- a/frontend/src/components/StorageRow.tsx +++ b/frontend/src/components/StorageRow.tsx @@ -1,5 +1,5 @@ import { useQueryClient, useMutation } from "@tanstack/react-query"; -import { updateStorage } from "../utils/uxFncs"; +import { deleteStorage, updateStorage } from "../utils/uxFncs"; import { useForm } from "@tanstack/react-form"; import { useStore } from "@tanstack/react-store"; import { Input, Button } from "@mui/joy"; @@ -21,6 +21,13 @@ export const StorageRow = ({ storage }: StorageRowProps) => { }, }); + const deleteMutation = useMutation({ + mutationFn: (uuid: string) => deleteStorage(uuid), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["storages"] }); + }, + }); + const form = useForm({ defaultValues: { name: storage.name, @@ -68,15 +75,16 @@ export const StorageRow = ({ storage }: StorageRowProps) => { diff --git a/frontend/src/pages/Storages.tsx b/frontend/src/pages/Storages.tsx index c5bf8e3..c2833d0 100644 --- a/frontend/src/pages/Storages.tsx +++ b/frontend/src/pages/Storages.tsx @@ -1,6 +1,6 @@ import { useQuery } from "@tanstack/react-query"; import { getStorages } from "../utils/uxFncs"; -import { Sheet, Table, Button, CircularProgress } from "@mui/joy"; +import { Sheet, Table, Button, CircularProgress, Typography } from "@mui/joy"; import { useTranslation } from "react-i18next"; import type { Storage } from "../misc/interfaces"; import { StorageRow } from "../components/StorageRow"; @@ -24,6 +24,9 @@ export const Storages = () => { <> + + {t("storage-delete-info")} + { return response.data; } }; + +export const deleteStorage = async (uuid: string) => { + const result = await fetch(`${API_BASE}/storage/delete?uuid=${uuid}`, { + method: "DELETE", + headers: { + Authorization: `Bearer ${Cookies.get("token") || ""}`, + "Content-Type": "application/json", + Accept: "application/json", + }, + }); + + const response = await result.json(); + + if (response.code === "es004") { + return { success: false, code: response.code }; + } + + if (response.code === "ss004") { + return { success: true, code: response.code }; + } +};