add envelop

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-13 13:41:29 +02:00
commit 1f3ed998df
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
8 changed files with 363 additions and 19 deletions

View file

@ -18,13 +18,22 @@ func NewAccountHandler(s *store.Store) *AccountHandler {
}
func (h *AccountHandler) RegisterRoutes(mux *http.ServeMux) {
// Comptes maîtres
mux.HandleFunc("GET /accounts", h.list)
mux.HandleFunc("POST /accounts", h.create)
mux.HandleFunc("GET /accounts/{id}", h.get)
mux.HandleFunc("PUT /accounts/{id}", h.update)
mux.HandleFunc("DELETE /accounts/{id}", h.delete)
// Enveloppes (sous-comptes)
mux.HandleFunc("GET /accounts/{id}/envelopes", h.listEnvelopes)
mux.HandleFunc("POST /accounts/{id}/envelopes", h.createEnvelope)
mux.HandleFunc("PUT /envelopes/{id}", h.updateEnvelope)
mux.HandleFunc("DELETE /envelopes/{id}", h.deleteEnvelope)
}
// --- Comptes maîtres ---
func (h *AccountHandler) list(w http.ResponseWriter, r *http.Request) {
accounts, err := h.store.ListAccounts(r.Context())
if err != nil {
@ -40,7 +49,7 @@ func (h *AccountHandler) get(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "invalid id")
return
}
account, err := h.store.GetAccount(r.Context(), id)
account, err := h.store.GetAccountWithEnvelopes(r.Context(), id)
if errors.Is(err, pgx.ErrNoRows) {
writeError(w, http.StatusNotFound, "account not found")
return
@ -62,9 +71,6 @@ func (h *AccountHandler) create(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "nom and type are required")
return
}
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
account, err := h.store.CreateAccount(r.Context(), p)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to create account")
@ -84,9 +90,6 @@ func (h *AccountHandler) update(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
account, err := h.store.UpdateAccount(r.Context(), id, p)
if errors.Is(err, pgx.ErrNoRows) {
writeError(w, http.StatusNotFound, "account not found")
@ -111,3 +114,86 @@ func (h *AccountHandler) delete(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusNoContent)
}
// --- Enveloppes ---
func (h *AccountHandler) listEnvelopes(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid id")
return
}
envelopes, err := h.store.ListEnvelopes(r.Context(), id)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to list envelopes")
return
}
writeJSON(w, http.StatusOK, envelopes)
}
func (h *AccountHandler) createEnvelope(w http.ResponseWriter, r *http.Request) {
masterID, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid id")
return
}
var p store.CreateEnvelopeParams
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if p.Nom == "" {
writeError(w, http.StatusBadRequest, "nom is required")
return
}
envelope, err := h.store.CreateEnvelope(r.Context(), masterID, p)
if errors.Is(err, pgx.ErrNoRows) {
writeError(w, http.StatusNotFound, "account not found")
return
}
if errors.Is(err, store.ErrMasterIsSubAccount) {
writeError(w, http.StatusBadRequest, "account cannot itself be an envelope")
return
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to create envelope")
return
}
writeJSON(w, http.StatusCreated, envelope)
}
func (h *AccountHandler) updateEnvelope(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid id")
return
}
var p store.CreateEnvelopeParams
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
envelope, err := h.store.UpdateEnvelope(r.Context(), id, p)
if errors.Is(err, pgx.ErrNoRows) {
writeError(w, http.StatusNotFound, "envelope not found")
return
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update envelope")
return
}
writeJSON(w, http.StatusOK, envelope)
}
func (h *AccountHandler) deleteEnvelope(w http.ResponseWriter, r *http.Request) {
id, err := parseID(r)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid id")
return
}
if err := h.store.DeleteAccount(r.Context(), id); err != nil {
writeError(w, http.StatusInternalServerError, "failed to delete envelope")
return
}
w.WriteHeader(http.StatusNoContent)
}

View file

