Skip to content

Commit

Permalink
refactor: comment reaftions
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Aug 28, 2024
1 parent 98d36b0 commit fc2b5ff
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 114 deletions.
8 changes: 6 additions & 2 deletions internal/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ func (r *readTxImpl) GetWorkflow(ctx context.Context, workflow *models.Workflow)

// GetDesignComment is a method that returns a design comment by ID
func (r *readTxImpl) GetDesignComment(ctx context.Context, comment *models.DesignComment) error {
return r.conn.Preload("Reactions").Preload("Reactions.Reactor").First(comment, comment.ID).Error
return r.conn.
Preload(clause.Associations).
Preload("Reactions").
Preload("Reactions.Reactor").
First(comment, comment.ID).Error
}

// ListDesignCommentReactions is a method that returns a list of design comment reactions
Expand Down Expand Up @@ -275,7 +279,7 @@ func (rw *writeTxImpl) RemoveTagDesign(ctx context.Context, designId uuid.UUID,

// 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
return rw.conn.Preload(clause.Associations).Create(comment).Error
}

// CreateProfile is a method that creates a profile
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (a *handlers) ShowDesign() fiber.Handler {
// CreateDesignComment ...
func (a *handlers) CreateDesignComment() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return comments.NewCreateDesignCommentController(a.store)
return designs.NewCommentsController(a.store)
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/components/designs/design-comment-reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ type DesignCommentReactionsProps struct {
}

// DesignCommentReactions ...
func DesignCommentReactions(props DesignCommentReactionsProps) htmx.Node {
func DesignCommentReactions(props DesignCommentReactionsProps, children ...htmx.Node) htmx.Node {
return htmx.Div(
htmx.ID(fmt.Sprintf("reaction-%s", props.Comment.ID)),
htmx.HxSwapOob(fmt.Sprintf("#reaction-%s", props.Comment.ID)),
htmx.ClassNames{
tailwind.Flex: true,
tailwind.ItemsCenter: true,
Expand Down Expand Up @@ -63,5 +62,6 @@ func DesignCommentReactions(props DesignCommentReactionsProps) htmx.Node {
},
)...,
),
htmx.Group(children...),
)
}
51 changes: 51 additions & 0 deletions internal/controllers/designs/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package designs
import (
"context"

"github.com/go-playground/validator/v10"
"github.com/zeiss/fiber-htmx/components/toasts"
"github.com/zeiss/service-lens/internal/components/designs"
"github.com/zeiss/service-lens/internal/models"
"github.com/zeiss/service-lens/internal/ports"

Expand Down Expand Up @@ -37,6 +39,55 @@ func (l *CommentsControllerImpl) Error(err error) error {
)
}

// Post ...
func (l *CommentsControllerImpl) Post() error {
validate = validator.New()

var params struct {
DesignID uuid.UUID `json:"id" params:"id" validate:"required,uuid"`
Comment string `json:"comment" validate:"required"`
}

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

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

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

comment := models.DesignComment{
DesignID: params.DesignID,
Comment: params.Comment,
AuthorID: l.Session().ID,
Author: l.Session().User,
}

err = l.store.ReadWriteTx(l.Context(), func(ctx context.Context, tx ports.ReadWriteTx) error {
return tx.CreateDesignComment(ctx, &comment)
})
if err != nil {
return err
}

return l.Render(
designs.DesignComment(
designs.DesignCommentProps{
Comment: comment,
User: l.Session().User,
Design: comment.Design,
},
),
)
}

// Delete ...
func (l *CommentsControllerImpl) Delete() error {
var params struct {
Expand Down
92 changes: 0 additions & 92 deletions internal/controllers/designs/comments/create.go

This file was deleted.

21 changes: 4 additions & 17 deletions internal/controllers/designs/comments/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package comments

import (
"context"
"fmt"

"github.com/zeiss/fiber-htmx/components/toasts"
"github.com/zeiss/service-lens/internal/components/designs"
Expand Down Expand Up @@ -95,16 +96,12 @@ func (l *ReactionCommentControllerImpl) Delete() error {
Design: design,
Comment: comment,
},
htmx.HxSwapOob(fmt.Sprintf("#reaction-%s", comment.ID)),
),
),
)
}

// Prepare ...
func (l *ReactionCommentControllerImpl) Prepare() error {
return nil
}

// Post ...
func (l *ReactionCommentControllerImpl) Post() error {
var params struct {
Expand Down Expand Up @@ -151,25 +148,15 @@ func (l *ReactionCommentControllerImpl) Post() error {
return err
}

design := models.Design{
ID: params.ID,
}

err = l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error {
return tx.GetDesign(ctx, &design)
})
if err != nil {
return err
}

return l.Render(
htmx.Fragment(
designs.DesignCommentReactions(
designs.DesignCommentReactionsProps{
User: l.Session().User,
Design: design,
Design: comment.Design,
Comment: comment,
},
htmx.HxSwapOob(fmt.Sprintf("#reaction-%s", comment.ID)),
),
),
)
Expand Down

0 comments on commit fc2b5ff

Please sign in to comment.