Skip to content

Commit

Permalink
wip: add comments to design
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 22, 2024
1 parent 3d1eb8c commit da7099e
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"forwardPorts": [
3000,
5432
5432,
26257
],
"customizations": {
"vscode": {
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
app.Get("/designs/:id", handlers.ShowDesign())
app.Put("/designs/:id", handlers.UpdateDesign())
app.Delete("/designs/:id", handlers.DeleteDesign())
app.Post("/designs/:id/comments", handlers.CreateDesignComment())

// Team ...
team := app.Group("/teams/:t_slug")
Expand Down
17 changes: 3 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
services:

crdb:
restart: always
image: cockroachdb/cockroach:latest-v24.1
Expand All @@ -14,7 +13,7 @@ services:
interval: 3s
timeout: 3s
retries: 5

migrate:
depends_on:
crdb:
Expand All @@ -25,7 +24,7 @@ services:
environment:
- OPENFGA_DATASTORE_ENGINE=postgres
- OPENFGA_DATASTORE_URI=postgres://root@crdb:26257/defaultdb?sslmode=disable

openfga:
depends_on:
migrate:
Expand All @@ -42,15 +41,5 @@ services:
- "8081:8081"
- "3000:3000"

db:
image: postgres
restart: always
environment:
POSTGRES_USER: example
POSTGRES_PASSWORD: example
POSTGRES_DB: example
ports:
- "5432:5432"

volumes:
crdb: {}
crdb: {}
10 changes: 10 additions & 0 deletions internal/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func (rw *writeTxImpl) CreateDesign(ctx context.Context, design *models.Design)
return rw.conn.Create(design).Error
}

// UpdateDesign is a method that updates a design
func (rw *writeTxImpl) UpdateDesign(ctx context.Context, design *models.Design) error {
return rw.conn.Session(&gorm.Session{FullSaveAssociations: true}).Save(design).Error
}

// CreateDesignComment is a method that creates a design comment
func (rw *writeTxImpl) CreateDesignComment(ctx context.Context, comment *models.DesignComment) error {
return rw.conn.Create(comment).Error
}

// CreateProfile is a method that creates a profile
func (rw *writeTxImpl) CreateProfile(ctx context.Context, profile *models.Profile) error {
return rw.conn.Create(profile).Error
Expand Down
8 changes: 8 additions & 0 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/zeiss/service-lens/internal/controllers/dashboard"
"github.com/zeiss/service-lens/internal/controllers/designs"
"github.com/zeiss/service-lens/internal/controllers/designs/comments"
"github.com/zeiss/service-lens/internal/controllers/environments"
"github.com/zeiss/service-lens/internal/controllers/lenses"
"github.com/zeiss/service-lens/internal/controllers/login"
Expand Down Expand Up @@ -81,6 +82,13 @@ func (a *handlers) ShowDesign() fiber.Handler {
})
}

// CreateDesignComment ...
func (a *handlers) CreateDesignComment() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return comments.NewCreateDesignCommentController(a.store)
})
}

// CreateDesign ...
func (a *handlers) CreateDesign() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
Expand Down
96 changes: 96 additions & 0 deletions internal/controllers/designs/comments/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package comments

import (
"context"

"github.com/google/uuid"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/toasts"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"

"github.com/go-playground/validator/v10"
htmx "github.com/zeiss/fiber-htmx"
seed "github.com/zeiss/gorm-seed"
)

var validate *validator.Validate

// CreateDesignCommentControllerImpl ...
type CreateDesignCommentControllerImpl struct {
Comment models.DesignComment
store seed.Database[ports.ReadTx, ports.ReadWriteTx]
htmx.DefaultController
}

// NewCreateDesignCommentController ...
func NewCreateDesignCommentController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *CreateDesignCommentControllerImpl {
return &CreateDesignCommentControllerImpl{store: store}
}

// Error ...
func (l *CreateDesignCommentControllerImpl) Error(err error) error {
return toasts.RenderToasts(
l.Ctx(),
toasts.Toasts(
toasts.ToastsProps{},
toasts.ToastAlertError(
toasts.ToastProps{},
htmx.Text(err.Error()),
),
),
)
}

// Prepare ...
func (l *CreateDesignCommentControllerImpl) Prepare() error {
validate = validator.New()

var request struct {
ID uuid.UUID `uri:"id" validate:"required,uuid"`
Comment string `json:"comment" validate:"required"`
}

err := l.BindBody(&request)
if err != nil {
return err
}

err = l.BindParams(&request)
if err != nil {
return err
}

err = validate.Struct(&request)
if err != nil {
return err
}

l.Comment = models.DesignComment{
DesignID: request.ID,
Comment: request.Comment,
}

return l.store.ReadWriteTx(l.Context(), func(ctx context.Context, tx ports.ReadWriteTx) error {
return tx.CreateDesignComment(ctx, &l.Comment)
})
}

