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 /main main.go
RUN go build -o /create-user cli/create-user_gen.go
FROM alpine FROM alpine
ARG USER=default ARG USER=default
@ -31,7 +29,6 @@ USER $USER
WORKDIR $HOME WORKDIR $HOME
COPY --from=build --chown=default:default /main ./main 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 --from=build --chown=default:default /app/.env .env
COPY --chown=default:default views/ ./views COPY --chown=default:default views/ ./views
COPY --chown=default:default static/ ./static/ 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 package html_components
import "maragu.dev/gomponents"
type EditItemInputType string type EditItemInputType string
const ( const (
@ -27,6 +29,7 @@ type EditItem struct {
CancelUrl string CancelUrl string
IsCreate bool IsCreate bool
Inputs []EditItemInputs Inputs []EditItemInputs
ComponentInputs []gomponents.Node
SubmitButtonLabel string SubmitButtonLabel string
HasFileUpload bool HasFileUpload bool
FromTable bool FromTable bool

View File

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

View File

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

View File

@ -1,5 +1,10 @@
package html_components package html_components
import (
"fmt"
"maragu.dev/gomponents"
)
type TableColumnType string type TableColumnType string
const ( const (
@ -12,12 +17,24 @@ type TableColumn struct {
Type TableColumnType Type TableColumnType
} }
type TableComponentColumnsStrikethrough struct {
Active bool
Url string
Value bool
}
type TableComponentColumns struct {
Nodes []gomponents.Node
Strikethrough TableComponentColumnsStrikethrough
}
type TableRow struct { type TableRow struct {
Id string Id string
Columns []TableColumn Columns []TableColumn
EntityUrl string ComponentColumns TableComponentColumns
EditItemUrl string EntityUrl string
DeleteItemUrl string EditItemUrl string
DeleteItemUrl string
} }
type Pagination struct { type Pagination struct {
@ -31,8 +48,27 @@ type Pagination struct {
NextPage int 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 Headers []string
TableHeaders []TableHeader
Rows []TableRow Rows []TableRow
EntityUrl string EntityUrl string
OrderBy string OrderBy string
@ -43,4 +79,6 @@ type Table struct {
ShowBack bool ShowBack bool
BackUrl string BackUrl string
CreateItemUrl string CreateItemUrl string
Sortable TableSortable
QueryParams string
} }

View File

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

View File

@ -1,2 +1,2 @@
#!/bin/bash #!/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(), "updated_at": time.Now(),
"name": todo.Name, "name": todo.Name,
"completed": todo.Completed, "completed": todo.Completed,
"due": r.jsonToString(todo.Due),
"user_id": todo.UserId, "user_id": todo.UserId,
}). }).
Returning("id"). Returning("id").
@ -79,7 +80,7 @@ func (e TodoAlreadyExistsError) Error() string {
func (r *TodoRepository) getSelectColumns() []any { func (r *TodoRepository) getSelectColumns() []any {
return []any{"id", "created_at", "updated_at", 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.UpdatedAt,
&item.Name, &item.Name,
&item.Completed, &item.Completed,
&item.Due,
&item.UserId, &item.UserId,
) )
if err != nil { if err != nil {
@ -135,6 +137,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error)
&item.UpdatedAt, &item.UpdatedAt,
&item.Name, &item.Name,
&item.Completed, &item.Completed,
&item.Due,
&item.UserId, &item.UserId,
) )
if err != nil { if err != nil {
@ -147,6 +150,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error)
UpdatedAt: item.UpdatedAt, UpdatedAt: item.UpdatedAt,
Name: item.Name, Name: item.Name,
Completed: item.Completed, Completed: item.Completed,
Due: item.Due,
UserId: item.UserId, UserId: item.UserId,
}, item.Count, nil }, item.Count, nil
} }
@ -159,6 +163,7 @@ func (r *TodoRepository) Update(userId int, todo Todo) error {
"updated_at": time.Now(), "updated_at": time.Now(),
"name": todo.Name, "name": todo.Name,
"completed": todo.Completed, "completed": todo.Completed,
"due": r.jsonToString(todo.Due),
"user_id": todo.UserId, "user_id": todo.UserId,
}). }).
Where(goqu.Ex{ Where(goqu.Ex{
@ -207,6 +212,7 @@ type TodoField string
const ( const (
TodoFieldName TodoField = "name" TodoFieldName TodoField = "name"
TodoFieldCompleted TodoField = "completed" TodoFieldCompleted TodoField = "completed"
TodoFieldDue TodoField = "due"
) )
type TodoNameFilter struct { type TodoNameFilter struct {
@ -325,7 +331,3 @@ func (r *TodoRepository) jsonToString(jsonData any) string {
} }
return string(bytes) 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 { type TodoDisplay struct {
IsTable bool IsTable bool
Table Table Table GohtmlTable
IsDisplay bool IsDisplay bool
ItemDisplay ItemDisplay ItemDisplay ItemDisplay
IsEdit bool IsEdit bool
@ -246,7 +246,7 @@ func (i *TodoCrud) returnRenderTable(c Context, items []Todo, count int) error {
return i.html.RenderComponent(c, "table", table) 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") filter := c.FormValue("filter")
page := ParseIntWithDefault(c.FormValue("pageNumber"), 1) page := ParseIntWithDefault(c.FormValue("pageNumber"), 1)
index := (page - 1) * 5 index := (page - 1) * 5
@ -254,7 +254,7 @@ func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) Table {
if itemEnd > count { if itemEnd > count {
itemEnd = count itemEnd = count
} }
return Table{ return GohtmlTable{
Headers: []string{ Headers: []string{
"Name", "Name",

View File

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

View File

@ -309,10 +309,6 @@ func (r *UserRepository) jsonToString(jsonData any) string {
return string(bytes) 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) { func (u *UserRepository) DoesUserEmailExist(email string) (bool, error) {
sql, args, _ := u.dialect.From("user"). sql, args, _ := u.dialect.From("user").
Prepared(true). Prepared(true).