@ -2,16 +2,19 @@ package store
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
type Account struct {
ID int32 `json:"id"`
Nom string `json:"nom"`
Type string `json:"type"`
DeviseReference string `json:"devise_reference"`
ID int32 `json:"id"`
Nom string `json:"nom"`
Type string `json:"type"`
DeviseReference string `json:"devise_reference"`
Plafond *float64 `json:"plafond,omitempty"`
MasterAccountID *int32 `json:"master_account_id,omitempty"`
Objectif *string `json:"objectif,omitempty"`
}
type CreateAccountParams struct {
@ -21,9 +24,18 @@ type CreateAccountParams struct {
Plafond *float64 `json:"plafond,omitempty"`
}
type CreateEnvelopeParams struct {
Nom string `json:"nom"`
Objectif *string `json:"objectif,omitempty"`
}
var ErrMasterIsSubAccount = errors.New("master account cannot itself be a sub-account")
const accountCols = `id, nom, type, devise_reference, plafond, master_account_id, objectif`
func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
rows, err := s.pool.Query(ctx,
`SELECT id, nom, type, devise_reference, plafond FROM account ORDER BY nom`)
`SELECT `+accountCols+` FROM account WHERE master_account_id IS NULL ORDER BY nom`)
if err != nil {
return nil, err
}
@ -32,18 +44,50 @@ func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
func (s *Store) GetAccount(ctx context.Context, id int32) (Account, error) {
rows, err := s.pool.Query(ctx,
`SELECT id, nom, type, devise_reference, plafond FROM account WHERE id = $1`, id)
`SELECT `+accountCols+` FROM account WHERE id = $1`, id)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Account, error) {
func (s *Store) ListEnvelopes(ctx context.Context, masterID int32) ([]Account, error) {
rows, err := s.pool.Query(ctx,
`INSERT INTO account (nom, type, devise_reference, plafond, taux)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, nom, type, devise_reference, plafond`,
`SELECT `+accountCols+` FROM account WHERE master_account_id = $1 ORDER BY nom`, masterID)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, pgx.RowToStructByName[Account])
}
type AccountWithEnvelopes struct {
Account
Envelopes []Account `json:"envelopes"`
}
func (s *Store) GetAccountWithEnvelopes(ctx context.Context, id int32) (AccountWithEnvelopes, error) {
account, err := s.GetAccount(ctx, id)
if err != nil {
return AccountWithEnvelopes{}, err
}
envelopes, err := s.ListEnvelopes(ctx, id)
if err != nil {
return AccountWithEnvelopes{}, err
}
if envelopes == nil {
envelopes = []Account{}
}
return AccountWithEnvelopes{Account: account, Envelopes: envelopes}, nil
}
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Account, error) {
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
rows, err := s.pool.Query(ctx,
`INSERT INTO account (nom, type, devise_reference, plafond)
VALUES ($1, $2, $3, $4)
RETURNING `+accountCols,
p.Nom, p.Type, p.DeviseReference, p.Plafond)
if err != nil {
return Account{}, err
@ -51,11 +95,33 @@ func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Accou
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) CreateEnvelope(ctx context.Context, masterID int32, p CreateEnvelopeParams) (Account, error) {
master, err := s.GetAccount(ctx, masterID)
if err != nil {
return Account{}, err
}
if master.MasterAccountID != nil {
return Account{}, ErrMasterIsSubAccount
}
rows, err := s.pool.Query(ctx,
`INSERT INTO account (nom, type, devise_reference, master_account_id, objectif)
VALUES ($1, $2, $3, $4, $5)
RETURNING `+accountCols,
p.Nom, master.Type, master.DeviseReference, masterID, p.Objectif)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) UpdateAccount(ctx context.Context, id int32, p CreateAccountParams) (Account, error) {
if p.DeviseReference == "" {
p.DeviseReference = "EUR"
}
rows, err := s.pool.Query(ctx,
`UPDATE account SET nom = $2, type = $3, devise_reference = $4, plafond = $5
WHERE id = $1
RETURNING id, nom, type, devise_reference, plafond`,
WHERE id = $1 AND master_account_id IS NULL
RETURNING `+accountCols,
id, p.Nom, p.Type, p.DeviseReference, p.Plafond)
if err != nil {
return Account{}, err
@ -63,6 +129,18 @@ func (s *Store) UpdateAccount(ctx context.Context, id int32, p CreateAccountPara
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) UpdateEnvelope(ctx context.Context, id int32, p CreateEnvelopeParams) (Account, error) {
rows, err := s.pool.Query(ctx,
`UPDATE account SET nom = $2, objectif = $3
WHERE id = $1 AND master_account_id IS NOT NULL
RETURNING `+accountCols,
id, p.Nom, p.Objectif)
if err != nil {
return Account{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
}
func (s *Store) DeleteAccount(ctx context.Context, id int32) error {
_, err := s.pool.Exec(ctx, `DELETE FROM account WHERE id = $1`, id)
return err