mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: add tags
This commit is contained in:
@@ -21,7 +21,9 @@ func NewRepository(r *Hosts) *Hosts {
|
||||
}
|
||||
|
||||
func (r *Hosts) GetAll(opt GetAllOpt) ([]*models.Host, error) {
|
||||
query := r.db.Order("id DESC")
|
||||
query := r.db.
|
||||
Preload("Tags").
|
||||
Order("id DESC")
|
||||
|
||||
if len(opt.ID) > 0 {
|
||||
query = query.Where("hosts.id IN (?)", opt.ID)
|
||||
@@ -33,7 +35,7 @@ func (r *Hosts) GetAll(opt GetAllOpt) ([]*models.Host, error) {
|
||||
query = query.Where("hosts.owner_id = ? AND hosts.team_id IS NULL", r.User.ID)
|
||||
}
|
||||
|
||||
if opt.ParentID != nil {
|
||||
if opt.ParentID != nil && *opt.ParentID != "none" {
|
||||
if *opt.ParentID != "" {
|
||||
query = query.Where("hosts.parent_id = ?", *opt.ParentID)
|
||||
} else {
|
||||
@@ -89,3 +91,18 @@ func (r *Hosts) Delete(id string) error {
|
||||
func (r *Hosts) SetParentId(id *string, hostIds []string) error {
|
||||
return r.db.Model(&models.Host{}).Where("id IN (?)", hostIds).Update("parent_id", id).Error
|
||||
}
|
||||
|
||||
func (r *Hosts) GetAvailableTags(teamId string) (*[]models.HostTag, error) {
|
||||
query := r.db.Model(&models.HostTag{}).
|
||||
Joins("JOIN hosts ON hosts.id = host_tags.host_id").
|
||||
Distinct("host_tags.name")
|
||||
|
||||
if teamId != "" {
|
||||
query = query.Where("hosts.team_id = ?", teamId)
|
||||
} else {
|
||||
query = query.Where("hosts.owner_id = ? AND hosts.team_id IS NULL", r.User.ID)
|
||||
}
|
||||
|
||||
var result []models.HostTag
|
||||
return &result, query.Find(&result).Error
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ func Router(app fiber.Router) {
|
||||
router.Put("/:id", update)
|
||||
router.Delete("/:id", delete)
|
||||
router.Post("/move", move)
|
||||
router.Get("/tags", getTags)
|
||||
}
|
||||
|
||||
func getAll(c *fiber.Ctx) error {
|
||||
@@ -69,6 +70,11 @@ func create(c *fiber.Ctx) error {
|
||||
AltKeyID: body.AltKeyID,
|
||||
}
|
||||
|
||||
item.Tags = []*models.HostTag{}
|
||||
for _, tag := range body.Tags {
|
||||
item.Tags = append(item.Tags, &models.HostTag{Name: tag})
|
||||
}
|
||||
|
||||
osName, err := tryConnect(c, item)
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, fmt.Errorf("cannot connect to the host: %s", err), 500)
|
||||
@@ -113,16 +119,28 @@ func update(c *fiber.Ctx) error {
|
||||
AltKeyID: body.AltKeyID,
|
||||
}
|
||||
|
||||
osName, err := tryConnect(c, item)
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, fmt.Errorf("cannot connect to the host: %s", err), 500)
|
||||
}
|
||||
item.OS = osName
|
||||
// osName, err := tryConnect(c, item)
|
||||
// if err != nil {
|
||||
// return utils.ResponseError(c, fmt.Errorf("cannot connect to the host: %s", err), 500)
|
||||
// }
|
||||
// item.OS = osName
|
||||
|
||||
if err := repo.Update(id, item); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
tags := []models.HostTag{}
|
||||
for _, tag := range body.Tags {
|
||||
tags = append(tags, models.HostTag{
|
||||
Name: tag,
|
||||
})
|
||||
}
|
||||
|
||||
if err := repo.db.Unscoped().Model(&item).
|
||||
Association("Tags").Unscoped().Replace(&tags); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
@@ -200,3 +218,18 @@ func move(c *fiber.Ctx) error {
|
||||
|
||||
return c.JSON(true)
|
||||
}
|
||||
|
||||
func getTags(c *fiber.Ctx) error {
|
||||
teamId := c.Query("teamId")
|
||||
user := lib.GetUser(c)
|
||||
repo := NewRepository(&Hosts{User: user})
|
||||
|
||||
rows, err := repo.GetAvailableTags(teamId)
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"rows": rows,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ type CreateHostSchema struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Metadata datatypes.JSONMap `json:"metadata"`
|
||||
Tags []string `json:"tags"`
|
||||
|
||||
TeamID *string `json:"teamId"`
|
||||
ParentID *string `json:"parentId"`
|
||||
|
||||
Reference in New Issue
Block a user