diff --git a/src/internal/torrents/handlers.go b/src/internal/torrents/handlers.go index 6929293..8569fce 100644 --- a/src/internal/torrents/handlers.go +++ b/src/internal/torrents/handlers.go @@ -4,6 +4,9 @@ import ( "encoding/json" "errors" "net/http" + "strconv" + + "github.com/gorilla/mux" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/auth" "gitlab.quimbo.fr/odwrtw/canape-sql/src/internal/users" @@ -43,7 +46,7 @@ func DownloadHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error return env.RenderOK(w, "Torrent added") } -// ListHandler downloads a movie via polochon +// ListHandler downloads a torrent via polochon func ListHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error { v := auth.GetCurrentUser(r, env.Log) user, ok := v.(*users.User) @@ -63,3 +66,31 @@ func ListHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error { return env.RenderJSON(w, torrents) } + +// RemoveHandler removes a torrents via polochon +func RemoveHandler(env *web.Env, w http.ResponseWriter, r *http.Request) error { + v := auth.GetCurrentUser(r, env.Log) + user, ok := v.(*users.User) + if !ok { + return env.RenderError(w, errors.New("invalid user type")) + } + + // Get the torrent ID from the URL + vars := mux.Vars(r) + torrentID, err := strconv.Atoi(vars["id"]) + if err != nil { + return env.RenderError(w, errors.New("invalid argument")) + } + + client, err := user.NewPapiClient() + if err != nil { + return env.RenderError(w, err) + } + + err = client.RemoveTorrent(torrentID) + if err != nil { + return env.RenderError(w, err) + } + + return env.RenderOK(w, "Torrent removed") +} diff --git a/src/public/js/app.js b/src/public/js/app.js index b7e7ac8..8db0d3b 100644 --- a/src/public/js/app.js +++ b/src/public/js/app.js @@ -43,7 +43,7 @@ import getRoutes from './routes' function mapStateToProps(state) { let torrentCount = 0; if (state.torrentStore.has('torrents')) { - torrentCount = state.torrentStore.has('torrents').size; + torrentCount = state.torrentStore.get('torrents').size; } return { username: state.userStore.username, diff --git a/src/routes.go b/src/routes.go index ab1fab3..a00e836 100644 --- a/src/routes.go +++ b/src/routes.go @@ -52,6 +52,7 @@ func setupRoutes(env *web.Env) { // Torrents routes env.Handle("/torrents", torrents.DownloadHandler).WithRole(users.UserRole).Methods("POST") env.Handle("/torrents", torrents.ListHandler).WithRole(users.UserRole).Methods("GET") + env.Handle("/torrents/{id:[0-9]+}", torrents.RemoveHandler).WithRole(users.UserRole).Methods("DELETE") // Route to refresh all movies and shows env.Handle("/refresh", extmedias.RefreshHandler).WithRole(users.AdminRole).Methods("POST")