Skip to content

Commit

Permalink
add client procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquepw committed Nov 3, 2024
1 parent 8f06781 commit c33a731
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 50 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ clean:
@rm -f main



# run templ generation in watch mode to detect all .templ files and
# re-create _templ.txt files on change, then send reload event to browser.
# Default url: http://localhost:7331
Expand Down Expand Up @@ -82,6 +81,6 @@ watch/sync_assets:

# start all 4 watch processes in parallel.
.PHONY: watch
watch:
start:
make -j4 watch/templ watch/server watch/tailwind watch/sync_assets

2 changes: 1 addition & 1 deletion static/css/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}

.input {
@apply rounded-md px-2 h-9 bg-gray-1 border outline-none;
@apply rounded-md min-h-9 px-2 bg-gray-1 border outline-none;
@apply placeholder:text-gray-10;
@apply hover:border-gray-10 active:border-accent-10;
@apply focus:border-accent-10 focus:ring-2 focus:ring-accent-10;
Expand Down
57 changes: 57 additions & 0 deletions web/db/client_procedures_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package db

import (
"context"
"database/sql"

"github.com/henriquepw/imperium-tattoo/pkg/date"
"github.com/henriquepw/imperium-tattoo/web/types"
)

type ClientProcedureStore interface {
Insert(ctx context.Context, item types.ClientProcedure) error
Update(ctx context.Context, id string, dto types.ClientProcedureUpdateDTO) error
List(ctx context.Context, clientID string) ([]types.ClientProcedure, error)
}

type clientProcedureStore struct {
db *sql.DB
}

func NewClientProcedureStore(db *sql.DB) *clientProcedureStore {
return &clientProcedureStore{db}
}

func (s *clientProcedureStore) Insert(ctx context.Context, c types.ClientProcedure) error {
query := `
INSERT INTO client_procedure (
id,
client_id,
procedure_id,
description,
done_at,
created_at,
updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?)
`
_, error := s.db.ExecContext(
ctx, query,
c.ID,
c.ClientID,
c.ProcedureID,
c.Description,
date.FormatToISO(c.DoneAt),
date.FormatToISO(c.CreatedAt),
date.FormatToISO(c.UpdatedAt),
)

return error
}

func (s *clientProcedureStore) List(ctx context.Context, clientID string) ([]types.ClientProcedure, error) {
return []types.ClientProcedure{}, nil
}

func (s *clientProcedureStore) Update(ctx context.Context, clientID string, dto types.ClientProcedureUpdateDTO) error {
return nil
}
55 changes: 46 additions & 9 deletions web/handlers/client_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"log"
"net/http"

"github.com/a-h/templ"
Expand All @@ -13,15 +14,17 @@ import (
)

type ClientHandler struct {
svc services.ClientService
clientSVC services.ClientService
procedureSVC services.ProcedureService
clientProcedureSVC services.ClientProcedureService
}

func NewClientHandler(svc services.ClientService) *ClientHandler {
return &ClientHandler{svc}
func NewClientHandler(client services.ClientService, procedure services.ProcedureService, clientProcedure services.ClientProcedureService) *ClientHandler {
return &ClientHandler{client, procedure, clientProcedure}
}

func (h *ClientHandler) ClientsPage(w http.ResponseWriter, r *http.Request) {
clients, err := h.svc.ListClients(r.Context())
clients, err := h.clientSVC.ListClients(r.Context())
if err != nil {
httputil.RenderError(w, r, err, func(e errors.ServerError) templ.Component {
return pages.ClientsPage(r.Header.Get("HX-Boosted") == "true", nil)
Expand Down Expand Up @@ -53,7 +56,7 @@ func (h *ClientHandler) CreateClientAction(w http.ResponseWriter, r *http.Reques
},
}

client, err := h.svc.CreateClient(r.Context(), payload)
client, err := h.clientSVC.CreateClient(r.Context(), payload)
if err != nil {
httputil.RenderError(w, r, err, func(e errors.ServerError) templ.Component {
return pages.ClientCreateForm(payload, e.Errors)
Expand All @@ -63,20 +66,26 @@ func (h *ClientHandler) CreateClientAction(w http.ResponseWriter, r *http.Reques

httputil.Render(
w, r, http.StatusCreated,
pages.EmployeeCreateForm(types.EmployeeCreateDTO{}, nil),
pages.ClientCreateForm(types.ClientCreateDTO{}, nil),
pages.OobNewClient(*client),
)
}

func (h *ClientHandler) ClientDetailPage(w http.ResponseWriter, r *http.Request) {
client, err := h.svc.GetClientById(r.Context(), r.PathValue("id"))
client, err := h.clientSVC.GetClientById(r.Context(), r.PathValue("id"))
if err != nil {
httputil.RenderError(w, r, err, nil)
return
}

procedures, err := h.procedureSVC.ListProcedures(r.Context())
if err != nil {
httputil.RenderError(w, r, err, nil)
return
}

httputil.RenderPage(w, r, func(b bool) templ.Component {
return pages.ClientDetailPage(b, *client)
return pages.ClientDetailPage(b, *client, procedures)
})
}

Expand Down Expand Up @@ -106,7 +115,7 @@ func (h *ClientHandler) EditClientAction(w http.ResponseWriter, r *http.Request)
},
}

client, err := h.svc.UpdateClinetById(r.Context(), id, payload)
client, err := h.clientSVC.UpdateClinetById(r.Context(), id, payload)
if err != nil {
httputil.RenderError(w, r, err, func(e errors.ServerError) templ.Component {
return pages.ClientEditForm(id, payload, e.Errors)
Expand All @@ -120,3 +129,31 @@ func (h *ClientHandler) EditClientAction(w http.ResponseWriter, r *http.Request)
pages.OobClientUpdated(*client),
)
}

func (h *ClientHandler) CreateClientProcedureAction(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
clientID := r.PathValue("id")
payload := types.ClientProcedureCreateDTO{
ClientID: clientID,
ProcedureID: r.Form.Get("procedureId"),
DoneAt: r.Form.Get("doneAt"),
Description: r.Form.Get("description"),
}

log.Println(payload)
p, err := h.clientProcedureSVC.CreateClientProcedure(r.Context(), payload)
log.Println(p)
if err != nil {
httputil.RenderError(w, r, err, func(e errors.ServerError) templ.Component {
log.Println(payload)
return pages.ClientProcessCreateForm(clientID, payload, e.Errors)
})
return
}

httputil.Render(
w, r, http.StatusCreated,
pages.ClientProcessCreateForm(clientID, payload, nil),
// pages.OobNewClient(*client),
)
}
63 changes: 63 additions & 0 deletions web/services/client_procedure_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package services

import (
"context"
"fmt"
"time"

"github.com/henriquepw/imperium-tattoo/pkg/customid"
"github.com/henriquepw/imperium-tattoo/pkg/errors"
"github.com/henriquepw/imperium-tattoo/pkg/validate"
"github.com/henriquepw/imperium-tattoo/web/db"
"github.com/henriquepw/imperium-tattoo/web/types"
)

type ClientProcedureService interface {
CreateClientProcedure(ctx context.Context, dto types.ClientProcedureCreateDTO) (*types.ClientProcedure, error)
}

type clientProcedureService struct {
store db.ClientProcedureStore
}

func NewClientProcedureService(store db.ClientProcedureStore) *clientProcedureService {
return &clientProcedureService{store}
}

func (s *clientProcedureService) CreateClientProcedure(ctx context.Context, dto types.ClientProcedureCreateDTO) (*types.ClientProcedure, error) {
if err := validate.CheckPayload(dto); err != nil {
return nil, err
}

id, err := customid.New()
if err != nil {
return nil, errors.Internal("Não foi possível inserir o procedimento")
}

// TODO: check if procedure exist
// TODO: check if client exist

doneAt, err := time.Parse(time.DateOnly, dto.DoneAt)
if err != nil {
return nil, errors.InvalidRequestData(map[string]string{"doneAt": "Data inválida"})
}

now := time.Now()
procedure := types.ClientProcedure{
ID: id,
CreatedAt: now,
UpdatedAt: now,
DoneAt: doneAt,
Description: dto.Description,
ClientID: dto.ClientID,
ProcedureID: dto.ProcedureID,
}

err = s.store.Insert(ctx, procedure)
if err != nil {
fmt.Println(err)
return nil, errors.Internal("Não foi possível inserir o procedimento")
}

return &procedure, nil
}
25 changes: 25 additions & 0 deletions web/types/client_procedure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

import "time"

type ClientProcedure struct {
CreatedAt time.Time
UpdatedAt time.Time
DoneAt time.Time
ID string
ClientID string
ProcedureID string
Description string
}

type ClientProcedureCreateDTO struct {
DoneAt string `json:"doneAt" validate:"required"`
ClientID string `json:"clientId" validate:"required,id,len=18"`
ProcedureID string `json:"procedureId" validate:"required,id,len=18"`
Description string `json:"description" validate:"required,min=5"`
}

type ClientProcedureUpdateDTO struct {
DoneAt time.Time `validate:"required"`
Description string `validate:"required,min=5"`
}
13 changes: 0 additions & 13 deletions web/types/employee_procedure.go

This file was deleted.

4 changes: 2 additions & 2 deletions web/types/procedures.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

type Procedure struct {
ID string
Name string
ID string `json:"id"`
Name string `json:"name"`
}
Loading

0 comments on commit c33a731

Please sign in to comment.