-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
528327f
commit 8db397d
Showing
24 changed files
with
311 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/a-h/templ" | ||
"github.com/henriquepw/imperium-tattoo/web" | ||
"github.com/henriquepw/imperium-tattoo/web/service" | ||
"github.com/henriquepw/imperium-tattoo/web/types" | ||
"github.com/henriquepw/imperium-tattoo/web/view/employee" | ||
) | ||
|
||
type EmployeeHandler struct { | ||
svc service.EmployeeService | ||
} | ||
|
||
func NewEmployeeHandler(svc service.EmployeeService) *EmployeeHandler { | ||
return &EmployeeHandler{svc} | ||
} | ||
|
||
func (h EmployeeHandler) EmployeesPage(w http.ResponseWriter, r *http.Request) { | ||
web.RenderPage(w, r, employee.EmployeesPage) | ||
} | ||
|
||
func (h EmployeeHandler) EmployeeCreatePage(w http.ResponseWriter, r *http.Request) { | ||
web.RenderPage(w, r, employee.EmployeeCreatePage) | ||
} | ||
|
||
func (h EmployeeHandler) EmployeeCreateAction(w http.ResponseWriter, r *http.Request) { | ||
r.ParseForm() | ||
payload := types.EmployeeCreateDTO{ | ||
Name: r.Form.Get("name"), | ||
Email: r.Form.Get("email"), | ||
Roles: r.Form.Get("roles"), | ||
Password: r.Form.Get("email"), | ||
} | ||
|
||
_, err := h.svc.CreateEmployee(r.Context(), payload) | ||
if err != nil { | ||
web.RenderError(w, r, err, func(e web.ServerError) templ.Component { | ||
return employee.EmployeeCreateForm(employee.EmployeeCreateFormProps{ | ||
Values: payload, | ||
Errors: e.Errors, | ||
}) | ||
}) | ||
return | ||
} | ||
|
||
http.Redirect(w, r, "/employees", http.StatusSeeOther) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package web | ||
|
||
import ( | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func HashPassword(password string) (string, error) { | ||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) | ||
return string(bytes), err | ||
} | ||
|
||
func VerifyPassword(password, hash string) bool { | ||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/a-h/templ" | ||
) | ||
|
||
func RenderPage(w http.ResponseWriter, r *http.Request, comp func(boosted bool) templ.Component) error { | ||
t := comp(r.Header.Get("HX-Boosted") == "true") | ||
return Render(w, r, http.StatusOK, t) | ||
} | ||
|
||
func Render(w http.ResponseWriter, r *http.Request, statusCode int, t templ.Component) error { | ||
w.WriteHeader(statusCode) | ||
w.Header().Set("Content-Type", "text/html") | ||
|
||
return t.Render(r.Context(), w) | ||
} | ||
|
||
func RenderError(w http.ResponseWriter, r *http.Request, err error, t func(e ServerError) templ.Component) error { | ||
if e, ok := err.(ServerError); ok { | ||
if e.Errors != nil { | ||
return Render(w, r, e.StatusCode, t(e)) | ||
} | ||
|
||
w.WriteHeader(e.StatusCode) | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.Write([]byte(e.Message)) | ||
return nil | ||
} | ||
|
||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.Write([]byte("Houve um erro inesperado")) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/henriquepw/imperium-tattoo/database" | ||
"github.com/henriquepw/imperium-tattoo/web" | ||
"github.com/henriquepw/imperium-tattoo/web/types" | ||
) | ||
|
||
type EmployeeService interface { | ||
CreateEmployee(ctx context.Context, payload types.EmployeeCreateDTO) (*string, error) | ||
} | ||
|
||
type EmployeeSvc struct { | ||
repo database.EmployeeRepo | ||
} | ||
|
||
func NewEmployeeService(repo database.EmployeeRepo) *EmployeeSvc { | ||
return &EmployeeSvc{repo} | ||
} | ||
|
||
func (s *EmployeeSvc) CreateEmployee(ctx context.Context, payload types.EmployeeCreateDTO) (*string, error) { | ||
if err := web.CheckPayload(payload); err != nil { | ||
return nil, err | ||
} | ||
|
||
if s.repo.CheckEmail(ctx, payload.Email) { | ||
return nil, web.InvalidRequestDataError(map[string]string{"email": "Email já cadastrado"}) | ||
} | ||
|
||
hash, err := web.HashPassword(payload.Password) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
id, err := s.repo.Insert(ctx, types.EmployeeCreateDTO{ | ||
Name: payload.Name, | ||
Email: payload.Email, | ||
Roles: payload.Roles, | ||
Password: hash, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return id, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
package client | ||
|
||
import "github.com/henriquepw/imperium-tattoo/web/view/layout" | ||
import ( | ||
"github.com/henriquepw/imperium-tattoo/web/view/layout" | ||
"github.com/henriquepw/imperium-tattoo/web/view/ui" | ||
) | ||
|
||
templ ClientsPage() { | ||
@layout.Dashbaord("Painel", "clients", false) { | ||
<div> | ||
MAIN | ||
</div> | ||
@layout.PageHeader( | ||
"Clientes", | ||
[]ui.BreadcrumbItem{ | ||
{Label: "Clientes", Href: "/clients"}, | ||
}, | ||
) | ||
<section> | ||
CLIENTS | ||
</section> | ||
} | ||
} |
Oops, something went wrong.