Skip to content

Commit

Permalink
feat: torrent delete api (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jul 23, 2024
1 parent eeea8ae commit ceacd4e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
14 changes: 14 additions & 0 deletions internal/common/db/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"errors"
"fmt"

"github.com/TensoRaws/NuxBT-Backend/dal/model"
"github.com/TensoRaws/NuxBT-Backend/dal/query"
Expand All @@ -22,6 +23,19 @@ func CreateTorrent(torrent *model.Torrent) (err error) {
return err
}

// DeleteTorrent 删除种子
func DeleteTorrent(torrentID int32) (err error) {
q := query.Torrent
info, err := q.Where(q.TorrentID.Eq(torrentID)).Delete()
if err != nil {
return err
}
if info.RowsAffected == 0 {
return fmt.Errorf("no rows affected, nothing will be updated, torrent ID: %v", torrentID)
}
return err
}

// PatchTorrent 更新种子信息,根据 torrentID 和 details 更新种子信息
func PatchTorrent[T *model.Torrent | map[string]any | any](torrentID int32, details T) (err error) {
q := query.Torrent
Expand Down
4 changes: 2 additions & 2 deletions internal/common/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func CreateUser(user *model.User) (err error) {
// PatchUser 更新用户信息,根据 userID 和 details 更新用户信息
func PatchUser[T *model.User | map[string]any | any](userID int32, details T) (err error) {
q := query.User
ResultInfo, err := q.Where(q.UserID.Eq(userID)).Updates(details)
info, err := q.Where(q.UserID.Eq(userID)).Updates(details)
if err != nil {
return err
}
if ResultInfo.RowsAffected == 0 {
if info.RowsAffected == 0 {
return fmt.Errorf("no rows affected, nothing will be updated, userID: %v", userID)
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/router/api/v1/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func TorrentRouterGroup(api *gin.RouterGroup) {
jwt.RequireAuth(false),
rbac.RABC(),
torrent_service.Edit)
// 种子删除
torrent.POST("delete",
jwt.RequireAuth(false),
rbac.RABC(role.ADMIN),
torrent_service.Delete)
// 获取种子文件列表
torrent.GET("filelist",
jwt.RequireAuth(false),
Expand Down
34 changes: 34 additions & 0 deletions internal/service/torrent/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 ProfileUpdateRequest struct {
TorrentID int32 `json:"torrent_id" binding:"required"`
}

// Delete 种子删除 (POST /delete)
func Delete(c *gin.Context) {
// 参数绑定
var req ProfileUpdateRequest
if err := c.ShouldBindJSON(&req); err != nil {
resp.AbortWithMsg(c, code.RequestErrorInvalidParams, err.Error())
return
}

err := db.DeleteTorrent(req.TorrentID)
if err != nil {
resp.AbortWithMsg(c, code.DatabaseErrorRecordDeleteFailed, err.Error())
log.Logger.Errorf("delete torrent failed, torrent_id: %d, error: %s", req.TorrentID, err.Error())
return
}

resp.OK(c)

log.Logger.Infof("delete torrent success, torrent_id: %d", req.TorrentID)
}
3 changes: 2 additions & 1 deletion module/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const (
RequestErrorInvalidParams
// DatabaseError 数据库错误
DatabaseErrorRecordCreateFailed
DatabaseErrorRecordNotFound
DatabaseErrorRecordDeleteFailed
DatabaseErrorRecordPatchFailed
DatabaseErrorRecordNotFound
// OssError OSS错误
OssErrorPutFailed
OssErrorGetFailed
Expand Down
3 changes: 2 additions & 1 deletion module/code/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export const enum ErrorCode {
AuthErrorNoPermission,
RequestErrorInvalidParams,
DatabaseErrorRecordCreateFailed,
DatabaseErrorRecordNotFound,
DatabaseErrorRecordDeleteFailed,
DatabaseErrorRecordPatchFailed,
DatabaseErrorRecordNotFound,
OssErrorPutFailed,
OssErrorGetFailed,
UserErrorRegisterNotAllowed,
Expand Down
3 changes: 2 additions & 1 deletion module/code/code_map.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ceacd4e

Please sign in to comment.