From 738917979eee27fe0050546f7b5656f71eeca1be Mon Sep 17 00:00:00 2001 From: Daniil Tsivinsky Date: Thu, 12 Feb 2026 23:34:53 +0300 Subject: [PATCH] allow to delete torrents --- main.go | 17 +++++++++++++++++ model/torrents.go | 5 +++++ web/src/api/useDeleteTorrentMutation.ts | 13 +++++++++++++ web/src/components/Item.tsx | 25 ++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 web/src/api/useDeleteTorrentMutation.ts diff --git a/main.go b/main.go index f67d543..8e17e96 100644 --- a/main.go +++ b/main.go @@ -322,6 +322,23 @@ func main() { }{true}, 200) }) + mux.HandleFunc("DELETE /api/torrents/{id}", func(w http.ResponseWriter, r *http.Request) { + torrentId, err := strconv.Atoi(r.PathValue("id")) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + if err := m.DeleteTorrentById(int64(torrentId)); err != nil { + http.Error(w, err.Error(), 404) + return + } + + sendJSON(w, struct { + Ok bool `json:"ok"` + }{true}, 200) + }) + addr := fmt.Sprintf("%s:%d", *hostname, *port) fmt.Printf("starting http server on %s\n", addr) if err := http.ListenAndServe(addr, mux); err != nil { diff --git a/model/torrents.go b/model/torrents.go index f418ebf..8fa2407 100644 --- a/model/torrents.go +++ b/model/torrents.go @@ -41,3 +41,8 @@ func (m *Model) GetTorrentByGuidAndItemId(guid string, itemId int64) (*Torrent, return torrent, nil } + +func (m *Model) DeleteTorrentById(torrentId int64) error { + _, err := m.db.Exec("DELETE FROM torrents WHERE id = ?", torrentId) + return err +} diff --git a/web/src/api/useDeleteTorrentMutation.ts b/web/src/api/useDeleteTorrentMutation.ts new file mode 100644 index 0000000..dc9443f --- /dev/null +++ b/web/src/api/useDeleteTorrentMutation.ts @@ -0,0 +1,13 @@ +import { useMutation } from "@tanstack/react-query"; + +export const useDeleteTorrentMutation = () => { + return useMutation({ + mutationFn: async ({ id }: { id: number }) => { + const resp = await fetch(`/api/torrents/${id}`, { + method: "DELETE", + }); + const data = await resp.json(); + return data; + }, + }); +}; diff --git a/web/src/components/Item.tsx b/web/src/components/Item.tsx index f025224..56044a5 100644 --- a/web/src/components/Item.tsx +++ b/web/src/components/Item.tsx @@ -13,6 +13,7 @@ import { useDeleteItemMutation } from "../api/useDeleteItemMutation"; import { useQueryClient } from "@tanstack/react-query"; import dayjs from "dayjs"; import { humanFileSize } from "../utils/humanFileSize"; +import { useDeleteTorrentMutation } from "../api/useDeleteTorrentMutation"; export type ItemProps = { item: ItemDetails; @@ -27,6 +28,7 @@ export const Item = ({ item }: ItemProps) => { const deleteMutation = useDeleteItemMutation(); const downloadMutation = useDownloadTorrentMutation(); + const deleteTorrentMutation = useDeleteTorrentMutation(); const Icon = open ? CaretUpIcon : CaretDownIcon; @@ -49,6 +51,21 @@ export const Item = ({ item }: ItemProps) => { ); }; + const handleDeleteTorrent = (torrentId: number) => { + deleteTorrentMutation.mutate( + { + id: torrentId, + }, + { + onSuccess() { + queryClient.invalidateQueries({ + queryKey: ["items", item.id, "torrents"], + }); + }, + }, + ); + }; + return (
{ torrents?.map((torrent) => (
@@ -92,6 +109,12 @@ export const Item = ({ item }: ItemProps) => { )} +
Seeds: {torrent.seeders}