This commit is contained in:
Achim Rohn 2025-02-20 16:48:07 +01:00
parent 94c0e4fc5e
commit 9795d07196
13 changed files with 95 additions and 32 deletions

View File

@ -13,8 +13,6 @@ RUN bash scripts/db-push.sh
RUN go build -o /main main.go
RUN go build -o /create-user cli/create-user_gen.go
FROM alpine
ARG USER=default
@ -31,7 +29,6 @@ USER $USER
WORKDIR $HOME
COPY --from=build --chown=default:default /main ./main
COPY --from=build --chown=default:default /create-user ./create-user
COPY --from=build --chown=default:default /app/.env .env
COPY --chown=default:default views/ ./views
COPY --chown=default:default static/ ./static/

14
crud/tracing.go Normal file
View File

@ -0,0 +1,14 @@
package crud
import (
"encoding/json"
"fmt"
)
func Trace(object any) string {
marshalled, err := json.Marshal(object)
if err != nil {
return fmt.Sprint(object)
}
return string(marshalled)
}

7
crud/util.go Normal file
View File

@ -0,0 +1,7 @@
package crud
import "strings"
func FirstLetterToUpper(name string) string {
return strings.ToUpper(name[:1]) + name[1:]
}

View File

@ -1,5 +1,7 @@
package html_components
import "maragu.dev/gomponents"
type EditItemInputType string
const (
@ -27,6 +29,7 @@ type EditItem struct {
CancelUrl string
IsCreate bool
Inputs []EditItemInputs
ComponentInputs []gomponents.Node
SubmitButtonLabel string
HasFileUpload bool
FromTable bool

View File

@ -1,5 +1,7 @@
package html_components
import "maragu.dev/gomponents"
type ItemDisplayType string
const (
@ -24,12 +26,13 @@ type ItemDisplaySubItem struct {
}
type ItemDisplay struct {
BackUrl string
Columns []ItemDisplayColumn
SubItems []ItemDisplaySubItem
EditItemUrl string
DeleteItemUrl string
DeletePushUrl string
HasImages bool
ItemId int
BackUrl string
Columns []ItemDisplayColumn
ComponentColumns []gomponents.Node
SubItems []ItemDisplaySubItem
EditItemUrl string
DeleteItemUrl string
DeletePushUrl string
HasImages bool
ItemId int
}

View File

@ -26,7 +26,7 @@ type PersonStruct struct {
func AddTablePrototype(e *Echo) {
items := getPrototypeStructs()
e.GET(tablePrototypePath, func(c Context) error {
table := Table{
table := GohtmlTable{
Headers: []string{"Name", "Age", "Profession", "Member Since"},
Rows: prototypeStructsToTableRows(items),
EntityUrl: tablePrototypePath,
@ -120,7 +120,7 @@ func ParseDateTime(since string) time.Time {
}
func returnRenderTable(c Context, items []PersonStruct) error {
table := Table{
table := GohtmlTable{
Headers: []string{"Name", "Age", "Profession", "Member Since"},
Rows: prototypeStructsToTableRows(items),
EntityUrl: tablePrototypePath,

View File

@ -1,5 +1,10 @@
package html_components
import (
"fmt"
"maragu.dev/gomponents"
)
type TableColumnType string
const (
@ -12,12 +17,24 @@ type TableColumn struct {
Type TableColumnType
}
type TableComponentColumnsStrikethrough struct {
Active bool
Url string
Value bool
}
type TableComponentColumns struct {
Nodes []gomponents.Node
Strikethrough TableComponentColumnsStrikethrough
}
type TableRow struct {
Id string
Columns []TableColumn
EntityUrl string
EditItemUrl string
DeleteItemUrl string
Id string
Columns []TableColumn
ComponentColumns TableComponentColumns
EntityUrl string
EditItemUrl string
DeleteItemUrl string
}
type Pagination struct {
@ -31,8 +48,27 @@ type Pagination struct {
NextPage int
}
type Table struct {
type TableSortable struct {
IsSortable bool
EntityUrl string
}
type TableHeader struct {
Label string
OrderBy string
}
func (t TableSortable) UpUrl(id string) string {
return fmt.Sprint(t.EntityUrl, "/", id, "/up")
}
func (t TableSortable) DownUrl(id string) string {
return fmt.Sprint(t.EntityUrl, "/", id, "/down")
}
type GohtmlTable struct {
Headers []string
TableHeaders []TableHeader
Rows []TableRow
EntityUrl string
OrderBy string
@ -43,4 +79,6 @@ type Table struct {
ShowBack bool
BackUrl string
CreateItemUrl string
Sortable TableSortable
QueryParams string
}

View File

@ -26,6 +26,7 @@ model todo {
id Int @id @default(autoincrement())
name String @default("")
completed Boolean @default(false)
due Json @default("{}")
user_id Int @default(0)
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt

View File

@ -1,2 +1,2 @@
#!/bin/bash
go run github.com/steebchen/prisma-client-go db push
go run github.com/steebchen/prisma-client-go db push --skip-generate

View File

@ -38,6 +38,7 @@ func (r *TodoRepository) Create(todo Todo) (int, error) {
"updated_at": time.Now(),
"name": todo.Name,
"completed": todo.Completed,
"due": r.jsonToString(todo.Due),
"user_id": todo.UserId,
}).
Returning("id").
@ -79,7 +80,7 @@ func (e TodoAlreadyExistsError) Error() string {
func (r *TodoRepository) getSelectColumns() []any {
return []any{"id", "created_at", "updated_at",
"name", "completed", "user_id",
"name", "completed", "due", "user_id",
}
}
@ -123,6 +124,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error)
&item.UpdatedAt,
&item.Name,
&item.Completed,
&item.Due,
&item.UserId,
)
if err != nil {
@ -135,6 +137,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error)
&item.UpdatedAt,
&item.Name,
&item.Completed,
&item.Due,
&item.UserId,
)
if err != nil {
@ -147,6 +150,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error)
UpdatedAt: item.UpdatedAt,
Name: item.Name,
Completed: item.Completed,
Due: item.Due,
UserId: item.UserId,
}, item.Count, nil
}
@ -159,6 +163,7 @@ func (r *TodoRepository) Update(userId int, todo Todo) error {
"updated_at": time.Now(),
"name": todo.Name,
"completed": todo.Completed,
"due": r.jsonToString(todo.Due),
"user_id": todo.UserId,
}).
Where(goqu.Ex{
@ -207,6 +212,7 @@ type TodoField string
const (
TodoFieldName TodoField = "name"
TodoFieldCompleted TodoField = "completed"
TodoFieldDue TodoField = "due"
)
type TodoNameFilter struct {
@ -325,7 +331,3 @@ func (r *TodoRepository) jsonToString(jsonData any) string {
}
return string(bytes)
}
func (r *TodoRepository) FirstLetterToUpper(name string) string {
return strings.ToUpper(name[:1]) + name[1:]
}

View File

@ -82,7 +82,7 @@ func (i *TodoCrud) getSubItemDisplays(item Todo, c Context) []ItemDisplaySubItem
type TodoDisplay struct {
IsTable bool
Table Table
Table GohtmlTable
IsDisplay bool
ItemDisplay ItemDisplay
IsEdit bool
@ -246,7 +246,7 @@ func (i *TodoCrud) returnRenderTable(c Context, items []Todo, count int) error {
return i.html.RenderComponent(c, "table", table)
}
func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) Table {
func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) GohtmlTable {
filter := c.FormValue("filter")
page := ParseIntWithDefault(c.FormValue("pageNumber"), 1)
index := (page - 1) * 5
@ -254,7 +254,7 @@ func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) Table {
if itemEnd > count {
itemEnd = count
}
return Table{
return GohtmlTable{
Headers: []string{
"Name",

View File

@ -12,6 +12,7 @@ type Todo struct {
Id int `db:"id"`
Name string `db:"name"`
Completed bool `db:"completed"`
Due time `db:"due"`
UserId int `db:"user_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
@ -22,6 +23,7 @@ func (s *Todo) String() string {
"Id: ", s.Id, ", ",
"Name: ", s.Name, ", ",
"Completed: ", s.Completed, ", ",
"Due: ", s.Due, ", ",
"UserId: ", s.UserId, ", ",
"CreatedAt: ", s.CreatedAt, ", ",
"UpdatedAt: ", s.UpdatedAt, ", ",

View File

@ -309,10 +309,6 @@ func (r *UserRepository) jsonToString(jsonData any) string {
return string(bytes)
}
func (r *UserRepository) FirstLetterToUpper(name string) string {
return strings.ToUpper(name[:1]) + name[1:]
}
func (u *UserRepository) DoesUserEmailExist(email string) (bool, error) {
sql, args, _ := u.dialect.From("user").
Prepared(true).