Skip to content

Commit

Permalink
redirect after create employee
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquepw committed Sep 6, 2024
1 parent 8db397d commit 1f66b8e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 30 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ watch/sync_assets:
.PHONY: watch
watch:
make -j4 watch/templ watch/server watch/tailwind watch/sync_assets

4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"

Expand All @@ -19,8 +20,7 @@ func main() {

db, err := sql.Open("libsql", os.Getenv("DB_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open db: %s", err)
os.Exit(1)
log.Fatalf("failed to open db: %s", err.Error())
}
defer db.Close()

Expand Down
21 changes: 14 additions & 7 deletions database/employee_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"database/sql"
"time"

"github.com/henriquepw/imperium-tattoo/web"
"github.com/henriquepw/imperium-tattoo/web/types"
Expand All @@ -11,7 +12,7 @@ import (
type EmployeeRepo interface {
Insert(ctx context.Context, payload types.EmployeeCreateDTO) (*string, error)
List(ctx context.Context) ([]types.Employee, error)
CheckEmail(ctx context.Context, email string) bool
HasEmail(ctx context.Context, email string) bool
}

type repo struct {
Expand All @@ -34,28 +35,30 @@ func (r repo) Insert(ctx context.Context, payload types.EmployeeCreateDTO) (*str
}
defer tx.Rollback()

now := time.Now().UnixMilli()
_, err = tx.QueryContext(
ctx,
"INSERT INTO employee (id, name, email, roles) VALUES ($1, $2, $3, $4)",
"INSERT INTO employee (id, name, email, roles, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
id,
payload.Name,
payload.Email,
payload.Roles,
now,
now,
)
if err != nil {
return nil, err
}

_, err = tx.QueryContext(
ctx,
"INSERT INTO credential (id, secret) VALUES ($1, $2)",
"INSERT INTO credential (id, secret) VALUES (?, ?)",
payload.Email,
payload.Password,
)
if err != nil {
return nil, err
}

if err = tx.Commit(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,7 +86,11 @@ func (r repo) List(ctx context.Context) ([]types.Employee, error) {
return items, nil
}

func (r repo) CheckEmail(ctx context.Context, email string) bool {
row := r.db.QueryRowContext(ctx, "SELECT COUNT(1) FROM employee WHERE email = ?", email)
return row.Err() == nil
func (r repo) HasEmail(ctx context.Context, email string) bool {
rows, err := r.db.QueryContext(ctx, "SELECT id FROM employee WHERE email = ?", email)
if err != nil {
return false
}

return rows.Next()
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/henriquepw/imperium-tattoo
go 1.23.1

require (
github.com/a-h/templ v0.2.747
github.com/a-h/templ v0.2.771
github.com/go-playground/validator/v10 v10.22.0
github.com/joho/godotenv v1.5.1
github.com/matoous/go-nanoid/v2 v2.1.0
github.com/tursodatabase/libsql-client-go v0.0.0-20240812094001-348a4e45b535
golang.org/x/crypto v0.22.0
golang.org/x/crypto v0.26.0
)

require (
Expand All @@ -19,7 +19,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
github.com/a-h/templ v0.2.771 h1:4KH5ykNigYGGpCe0fRJ7/hzwz72k3qFqIiiLLJskbSo=
github.com/a-h/templ v0.2.771/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
Expand Down Expand Up @@ -30,15 +30,15 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tursodatabase/libsql-client-go v0.0.0-20240812094001-348a4e45b535 h1:iLjJLq2A5J6L9zrhyNn+fpmxFvtEpYB4XLMr0rX3epI=
github.com/tursodatabase/libsql-client-go v0.0.0-20240812094001-348a4e45b535/go.mod h1:l8xTsYB90uaVdMHXMCxKKLSgw5wLYBwBKKefNIUnm9s=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion web/handler/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func (h EmployeeHandler) EmployeeCreateAction(w http.ResponseWriter, r *http.Req
return
}

http.Redirect(w, r, "/employees", http.StatusSeeOther)
web.Redirect(w, "/employees")
}
6 changes: 6 additions & 0 deletions web/renders.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"log/slog"
"net/http"

"github.com/a-h/templ"
Expand All @@ -19,6 +20,7 @@ func Render(w http.ResponseWriter, r *http.Request, statusCode int, t templ.Comp
}

func RenderError(w http.ResponseWriter, r *http.Request, err error, t func(e ServerError) templ.Component) error {
slog.Error("render error", "error", err.Error())
if e, ok := err.(ServerError); ok {
if e.Errors != nil {
return Render(w, r, e.StatusCode, t(e))
Expand All @@ -35,3 +37,7 @@ func RenderError(w http.ResponseWriter, r *http.Request, err error, t func(e Ser
w.Write([]byte("Houve um erro inesperado"))
return nil
}

func Redirect(w http.ResponseWriter, to string) {
w.Header().Add("HX-Location", to)
}
2 changes: 1 addition & 1 deletion web/service/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *EmployeeSvc) CreateEmployee(ctx context.Context, payload types.Employee
return nil, err
}

if s.repo.CheckEmail(ctx, payload.Email) {
if s.repo.HasEmail(ctx, payload.Email) {
return nil, web.InvalidRequestDataError(map[string]string{"email": "Email já cadastrado"})
}

Expand Down
9 changes: 5 additions & 4 deletions web/view/layout/dashboard.templ
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ templ Dashbaord(title, route string, boosted bool) {
{ children... }
</main>
<aside
class="fixed bg-agray-1 backdrop-blur-xl inset-0 z-50 transition-opacity duration-300 opacity-0 pointer-events-none"
class="flex flex-row fixed bg-agray-1 backdrop-blur-xl inset-0 z-50 transition-opacity duration-300 opacity-0 pointer-events-none"
:class="menu ? 'opacity-100 pointer-events-auto':''"
>
<nav
class="h-full w-full max-w-60 bg-gray-2 p-4 shadow shadow-agray-2 -translate-x-[100%] transition-transform duration-500"
class="h-full w-full max-w-60 bg-gray-2 p-4 md:p-8 md:pt-10 shadow shadow-agray-2 -translate-x-[100%] transition-transform duration-500"
:class="menu ? 'translate-x-0':''"
>
<header class="h-9 mb-4 flex flex-col justify-center">
<button
class="transition-colors hover:bg-agray-3 active:bg-agray-5 rounded h-8 w-8 flex items-center justify-center -ml-2"
@click="menu = !menu"
class="transition-colors hover:bg-agray-3 active:bg-agray-5 rounded h-8 w-8 flex items-center justify-center -ml-1"
@click="menu = false"
>
<i data-feather="x"></i>
</button>
Expand All @@ -38,6 +38,7 @@ templ Dashbaord(title, route string, boosted bool) {
@item("Funcionários", "users", "/employees", route == "employees")
</ul>
</nav>
<div class="flex-1 opacity-0" @click="menu = false"></div>
</aside>
</div>
}
Expand Down

0 comments on commit 1f66b8e

Please sign in to comment.