Skip to content

Commit

Permalink
wip: create new design
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 21, 2024
1 parent 1219cda commit 5c88259
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 3 deletions.
10 changes: 10 additions & 0 deletions internal/adapters/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (r *readTxImpl) GetUser(ctx context.Context, user *adapters.GothUser) error
return r.conn.Where(user).First(user).Error
}

// GetDesign is a method that returns a design by ID
func (r *readTxImpl) GetDesign(ctx context.Context, design *models.Design) error {
return r.conn.Preload(clause.Associations).Where(design).First(design).Error
}

// ListDesigns is a method that returns a list of designs
func (r *readTxImpl) ListDesigns(ctx context.Context, pagination *tables.Results[models.Design]) error {
return r.conn.Scopes(tables.PaginatedResults(&pagination.Rows, pagination, r.conn)).Find(&pagination.Rows).Error
Expand Down Expand Up @@ -130,6 +135,11 @@ func NewWriteTx() seed.ReadWriteTxFactory[ports.ReadWriteTx] {
}
}

// CreateDesign is a method that creates a design
func (rw *writeTxImpl) CreateDesign(ctx context.Context, design *models.Design) error {
return rw.conn.Create(design).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
6 changes: 3 additions & 3 deletions internal/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ func (a *handlers) ListDesigns() fiber.Handler {
// NewDesign ...
func (a *handlers) NewDesign() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return nil
return designs.NewDesignController(a.store)
})
}

// ShowDesign ...
func (a *handlers) ShowDesign() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return nil
return designs.NewShowDesignController(a.store)
})
}

// CreateDesign ...
func (a *handlers) CreateDesign() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return nil
return designs.NewCreateDesignController(a.store)
})
}

Expand Down
79 changes: 79 additions & 0 deletions internal/controllers/designs/create.go
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))
}
115 changes: 115 additions & 0 deletions internal/controllers/designs/new.go
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"),
),
),
),
),
),
),
)
}
108 changes: 108 additions & 0 deletions internal/controllers/designs/show.go
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(&params)
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"),
),
),
),
),
),
),
),
),
)
}
4 changes: 4 additions & 0 deletions internal/ports/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Datastore interface {
type ReadTx interface {
// GetUser is a method that returns the profile of the current user
GetUser(ctx context.Context, user *adapters.GothUser) error
// GetDesign is a method that returns a design by ID
GetDesign(ctx context.Context, design *models.Design) error
// ListDesigns is a method that returns a list of designs
ListDesigns(ctx context.Context, designs *tables.Results[models.Design]) error
// ListProfiles is a method that returns a list of profiles
Expand Down Expand Up @@ -65,6 +67,8 @@ type ReadTx interface {
type ReadWriteTx interface {
ReadTx

// CreateDesign is a method that creates a design
CreateDesign(ctx context.Context, design *models.Design) 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
5 changes: 5 additions & 0 deletions internal/utils/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package utils

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

0 comments on commit 5c88259

Please sign in to comment.