wip: list

This commit is contained in:
loveuer
2024-09-29 22:15:28 +08:00
parent b2c13508f4
commit 5e0885f22d
13 changed files with 241 additions and 19 deletions

View File

@ -1,11 +1,14 @@
package handler
import (
"errors"
"github.com/loveuer/nf-disk/internal/db"
"github.com/loveuer/nf-disk/internal/manager"
"github.com/loveuer/nf-disk/internal/model"
"github.com/loveuer/nf-disk/internal/s3"
"github.com/loveuer/nf-disk/ndh"
"github.com/samber/lo"
"gorm.io/gorm"
)
func ConnectionTest(c *ndh.Ctx) error {
@ -78,7 +81,7 @@ func ConnectionCreate(c *ndh.Ctx) error {
return c.Send500(err.Error(), "创建连接失败(1)")
}
if err = manager.Register(connection, client); err != nil {
if err = manager.Manager.Register(connection, client); err != nil {
return c.Send500(err.Error(), "创建连接失败(2)")
}
@ -97,5 +100,52 @@ func ConnectionList(c *ndh.Ctx) error {
return err
}
listMap := lo.SliceToMap(list, func(item *model.Connection) (uint64, *model.Connection) {
return item.Id, item
})
manager.Manager.Map(func(c *model.Connection, s *s3.Client) error {
if item, ok := listMap[c.Id]; ok {
item.Active = true
}
return nil
})
return c.Send200(map[string]any{"list": list})
}
func ConnectionConnect(c *ndh.Ctx) error {
type Req struct {
Id uint64 `json:"id"`
}
var (
err error
req = new(Req)
conn = new(model.Connection)
client *s3.Client
)
if err = c.ReqParse(req); err != nil {
return c.Send400(req)
}
if err = db.Default.Session().Take(conn, req.Id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.Send400(err.Error(), "连接不存在")
}
return c.Send500(err.Error())
}
if client, err = s3.New(c.Context(), conn.Endpoint, conn.Access, conn.Key); err != nil {
return c.Send500(err.Error(), "连接失败")
}
if err = manager.Manager.Register(conn, client); err != nil {
return c.Send500(err.Error(), "连接失败")
}
return c.Send200(conn, "连接成功")
}