-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1219cda
commit 5c88259
Showing
7 changed files
with
324 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package designs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/zeiss/fiber-htmx/components/toasts" | ||
"github.com/zeiss/service-lens/internal/models" | ||
"github.com/zeiss/service-lens/internal/ports" | ||
"github.com/zeiss/service-lens/internal/utils" | ||
|
||
"github.com/go-playground/validator/v10" | ||
htmx "github.com/zeiss/fiber-htmx" | ||
seed "github.com/zeiss/gorm-seed" | ||
) | ||
|
||
var validate *validator.Validate | ||
|
||
type CreateDesignBody struct { | ||
Title string `json:"title" form:"title" validate:"required,min=3,max=2048"` | ||
Body string `json:"body" form:"body" validate:"required"` | ||
} | ||
|
||
// CreateDesignControllerImpl ... | ||
type CreateDesignControllerImpl struct { | ||
body CreateDesignBody | ||
store seed.Database[ports.ReadTx, ports.ReadWriteTx] | ||
htmx.DefaultController | ||
} | ||
|
||
// NewCreateDesignController ... | ||
func NewCreateDesignController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *CreateDesignControllerImpl { | ||
return &CreateDesignControllerImpl{store: store} | ||
} | ||
|
||
// Prepare ... | ||
func (l *CreateDesignControllerImpl) Prepare() error { | ||
validate = validator.New() | ||
|
||
err := l.Ctx().BodyParser(&l.body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = validate.Struct(&l.body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Error ... | ||
func (l *CreateDesignControllerImpl) Error(err error) error { | ||
return toasts.RenderToasts( | ||
l.Ctx(), | ||
toasts.Toasts( | ||
toasts.ToastsProps{}, | ||
toasts.ToastAlertError( | ||
toasts.ToastProps{}, | ||
htmx.Text(err.Error()), | ||
), | ||
), | ||
) | ||
} | ||
|
||
// Post ... | ||
func (l *CreateDesignControllerImpl) Post() error { | ||
design := models.Design{ | ||
Title: l.body.Title, | ||
Body: l.body.Body, | ||
} | ||
|
||
l.store.ReadWriteTx(l.Context(), func(ctx context.Context, tx ports.ReadWriteTx) error { | ||
return tx.CreateDesign(ctx, &design) | ||
}) | ||
|
||
return l.Redirect(fmt.Sprintf(utils.ShowDesigUrlFormat, design.ID)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package designs | ||
|
||
import ( | ||
"github.com/zeiss/fiber-htmx/components/buttons" | ||
"github.com/zeiss/fiber-htmx/components/cards" | ||
"github.com/zeiss/fiber-htmx/components/forms" | ||
seed "github.com/zeiss/gorm-seed" | ||
"github.com/zeiss/service-lens/internal/components" | ||
"github.com/zeiss/service-lens/internal/ports" | ||
|
||
htmx "github.com/zeiss/fiber-htmx" | ||
) | ||
|
||
// NewDesignControllerImpl ... | ||
type NewDesignControllerImpl struct { | ||
store seed.Database[ports.ReadTx, ports.ReadWriteTx] | ||
htmx.DefaultController | ||
} | ||
|
||
// NewDesignController ... | ||
func NewDesignController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *NewDesignControllerImpl { | ||
return &NewDesignControllerImpl{store: store} | ||
} | ||
|
||
// Get ... | ||
func (l *NewDesignControllerImpl) Get() error { | ||
return l.Render( | ||
components.Page( | ||
components.PageProps{}, | ||
components.Layout( | ||
components.LayoutProps{ | ||
Path: l.Path(), | ||
}, | ||
htmx.FormElement( | ||
htmx.HxPost("/designs/new"), | ||
cards.CardBordered( | ||
cards.CardProps{}, | ||
cards.Body( | ||
cards.BodyProps{}, | ||
cards.Title( | ||
cards.TitleProps{}, | ||
htmx.Text("Create Design"), | ||
), | ||
forms.FormControl( | ||
forms.FormControlProps{ | ||
ClassNames: htmx.ClassNames{}, | ||
}, | ||
forms.TextInputBordered( | ||
forms.TextInputProps{ | ||
Name: "title", | ||
Placeholder: "Title", | ||
}, | ||
), | ||
forms.FormControlLabel( | ||
forms.FormControlLabelProps{}, | ||
forms.FormControlLabelText( | ||
forms.FormControlLabelTextProps{ | ||
ClassNames: htmx.ClassNames{ | ||
"text-neutral-500": true, | ||
}, | ||
}, | ||
htmx.Text("The title must be from 3 to 2048 characters."), | ||
), | ||
), | ||
), | ||
forms.FormControl( | ||
forms.FormControlProps{ | ||
ClassNames: htmx.ClassNames{}, | ||
}, | ||
forms.TextareaBordered( | ||
forms.TextareaProps{ | ||
ClassNames: htmx.ClassNames{ | ||
"h-64": true, | ||
}, | ||
Name: "body", | ||
Placeholder: "Start typing...", | ||
}, | ||
), | ||
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("Save Design"), | ||
), | ||
), | ||
), | ||
), | ||
cards.CardBordered( | ||
cards.CardProps{}, | ||
cards.Body( | ||
cards.BodyProps{}, | ||
cards.Title( | ||
cards.TitleProps{}, | ||
htmx.Text("Tags - Optional"), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package designs | ||
|
||
import ( | ||
"context" | ||
|
||
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" | ||
|
||
htmx "github.com/zeiss/fiber-htmx" | ||
"github.com/zeiss/fiber-htmx/components/cards" | ||
) | ||
|
||
// ShowDesignControllerImpl ... | ||
type ShowDesignControllerImpl struct { | ||
Design models.Design | ||
store seed.Database[ports.ReadTx, ports.ReadWriteTx] | ||
htmx.DefaultController | ||
} | ||
|
||
// NewShowDesignController ... | ||
func NewShowDesignController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *ShowDesignControllerImpl { | ||
return &ShowDesignControllerImpl{ | ||
store: store, | ||
} | ||
} | ||
|
||
// Prepare ... | ||
func (l *ShowDesignControllerImpl) Prepare() error { | ||
var params struct { | ||
ID string `uri:"id" validate:"required,uuid"` | ||
} | ||
|
||
err := l.BindParams(¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return l.store.ReadTx(l.Context(), func(ctx context.Context, tx ports.ReadTx) error { | ||
return tx.GetDesign(ctx, &l.Design) | ||
}) | ||
} | ||
|
||
// Get ... | ||
func (l *ShowDesignControllerImpl) Get() error { | ||
return l.Render( | ||
components.Page( | ||
components.PageProps{}, | ||
components.Layout( | ||
components.LayoutProps{ | ||
Path: l.Ctx().Path(), | ||
}, | ||
cards.CardBordered( | ||
cards.CardProps{}, | ||
cards.Body( | ||
cards.BodyProps{}, | ||
cards.Title( | ||
cards.TitleProps{}, | ||
htmx.Text("Overview"), | ||
), | ||
htmx.Div( | ||
htmx.H1( | ||
htmx.Text(l.Design.Title), | ||
), | ||
htmx.Div( | ||
htmx.ClassNames{ | ||
"flex": true, | ||
"flex-col": true, | ||
"py-2": true, | ||
}, | ||
htmx.H4( | ||
htmx.ClassNames{ | ||
"text-gray-500": true, | ||
}, | ||
htmx.Text("Created at"), | ||
), | ||
htmx.H3( | ||
htmx.Text( | ||
l.Design.CreatedAt.Format("2006-01-02 15:04:05"), | ||
), | ||
), | ||
), | ||
htmx.Div( | ||
htmx.ClassNames{ | ||
"flex": true, | ||
"flex-col": true, | ||
"py-2": true, | ||
}, | ||
htmx.H4( | ||
htmx.ClassNames{ | ||
"text-gray-500": true, | ||
}, | ||
htmx.Text("Updated at"), | ||
), | ||
htmx.H3( | ||
htmx.Text( | ||
l.Design.UpdatedAt.Format("2006-01-02 15:04:05"), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package utils | ||
|
||
const ( | ||
ShowDesigUrlFormat = "/designs/%s" | ||
) |