Skip to content

Commit

Permalink
feat: adding tags to workloadds
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Aug 29, 2024
1 parent ccfce8d commit 279b863
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
workloads.Get("/:id", handlers.ShowWorkload())
workloads.Get("/:id/edit", handlers.EditWorkload())
workloads.Post("/:id/edit", handlers.EditWorkload())
workloads.Post("/:id/tags", handlers.AddTagWorkload())
workloads.Delete("/:id/tags/:tag_id", handlers.RemoveTagWorkload())
// app.Put("/workloads/:id", handlers.UpdateWorkload())
workloads.Delete("/:id", handlers.DeleteWorkload())
workloads.Get("/partials/environments", handlers.ListEnvironmentsPartial())
Expand Down
34 changes: 34 additions & 0 deletions internal/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ func (rw *writeTxImpl) AddTagDesign(ctx context.Context, designId uuid.UUID, tag
return err
}

err = rw.conn.Debug().Where(&models.Tag{Name: tag.Name, Value: tag.Value}).FirstOrCreate(tag).Error
if err != nil {
return err
}

return rw.conn.Debug().Model(&design).Association("Tags").Append(tag)
}

Expand Down Expand Up @@ -479,3 +484,32 @@ func (rw *writeTxImpl) PublishLens(ctx context.Context, lensID uuid.UUID) error
func (rw *writeTxImpl) UnpublishLens(ctx context.Context, lensID uuid.UUID) error {
return rw.conn.Debug().Model(&models.Lens{}).Where("id = ?", lensID).Update("is_draft", true).Error
}

// AddTagWorkload ...
func (rw *writeTxImpl) AddTagWorkload(ctx context.Context, workloadId uuid.UUID, tag *models.Tag) error {
workload := models.Workload{}

err := rw.conn.Debug().Preload(clause.Associations).First(&workload, workloadId).Error
if err != nil {
return err
}

err = rw.conn.Debug().Where(&models.Tag{Name: tag.Name, Value: tag.Value}).FirstOrCreate(tag).Error
if err != nil {
return err
}

return rw.conn.Debug().Model(&workload).Association("Tags").Append(tag)
}

// RemoveTagWorkload ...
func (rw *writeTxImpl) RemoveTagWorkload(ctx context.Context, workloadId uuid.UUID, tag *models.Tag) error {
workload := models.Workload{}

err := rw.conn.Debug().Preload(clause.Associations).First(&workload, workloadId).Error
if err != nil {
return err
}

return rw.conn.Debug().Model(&workload).Association("Tags").Delete(tag)
}
14 changes: 14 additions & 0 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,17 @@ func (a *handlers) UnpublishLens() fiber.Handler {
return lenses.NewLensPublishController(a.store)
})
}

// AddTagWorkload ...
func (a *handlers) AddTagWorkload() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return workloads.NewTagController(a.store)
})
}

// RemoveTagWorkload ...
func (a *handlers) RemoveTagWorkload() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return workloads.NewTagController(a.store)
})
}
96 changes: 96 additions & 0 deletions internal/components/workloads/workloads-add-tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package workloads

import (
"fmt"

"github.com/google/uuid"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/modals"
"github.com/zeiss/service-lens/internal/utils"
)

// AddTagModalProps ...
type AddTagModalProps struct {
// WorkloadID ...
WorkloadID uuid.UUID
}