// Post ...
func (l *CreateDesignCommentControllerImpl) Post() error {
return l.Render(
cards.CardBordered(
cards.CardProps{
ClassNames: htmx.ClassNames{
"my-4": true,
},
},
cards.Body(
cards.BodyProps{},
htmx.Div(
htmx.Text(l.Comment.Comment),
),
),
),
)
}
96 changes: 64 additions & 32 deletions internal/controllers/designs/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package designs

import (
"context"
"fmt"

seed "github.com/zeiss/gorm-seed"
"github.com/zeiss/service-lens/internal/components"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"
"github.com/zeiss/service-lens/internal/utils"

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/forms"
"github.com/zeiss/fiber-htmx/components/tables"
)

// ShowDesignControllerImpl ...
Expand Down Expand Up @@ -104,43 +107,72 @@ func (l *ShowDesignControllerImpl) Get() error {
),
),
),
htmx.FormElement(
htmx.HxPost("/designs/new"),
cards.CardBordered(
cards.CardProps{},
cards.Body(
cards.BodyProps{},
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
forms.TextareaBordered(
forms.TextareaProps{
cards.CardBordered(
cards.CardProps{},
cards.Body(
cards.BodyProps{},
cards.Title(
cards.TitleProps{},
htmx.Text("Comments"),
),
htmx.Div(
htmx.ID("comments"),
htmx.Group(htmx.ForEach(tables.RowsPtr(l.Design.Comments), func(c *models.DesignComment, choiceIdx int) htmx.Node {
return cards.CardBordered(
cards.CardProps{
ClassNames: htmx.ClassNames{
"h-32": true,
"my-4": true,
},
Name: "body",
Placeholder: "Start typing...",
},
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
cards.Body(
cards.BodyProps{},
htmx.Text(c.Comment),
),
)
})...),
),

htmx.FormElement(
htmx.HxPost(fmt.Sprintf(utils.CreateDesignCommentUrlFormat, l.Design.ID)),
htmx.HxTarget("#comments"),
htmx.HxSwap("beforeend"),
cards.CardBordered(
cards.CardProps{},
cards.Body(
cards.BodyProps{},
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{},
},
htmx.Text("Supports Markdown."),
forms.TextareaBordered(
forms.TextareaProps{
ClassNames: htmx.ClassNames{
"h-32": true,
},
Name: "comment",
Placeholder: "Add a comment...",
},
),
forms.FormControlLabel(
forms.FormControlLabelProps{},
forms.FormControlLabelText(
forms.FormControlLabelTextProps{
ClassNames: htmx.ClassNames{
"text-neutral-500": true,
},
},
htmx.Text("Supports Markdown."),
),
),
),
cards.Actions(
cards.ActionsProps{},
buttons.Outline(
buttons.ButtonProps{},
htmx.Attribute("type", "submit"),
htmx.Text("Comment"),
),
),
),
),
cards.Actions(
cards.ActionsProps{},
buttons.Outline(
buttons.ButtonProps{},
htmx.Attribute("type", "submit"),
htmx.Text("Comment"),
),
),
),
Expand Down
4 changes: 2 additions & 2 deletions internal/models/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type DesignComment struct {
// ID is the primary key
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()" params:"id"`
// DesignID is the foreign key to the design
DesignID uuid.UUID `json:"design_id" gorm:"type:uuid;index" params:"design_id"`
DesignID uuid.UUID `json:"design_id" gorm:"type:uuid;index" params:"id"`
// Design is the design
Design Design `json:"design" gorm:"foreignKey:DesignID;references:ID"`
Design Design `json:"design" gorm:"foreignKey:DesignID;references:ID" validate:"-"`
// Comment is the comment
Comment string `json:"comment" form:"comment" gorm:"type:text" validate:"required,min=3"`
// ParentID is the foreign key to the parent comment
Expand Down
2 changes: 2 additions & 0 deletions internal/ports/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Handlers interface {
ShowDesign() fiber.Handler
// CreateDesign ...
CreateDesign() fiber.Handler
// CreateDesignComment ...
CreateDesignComment() fiber.Handler
// UpdateDesign ...
UpdateDesign() fiber.Handler
// DeleteDesign ...
Expand Down
4 changes: 4 additions & 0 deletions internal/ports/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type ReadWriteTx interface {

// CreateDesign is a method that creates a design
CreateDesign(ctx context.Context, design *models.Design) error
// UpdateDesign is a method that updates a design
UpdateDesign(ctx context.Context, design *models.Design) error
// CreateDesignComment is a method that creates a design comment
CreateDesignComment(ctx context.Context, comment *models.DesignComment) error
// CreateProfile is a method that creates a profile
CreateProfile(ctx context.Context, profile *models.Profile) error
// UpdateProfile is a method that updates a profile
Expand Down
3 changes: 2 additions & 1 deletion internal/utils/urls.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

const (
ShowDesigUrlFormat = "/designs/%s"
ShowDesigUrlFormat = "/designs/%s"
CreateDesignCommentUrlFormat = "/designs/%s/comments"
)

0 comments on commit da7099e

Please sign in to comment.