Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: torrent detail api #35

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/common/cache/clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cache

import (
"github.com/TensoRaws/NuxBT-Backend/module/cache"
"strconv"
)

// ClearCacheByKeys 清除缓存
func ClearCacheByKeys(keys ...string) {
c := cache.Cache
c.Del(keys...)
}

// ClearTorrentDetailCacheByTorrentID 清除种子详情缓存
func ClearTorrentDetailCacheByTorrentID(torrentID int32) {
key := "/api/v1/torrent/detail?torrent_id=" + strconv.Itoa(int(torrentID))
ClearCacheByKeys(key)
}
9 changes: 7 additions & 2 deletions internal/router/api/v1/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ func TorrentRouterGroup(api *gin.RouterGroup) {
jwt.RequireAuth(false),
rbac.RABC(role.REVIEWER),
torrent_service.Review)
// 获取种子文件列表
// 种子详情
torrent.GET("detail",
jwt.RequireAuth(false),
middleware_cache.Response(1*time.Hour),
torrent_service.Detail)
// 种子文件列表
torrent.GET("filelist",
jwt.RequireAuth(false),
middleware_cache.Response(24*time.Hour),
middleware_cache.Response(6*time.Hour),
torrent_service.FileList)
}
2 changes: 2 additions & 0 deletions internal/service/torrent/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package torrent

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/cache"
"github.com/TensoRaws/NuxBT-Backend/internal/common/db"
"github.com/TensoRaws/NuxBT-Backend/module/code"
"github.com/TensoRaws/NuxBT-Backend/module/log"
Expand Down Expand Up @@ -29,6 +30,7 @@ func Delete(c *gin.Context) {
}

resp.OK(c)
cache.ClearTorrentDetailCacheByTorrentID(req.TorrentID)

log.Logger.Infof("delete torrent success, torrent_id: %d", req.TorrentID)
}
39 changes: 39 additions & 0 deletions internal/service/torrent/detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package torrent

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/db"
"github.com/TensoRaws/NuxBT-Backend/module/code"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/resp"
"github.com/gin-gonic/gin"
)

type DetailRequest struct {
TorrentID int32 `form:"torrent_id" binding:"required"`
}

// Detail 获取种子文件列表 (GET /detail)
func Detail(c *gin.Context) {
// 绑定参数
var req DetailRequest
if err := c.ShouldBindQuery(&req); err != nil {
resp.AbortWithMsg(c, code.RequestErrorInvalidParams, err.Error())
return
}

bt, err := db.GetTorrentByID(req.TorrentID)
if err != nil {
resp.AbortWithMsg(c, code.DatabaseErrorRecordNotFound, err.Error())
return
}

torrentInfo, err := GetTorrentInfo(bt)
if err != nil {
resp.AbortWithMsg(c, code.UnknownError, err.Error())
log.Logger.Error("failed to get torrent info: " + err.Error())
return
}

resp.OKWithData(c, torrentInfo)
log.Logger.Infof("get torrent detail success: %v", req.TorrentID)
}
2 changes: 2 additions & 0 deletions internal/service/torrent/edit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package torrent

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/cache"
"github.com/TensoRaws/NuxBT-Backend/internal/common/db"
"github.com/TensoRaws/NuxBT-Backend/module/code"
"github.com/TensoRaws/NuxBT-Backend/module/log"
Expand Down Expand Up @@ -63,6 +64,7 @@ func Edit(c *gin.Context) {
}

resp.OK(c)
cache.ClearTorrentDetailCacheByTorrentID(req.TorrentID)

log.Logger.Infof("update torrent info success, torrent id: %v", req.TorrentID)
}
Loading