// AddTagModal ...
func AddTagModal(props AddTagModalProps) htmx.Node {
return modals.Modal(
modals.ModalProps{
ID: "add_tag_modal",
},
htmx.FormElement(
htmx.HxPost(fmt.Sprintf(utils.AddWorkloadTagUrlFormat, props.WorkloadID)),
htmx.HxTrigger("submit"),
htmx.HxOn("htmx:after-settle", "event.target.closest('dialog').close(), event.target.reset()"),
htmx.HxSwap("none"),
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
forms.TextInputBordered(
forms.TextInputProps{
Name: "name",
Placeholder: "Provide tag name ...",
},
htmx.AutoComplete("off"),
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Use a unique name to identify the tag that has between 3 and 255 characters."),
),
),
),
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
forms.TextInputBordered(
forms.TextInputProps{
Name: "value",
Placeholder: "Provide a tag value ...",
},
htmx.AutoComplete("off"),
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Use a unique value of the tag that has between 3 and 255 characters."),
),
),
),
modals.ModalAction(
modals.ModalActionProps{},
buttons.Ghost(
buttons.ButtonProps{
Type: "button",
},
htmx.Text("Cancel"),
htmx.Attribute("formnovalidate", ""),
htmx.OnClick("event.target.closest('dialog').close()"),
),
buttons.Button(
buttons.ButtonProps{
Type: "submit",
},
htmx.Text("Create"),
),
),
),
)
}
87 changes: 87 additions & 0 deletions internal/components/workloads/workloads-tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package workloads

import (
"fmt"

"github.com/google/uuid"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/icons"
"github.com/zeiss/fiber-htmx/components/tailwind"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/utils"
)

// WorkloadTagProps ...
type WorkloadTagProps struct {
ClassNames htmx.ClassNames
WorkloadID uuid.UUID
Tag models.Tag
}

// WorkloadTag ...
func WorkloadTag(props WorkloadTagProps) htmx.Node {
return htmx.FormElement(
htmx.ClassNames{
tailwind.Flex: true,
tailwind.WFull: true,
tailwind.SpaceX4: true,
},
htmx.HxDelete(fmt.Sprintf(utils.RemoveWorkloadTagUrlFormat, props.WorkloadID, props.Tag.ID)),
htmx.HxConfirm("Are you sure you want to remove this tag?"),
htmx.HxDisabledElt("button"),
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
forms.TextInputBordered(
forms.TextInputProps{
Value: props.Tag.Name,
Disabled: true,
},
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Key in a tag."),
),
),
),
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
forms.TextInputBordered(
forms.TextInputProps{
Value: props.Tag.Value,
Disabled: true,
},
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Value in a tag."),
),
),
),
buttons.Button(
buttons.ButtonProps{
Type: "submit",
},
icons.TrashOutline(
icons.IconProps{},
),
),
)
}
62 changes: 62 additions & 0 deletions internal/components/workloads/workloads-tags-card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package workloads

import (
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/tailwind"
"github.com/zeiss/service-lens/internal/models"
)

// WorkloadTagsCardProps ...
type WorkloadTagsCardProps struct {
ClassNames htmx.ClassNames
Workload models.Workload
}

// WorkloadTagsCard ...
func WorkloadTagsCard(props WorkloadTagsCardProps) htmx.Node {
return cards.CardBordered(
cards.CardProps{
ClassNames: htmx.ClassNames{
tailwind.M2: true,
},
},
cards.Body(
cards.BodyProps{},
cards.Title(
cards.TitleProps{},
htmx.Text("Tags"),
),
htmx.Div(
htmx.ID("tags"),
htmx.Group(
htmx.ForEach(props.Workload.Tags, func(tag models.Tag, idx int) htmx.Node {
return WorkloadTag(
WorkloadTagProps{
WorkloadID: props.Workload.ID,
Tag: tag,
},
)
},
)...,
),
),
AddTagModal(
AddTagModalProps{
WorkloadID: props.Workload.ID,
},
),
cards.Actions(
cards.ActionsProps{},
buttons.Button(
buttons.ButtonProps{
Type: "button",
},
htmx.OnClick("add_tag_modal.showModal()"),
htmx.Text("Add Tag"),
),
),
),
)
}
5 changes: 5 additions & 0 deletions internal/controllers/workloads/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (w *WorkloadShowControllerImpl) Get() error {
Workload: w.workload,
},
),
workloads.WorkloadTagsCard(
workloads.WorkloadTagsCardProps{
Workload: w.workload,
},
),
workloads.WorkloadProfileCard(
workloads.WorkloadProfileCardProps{
Workload: w.workload,
Expand Down
Loading

0 comments on commit 279b863

Please sign in to comment.