diff --git a/internal/adapters/db/db.go b/internal/adapters/db/db.go index 134ffbd..74592bc 100644 --- a/internal/adapters/db/db.go +++ b/internal/adapters/db/db.go @@ -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 @@ -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 diff --git a/internal/adapters/handlers/handlers.go b/internal/adapters/handlers/handlers.go index 3f461fc..a9ab35c 100644 --- a/internal/adapters/handlers/handlers.go +++ b/internal/adapters/handlers/handlers.go @@ -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) }) } diff --git a/internal/components/designs/design-comment-reactions.go b/internal/components/designs/design-comment-reactions.go index f8c844c..4aa2014 100644 --- a/internal/components/designs/design-comment-reactions.go +++ b/internal/components/designs/design-comment-reactions.go @@ -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, @@ -63,5 +62,6 @@ func DesignCommentReactions(props DesignCommentReactionsProps) htmx.Node { }, )..., ), + htmx.Group(children...), ) } diff --git a/internal/controllers/designs/comments.go b/internal/controllers/designs/comments.go index f46eb19..827979d 100644 --- a/internal/controllers/designs/comments.go +++ b/internal/controllers/designs/comments.go @@ -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" @@ -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(¶ms) + if err != nil { + return err + } + + err = l.BindParams(¶ms) + if err != nil { + return err + } + + err = validate.Struct(¶ms) + 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 { diff --git a/internal/controllers/designs/comments/create.go b/internal/controllers/designs/comments/create.go deleted file mode 100644 index 9469815..0000000 --- a/internal/controllers/designs/comments/create.go +++ /dev/null @@ -1,92 +0,0 @@ -package comments - -import ( - "context" - - "github.com/google/uuid" - "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" - - "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, - AuthorID: l.Session().ID, - Author: l.Session().User, - } - - 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( - designs.DesignComment( - designs.DesignCommentProps{ - Comment: l.Comment, - User: l.Session().User, - Design: l.Comment.Design, - }, - ), - ) -} diff --git a/internal/controllers/designs/comments/reaction.go b/internal/controllers/designs/comments/reaction.go index 6ec074f..42ad003 100644 --- a/internal/controllers/designs/comments/reaction.go +++ b/internal/controllers/designs/comments/reaction.go @@ -2,6 +2,7 @@ package comments import ( "context" + "fmt" "github.com/zeiss/fiber-htmx/components/toasts" "github.com/zeiss/service-lens/internal/components/designs" @@ -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 { @@ -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)), ), ), )