Skip to content

Commit

Permalink
list client procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquepw committed Nov 4, 2024
1 parent c33a731 commit ab458ba
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
42 changes: 41 additions & 1 deletion web/db/client_procedures_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions web/handlers/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
}

Expand Down
12 changes: 12 additions & 0 deletions web/services/client_procedure_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"log"
"time"

"github.com/henriquepw/imperium-tattoo/pkg/customid"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions web/types/client_procedure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ClientProcedure struct {
ID string
ClientID string
ProcedureID string
Procedure string
Description string
}

Expand Down
31 changes: 24 additions & 7 deletions web/view/pages/client_detail_page.templ
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ templ ClientDetailSection(client types.Client) {
</div>
}

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) {
<div x-data="{editOpen:false}">
@layout.PageHeader(client.Name, []ui.BreadcrumbItem{
Expand All @@ -74,7 +74,7 @@ templ ClientDetailPage(boosted bool, client types.Client, procedures []types.Pro
<section id="client-section" class="space-y-4">
@ClientDetailSection(client)
</section>
@ClientProcessSection(client.ID, procedures)
@ClientProcessSection(client.ID, procedures, clientProcedures)
@ui.Modal("editOpen", "editOpen=false", "Editar Cliente") {
@ClientEditForm(client.ID, types.ClientUpdateDTO{
Name: client.Name,
Expand Down Expand Up @@ -235,23 +235,40 @@ func parseProcedures(items []types.Procedure) string {
return result
}

templ ClientProcessSection(id string, procedures []types.Procedure) {
<section class="mt-6" x-data={ "{ newProcedureOpen: true, procedures:" + parseProcedures(procedures) + " }" }>
templ ClientProcessSection(id string, procedures []types.Procedure, clientProcedures []types.ClientProcedure) {
<section class="mt-6" x-data={ "{ newProcedureOpen: false, procedures:" + parseProcedures(procedures) + " }" }>
<div class="row mb-4">
<h2 class="text-2xl flex-1 font-bold">Procedimentos</h2>
<button type="button" class="btn-primary" @click="newProcedureOpen = true">
@ui.Icon("plus")
</button>
</div>
@ClientProcessTable()
@ClientProcedureList(clientProcedures)
@ui.Modal("newProcedureOpen", "newProcedureOpen=false", "Registrar Procedimento") {
@ClientProcessCreateForm(id, types.ClientProcedureCreateDTO{}, nil)
}
</section>
}

templ ClientProcessTable() {
TODO
templ clientProcedureItem(p types.ClientProcedure) {
<li class="card">{ p.Description }</li>
}

templ ClientProcedureList(procedures []types.ClientProcedure) {
<ul class="space-y-2">
if len(procedures) == 0 {
<li class="card bg-warning-2 border-warning-7">
<div class="text-center text-warning-12">
Nenhum procedimento cadastrado
</div>
</li>
}
for _, i := range procedures {
<tr id={ "client-procedures-" + i.ID } class="group [&.htmx-request]:hidden">
@clientProcedureItem(i)
</tr>
}
</ul>
}

templ ClientProcessCreateForm(id string, values types.ClientProcedureCreateDTO, errors map[string]string) {
Expand Down

0 comments on commit ab458ba

Please sign in to comment.