From 9795d07196d31a422bba2038f7a8f426b178a52e Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Thu, 20 Feb 2025 16:48:07 +0100 Subject: [PATCH] Update --- Dockerfile | 3 -- crud/tracing.go | 14 +++++++++ crud/util.go | 7 +++++ html_components/edit-item.go | 3 ++ html_components/item-display.go | 19 +++++++----- html_components/table-prototype.go | 4 +-- html_components/table.go | 50 ++++++++++++++++++++++++++---- schema.prisma | 1 + scripts/db-push.sh | 2 +- todo/todo-repository_gen.go | 12 ++++--- todo/todo-rest-crud_gen.go | 6 ++-- todo/todo_gen.go | 2 ++ user/user-repository_gen.go | 4 --- 13 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 crud/tracing.go create mode 100644 crud/util.go diff --git a/Dockerfile b/Dockerfile index cbf920f..7ec0c83 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/ diff --git a/crud/tracing.go b/crud/tracing.go new file mode 100644 index 0000000..c344189 --- /dev/null +++ b/crud/tracing.go @@ -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) +} diff --git a/crud/util.go b/crud/util.go new file mode 100644 index 0000000..b76e788 --- /dev/null +++ b/crud/util.go @@ -0,0 +1,7 @@ +package crud + +import "strings" + +func FirstLetterToUpper(name string) string { + return strings.ToUpper(name[:1]) + name[1:] +} diff --git a/html_components/edit-item.go b/html_components/edit-item.go index cf3efd4..48a8164 100644 --- a/html_components/edit-item.go +++ b/html_components/edit-item.go @@ -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 diff --git a/html_components/item-display.go b/html_components/item-display.go index dae3982..614b1a0 100644 --- a/html_components/item-display.go +++ b/html_components/item-display.go @@ -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 } diff --git a/html_components/table-prototype.go b/html_components/table-prototype.go index 791dc2e..bd21f9e 100644 --- a/html_components/table-prototype.go +++ b/html_components/table-prototype.go @@ -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, diff --git a/html_components/table.go b/html_components/table.go index 12bcb91..98f7308 100644 --- a/html_components/table.go +++ b/html_components/table.go @@ -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 } diff --git a/schema.prisma b/schema.prisma index c38e5a5..f8087f3 100644 --- a/schema.prisma +++ b/schema.prisma @@ -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 diff --git a/scripts/db-push.sh b/scripts/db-push.sh index e3cd704..653b48b 100644 --- a/scripts/db-push.sh +++ b/scripts/db-push.sh @@ -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 diff --git a/todo/todo-repository_gen.go b/todo/todo-repository_gen.go index d9df717..2e40640 100644 --- a/todo/todo-repository_gen.go +++ b/todo/todo-repository_gen.go @@ -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:] -} diff --git a/todo/todo-rest-crud_gen.go b/todo/todo-rest-crud_gen.go index 188a7e1..203096d 100644 --- a/todo/todo-rest-crud_gen.go +++ b/todo/todo-rest-crud_gen.go @@ -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", diff --git a/todo/todo_gen.go b/todo/todo_gen.go index fc53990..efc7c16 100644 --- a/todo/todo_gen.go +++ b/todo/todo_gen.go @@ -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, ", ", diff --git a/user/user-repository_gen.go b/user/user-repository_gen.go index b4bce5a..1a07743 100644 --- a/user/user-repository_gen.go +++ b/user/user-repository_gen.go @@ -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).