add owner handle
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
0f2d7126eb
commit
f862abe5a4
12 changed files with 274 additions and 55 deletions
|
|
@ -28,11 +28,22 @@ func (h *TransactionHandler) RegisterRoutes(mux *http.ServeMux) {
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) listForAccount(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
accountID, err := parseID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid account id")
|
||||
return
|
||||
}
|
||||
if err := h.store.VerifyAccountOwner(r.Context(), accountID, ownerID); errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "account not found")
|
||||
return
|
||||
} else if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to verify account")
|
||||
return
|
||||
}
|
||||
f := store.AccountTransactionFilters{
|
||||
Validated: parseBoolParam(r, "validated"),
|
||||
Pending: parseBoolParam(r, "pending"),
|
||||
|
|
@ -48,12 +59,16 @@ func (h *TransactionHandler) listForAccount(w http.ResponseWriter, r *http.Reque
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) get(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := parseTxID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
tx, err := h.store.GetTransaction(r.Context(), id)
|
||||
tx, err := h.store.GetTransaction(r.Context(), id, ownerID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "transaction not found")
|
||||
return
|
||||
|
|
@ -66,6 +81,10 @@ func (h *TransactionHandler) get(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var p store.CreateTransactionParams
|
||||
if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
|
|
@ -79,6 +98,25 @@ func (h *TransactionHandler) create(w http.ResponseWriter, r *http.Request) {
|
|||
writeError(w, http.StatusBadRequest, "date is required")
|
||||
return
|
||||
}
|
||||
// Vérifier que les comptes référencés appartiennent à l'owner.
|
||||
if p.AccountSourceID != nil {
|
||||
if err := h.store.VerifyAccountOwner(r.Context(), *p.AccountSourceID, ownerID); errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "source account not found")
|
||||
return
|
||||
} else if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to verify source account")
|
||||
return
|
||||
}
|
||||
}
|
||||
if p.AccountDestID != nil {
|
||||
if err := h.store.VerifyAccountOwner(r.Context(), *p.AccountDestID, ownerID); errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "dest account not found")
|
||||
return
|
||||
} else if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to verify dest account")
|
||||
return
|
||||
}
|
||||
}
|
||||
tx, err := h.store.CreateTransaction(r.Context(), p)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to create transaction")
|
||||
|
|
@ -88,6 +126,10 @@ func (h *TransactionHandler) create(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := parseTxID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
|
|
@ -102,7 +144,7 @@ func (h *TransactionHandler) update(w http.ResponseWriter, r *http.Request) {
|
|||
writeError(w, http.StatusBadRequest, "label is required")
|
||||
return
|
||||
}
|
||||
tx, err := h.store.UpdateTransaction(r.Context(), id, p)
|
||||
tx, err := h.store.UpdateTransaction(r.Context(), id, p, ownerID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "transaction not found")
|
||||
return
|
||||
|
|
@ -115,12 +157,19 @@ func (h *TransactionHandler) update(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := parseTxID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
if err := h.store.DeleteTransaction(r.Context(), id); err != nil {
|
||||
if err := h.store.DeleteTransaction(r.Context(), id, ownerID); errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "transaction not found")
|
||||
return
|
||||
} else if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to delete transaction")
|
||||
return
|
||||
}
|
||||
|
|
@ -128,6 +177,10 @@ func (h *TransactionHandler) delete(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h *TransactionHandler) validate(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := requireOwner(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := parseTxID(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
|
|
@ -140,7 +193,7 @@ func (h *TransactionHandler) validate(w http.ResponseWriter, r *http.Request) {
|
|||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
tx, err := h.store.SetValidated(r.Context(), id, body.Validated)
|
||||
tx, err := h.store.SetValidated(r.Context(), id, body.Validated, ownerID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
writeError(w, http.StatusNotFound, "transaction not found")
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue