From 6dfd8b29e7ff82ea2ce0f34ab8d177c3efe8c86f Mon Sep 17 00:00:00 2001 From: Ersteller Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] Update --- Dockerfile | 3 --- crud/tracing.go | 14 ++++++++++++++ crud/util.go | 7 +++++++ html_components/table-prototype.go | 4 ++-- html_components/table.go | 2 +- schema.prisma | 1 + scripts/db-push.sh | 2 +- todo/todo-repository_gen.go | 27 +++++++++++++++++++++------ todo/todo-rest-crud_gen.go | 27 ++++++++++++++++++++++++--- todo/todo_gen.go | 4 +++- user/user-repository_gen.go | 4 ---- user/user_gen.go | 2 +- 12 files changed, 75 insertions(+), 22 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/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..decb890 100644 --- a/html_components/table.go +++ b/html_components/table.go @@ -31,7 +31,7 @@ type Pagination struct { NextPage int } -type Table struct { +type GohtmlTable struct { Headers []string Rows []TableRow EntityUrl string diff --git a/schema.prisma b/schema.prisma index 38466cc..c38e5a5 100644 --- a/schema.prisma +++ b/schema.prisma @@ -25,6 +25,7 @@ model user { model todo { id Int @id @default(autoincrement()) name String @default("") + completed Boolean @default(false) 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 bf43dc0..18e7d4e 100644 --- a/todo/todo-repository_gen.go +++ b/todo/todo-repository_gen.go @@ -37,6 +37,7 @@ func (r *TodoRepository) Create(todo Todo) (int, error) { "updated_at": time.Now(), "name": todo.Name, + "completed": todo.Completed, "user_id": todo.UserId, }). Returning("id"). @@ -78,7 +79,7 @@ func (e TodoAlreadyExistsError) Error() string { func (r *TodoRepository) getSelectColumns() []any { return []any{"id", "created_at", "updated_at", - "name", "user_id", + "name", "completed", "user_id", } } @@ -121,6 +122,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error) &item.CreatedAt, &item.UpdatedAt, &item.Name, + &item.Completed, &item.UserId, ) if err != nil { @@ -132,6 +134,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error) &item.CreatedAt, &item.UpdatedAt, &item.Name, + &item.Completed, &item.UserId, ) if err != nil { @@ -143,6 +146,7 @@ func (r *TodoRepository) rowToItem(rows pgx.Rows, rowId bool) (Todo, int, error) CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, Name: item.Name, + Completed: item.Completed, UserId: item.UserId, }, item.Count, nil } @@ -154,6 +158,7 @@ func (r *TodoRepository) Update(userId int, todo Todo) error { "updated_at": time.Now(), "name": todo.Name, + "completed": todo.Completed, "user_id": todo.UserId, }). Where(goqu.Ex{ @@ -200,7 +205,8 @@ func (r *TodoRepository) Delete(userId int, id int) error { type TodoField string const ( - TodoFieldName TodoField = "name" + TodoFieldName TodoField = "name" + TodoFieldCompleted TodoField = "completed" ) type TodoNameFilter struct { @@ -208,6 +214,11 @@ type TodoNameFilter struct { Value string } +type TodoCompletedFilter struct { + Active bool + Value bool +} + type TodoOrderDirection string const ( @@ -227,6 +238,8 @@ type TodoPaginationParams struct { NameFilter TodoNameFilter + CompletedFilter TodoCompletedFilter + References TodoReferences } @@ -296,6 +309,12 @@ func (r *TodoRepository) addPageFilters(params TodoPaginationParams, whereExpres }) } + if params.CompletedFilter.Active { + whereExpressions = append(whereExpressions, goqu.Ex{ + "completed": params.CompletedFilter.Value, + }) + } + return whereExpressions } @@ -306,7 +325,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 1bc8c10..203096d 100644 --- a/todo/todo-rest-crud_gen.go +++ b/todo/todo-rest-crud_gen.go @@ -57,6 +57,8 @@ func (i *TodoCrud) GetItem(c Context) error { Columns: []ItemDisplayColumn{ {Label: "Name", Value: item.Name, Type: ItemDisplayTypeText}, + + {Label: "Completed", Value: fmt.Sprint(item.Completed), Type: ItemDisplayTypeText}, }, SubItems: i.getSubItemDisplays(item, c), EditItemUrl: fmt.Sprint(i.GetEntityUrl(c), "/", item.Id, "/edit", queryString), @@ -80,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 @@ -115,6 +117,8 @@ func (i *TodoCrud) CreateNewItemInputs(c Context) error { inputs := []EditItemInputs{ {Label: "Name", Value: "", Name: "Name", Type: InputTypeText, Options: []SelectInputOption{}}, + + {Label: "Completed", Value: "", Name: "Completed", Type: InputTypeBool, Options: []SelectInputOption{}}, } url := fmt.Sprint(i.GetEntityUrl(c), "?", c.QueryString()) @@ -140,6 +144,8 @@ func (i *TodoCrud) CreateItem(c Context) error { UserId: userId, Name: c.FormValue("Name"), + + Completed: ParseCheckboxWithDefault(c.FormValue("Completed"), false), } _, err := i.repo.Create(item) if err != nil { @@ -158,6 +164,8 @@ func (i *TodoCrud) EditItem(c Context) error { inputs := []EditItemInputs{ {Label: "Name", Value: item.Name, Name: "Name", Type: InputTypeText, Options: []SelectInputOption{}}, + + {Label: "Completed", Value: fmt.Sprint(item.Completed), Name: "Completed", Type: InputTypeBool, Options: []SelectInputOption{}}, } path := fmt.Sprint(i.GetEntityUrl(c), "/", item.Id) queryString := GetCurrentUrlQueryParams(c) @@ -193,6 +201,8 @@ func (i *TodoCrud) UpdateItem(c Context) error { item.Name = c.FormValue("Name") + item.Completed = ParseCheckboxWithDefault(c.FormValue("Completed"), false) + err = i.repo.Update(userId, item) if err != nil { return err @@ -236,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 @@ -244,10 +254,12 @@ func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) Table { if itemEnd > count { itemEnd = count } - return Table{ + return GohtmlTable{ Headers: []string{ "Name", + + "Completed", }, Rows: i.structsToTableRows(c, items), EntityUrl: i.GetEntityUrl(c), @@ -262,6 +274,8 @@ func (i *TodoCrud) itemsToTable(c Context, items []Todo, count int) Table { Options: []SelectInputOption{ {Label: "Name filter", Value: "Name", Selected: filter == "Name"}, + + {Label: "Completed filter", Value: "Completed", Selected: filter == "Completed"}, }, }, Pagination: Pagination{ @@ -293,6 +307,8 @@ func (i *TodoCrud) structToRow(c Context, item Todo) TableRow { Columns: []TableColumn{ {Value: item.Name, Type: TableColumnTypeText}, + + {Value: fmt.Sprint(item.Completed), Type: TableColumnTypeText}, }, EntityUrl: i.GetEntityUrl(c), EditItemUrl: fmt.Sprint(i.GetEntityUrl(c), "/", item.Id, "/edit"), @@ -324,6 +340,11 @@ func (i *TodoCrud) getPage(c Context) ([]Todo, int, error) { Value: filterValue, }, + CompletedFilter: TodoCompletedFilter{ + Active: filter == "Completed", + Value: ParseBoolWithDefault(filterValue, false), + }, + References: TodoReferences{ UserId: userId, }, diff --git a/todo/todo_gen.go b/todo/todo_gen.go index f267e18..f0a9895 100644 --- a/todo/todo_gen.go +++ b/todo/todo_gen.go @@ -11,15 +11,17 @@ import ( type Todo struct { Id int `db:"id"` Name string `db:"name"` + Completed bool `db:"completed"` UserId int `db:"user_id"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` } -func (s *Todo) String() string { +func (s Todo) String() string { return fmt.Sprint("Todo{ ", "Id: ", s.Id, ", ", "Name: ", s.Name, ", ", + "Completed: ", s.Completed, ", ", "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). diff --git a/user/user_gen.go b/user/user_gen.go index a34ebcb..745f6c7 100644 --- a/user/user_gen.go +++ b/user/user_gen.go @@ -16,7 +16,7 @@ type User struct { UpdatedAt time.Time `db:"updated_at"` } -func (s *User) String() string { +func (s User) String() string { return fmt.Sprint("User{ ", "Id: ", s.Id, ", ", "Email: ", s.Email, ", ",