diff --git a/web/db/client_procedures_store.go b/web/db/client_procedures_store.go index 4e649a3..aa2f1a9 100644 --- a/web/db/client_procedures_store.go +++ b/web/db/client_procedures_store.go @@ -3,6 +3,7 @@ package db import ( "context" "database/sql" + "time" "github.com/henriquepw/imperium-tattoo/pkg/date" "github.com/henriquepw/imperium-tattoo/web/types" @@ -49,7 +50,46 @@ func (s *clientProcedureStore) Insert(ctx context.Context, c types.ClientProcedu } func (s *clientProcedureStore) List(ctx context.Context, clientID string) ([]types.ClientProcedure, error) { - return []types.ClientProcedure{}, nil + query := ` + SELECT + cp.id, + cp.description, + cp.done_at, + p.name + FROM + client_procedure cp + LEFT JOIN procedure p ON cp.procedure_id = p.id + WHERE + cp.client_id = ? + ` + rows, err := s.db.QueryContext(ctx, query, clientID) + if err != nil { + return nil, err + } + + items := []types.ClientProcedure{} + for rows.Next() { + var i types.ClientProcedure + doneAt := "" + err := rows.Scan( + &i.ID, + &i.Description, + &doneAt, + &i.Procedure, + ) + if err != nil { + return nil, err + } + + i.DoneAt, err = time.Parse(time.RFC3339, doneAt) + if err != nil { + return nil, err + } + + items = append(items, i) + } + + return items, nil } func (s *clientProcedureStore) Update(ctx context.Context, clientID string, dto types.ClientProcedureUpdateDTO) error { diff --git a/web/handlers/client_handler.go b/web/handlers/client_handler.go index e9a8eb8..3c3eb2a 100644 --- a/web/handlers/client_handler.go +++ b/web/handlers/client_handler.go @@ -72,7 +72,9 @@ func (h *ClientHandler) CreateClientAction(w http.ResponseWriter, r *http.Reques } func (h *ClientHandler) ClientDetailPage(w http.ResponseWriter, r *http.Request) { - client, err := h.clientSVC.GetClientById(r.Context(), r.PathValue("id")) + id := r.PathValue("id") + + client, err := h.clientSVC.GetClientById(r.Context(), id) if err != nil { httputil.RenderError(w, r, err, nil) return @@ -84,8 +86,14 @@ func (h *ClientHandler) ClientDetailPage(w http.ResponseWriter, r *http.Request) return } + clientProcedures, err := h.clientProcedureSVC.ListClientProcedures(r.Context(), id) + if err != nil { + httputil.RenderError(w, r, err, nil) + return + } + httputil.RenderPage(w, r, func(b bool) templ.Component { - return pages.ClientDetailPage(b, *client, procedures) + return pages.ClientDetailPage(b, *client, procedures, clientProcedures) }) } diff --git a/web/services/client_procedure_service.go b/web/services/client_procedure_service.go index 90a4501..545be9e 100644 --- a/web/services/client_procedure_service.go +++ b/web/services/client_procedure_service.go @@ -3,6 +3,7 @@ package services import ( "context" "fmt" + "log" "time" "github.com/henriquepw/imperium-tattoo/pkg/customid" @@ -14,6 +15,7 @@ import ( type ClientProcedureService interface { CreateClientProcedure(ctx context.Context, dto types.ClientProcedureCreateDTO) (*types.ClientProcedure, error) + ListClientProcedures(ctx context.Context, clientID string) ([]types.ClientProcedure, error) } type clientProcedureService struct { @@ -61,3 +63,13 @@ func (s *clientProcedureService) CreateClientProcedure(ctx context.Context, dto return &procedure, nil } + +func (s *clientProcedureService) ListClientProcedures(ctx context.Context, clientID string) ([]types.ClientProcedure, error) { + procedures, err := s.store.List(ctx, clientID) + if err != nil { + log.Println(err) + return nil, errors.Internal("Não foi possível listar os procedimentos desse cliente") + } + + return procedures, nil +} diff --git a/web/types/client_procedure.go b/web/types/client_procedure.go index 00d82a1..6bfefdf 100644 --- a/web/types/client_procedure.go +++ b/web/types/client_procedure.go @@ -9,6 +9,7 @@ type ClientProcedure struct { ID string ClientID string ProcedureID string + Procedure string Description string } diff --git a/web/view/pages/client_detail_page.templ b/web/view/pages/client_detail_page.templ index 468cc9d..0e5296d 100644 --- a/web/view/pages/client_detail_page.templ +++ b/web/view/pages/client_detail_page.templ @@ -60,7 +60,7 @@ templ ClientDetailSection(client types.Client) { } -templ ClientDetailPage(boosted bool, client types.Client, procedures []types.Procedure) { +templ ClientDetailPage(boosted bool, client types.Client, procedures []types.Procedure, clientProcedures []types.ClientProcedure) { @layout.Dashbaord(client.Name, boosted) {
@layout.PageHeader(client.Name, []ui.BreadcrumbItem{ @@ -74,7 +74,7 @@ templ ClientDetailPage(boosted bool, client types.Client, procedures []types.Pro
@ClientDetailSection(client)
- @ClientProcessSection(client.ID, procedures) + @ClientProcessSection(client.ID, procedures, clientProcedures) @ui.Modal("editOpen", "editOpen=false", "Editar Cliente") { @ClientEditForm(client.ID, types.ClientUpdateDTO{ Name: client.Name, @@ -235,23 +235,40 @@ func parseProcedures(items []types.Procedure) string { return result } -templ ClientProcessSection(id string, procedures []types.Procedure) { -
+templ ClientProcessSection(id string, procedures []types.Procedure, clientProcedures []types.ClientProcedure) { +

Procedimentos

- @ClientProcessTable() + @ClientProcedureList(clientProcedures) @ui.Modal("newProcedureOpen", "newProcedureOpen=false", "Registrar Procedimento") { @ClientProcessCreateForm(id, types.ClientProcedureCreateDTO{}, nil) }
} -templ ClientProcessTable() { - TODO +templ clientProcedureItem(p types.ClientProcedure) { +
  • { p.Description }
  • +} + +templ ClientProcedureList(procedures []types.ClientProcedure) { + } templ ClientProcessCreateForm(id string, values types.ClientProcedureCreateDTO, errors map[string]string) {