feat: add tags

This commit is contained in:
2024-11-18 01:28:31 +07:00
parent 95f9f76edd
commit b0932c39b1
17 changed files with 409 additions and 67 deletions
+19 -2
View File
@@ -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
}
+38 -5
View File
@@ -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,
})
}
+1
View File
@@ -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"`
+1
View File
@@ -10,6 +10,7 @@ var Models = []interface{}{
&models.UserAccount{},
&models.Keychain{},
&models.Host{},
&models.HostTag{},
&models.Team{},
&models.TeamMembers{},
&models.TermSession{},
+7
View File
@@ -28,6 +28,7 @@ type Host struct {
Port int `json:"port" gorm:"type:smallint"`
OS string `json:"os" gorm:"type:varchar(32)"`
Metadata datatypes.JSONMap `json:"metadata"`
Tags []*HostTag `json:"tags" gorm:"foreignKey:HostID"`
ParentID *string `json:"parentId" gorm:"type:varchar(26)"`
Parent *Host `json:"parent" gorm:"foreignKey:ParentID"`
@@ -40,6 +41,12 @@ type Host struct {
SoftDeletes
}
type HostTag struct {
HostID string `gorm:"primarykey;type:varchar(26)" json:"-"`
Host *Host `gorm:"foreignKey:HostID;references:ID" json:"-"`
Name string `gorm:"primarykey;type:varchar(64)" json:"name"`
}
type HostDecrypted struct {
Host
Key map[string]interface{}