-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: metadata update route & object support Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * feat: metadata OAS addition Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: conflict resolution Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com>
- Loading branch information
1 parent
ae1fa5e
commit 3a7e608
Showing
7 changed files
with
129 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type updateMetadataRepository interface { | ||
UpdatePaymentMetadata(ctx context.Context, paymentID uuid.UUID, metadata map[string]string) error | ||
} | ||
|
||
type updateMetadataRequest map[string]string | ||
|
||
func updateMetadataHandler(repo updateMetadataRepository) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
paymentID, err := uuid.Parse(mux.Vars(r)["paymentID"]) | ||
if err != nil { | ||
handleErrorBadRequest(w, r, err) | ||
|
||
return | ||
} | ||
|
||
var metadata updateMetadataRequest | ||
|
||
if r.ContentLength == 0 { | ||
handleErrorBadRequest(w, r, errors.New("body is required")) | ||
|
||
return | ||
} | ||
|
||
err = json.NewDecoder(r.Body).Decode(&metadata) | ||
if err != nil { | ||
handleError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
err = repo.UpdatePaymentMetadata(r.Context(), paymentID, metadata) | ||
if err != nil { | ||
handleError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/formancehq/payments/internal/app/models" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func (s *Storage) UpdatePaymentMetadata(ctx context.Context, paymentID uuid.UUID, metadata map[string]string) error { | ||
var metadataToInsert []models.Metadata // nolint:prealloc // it's against a map | ||
|
||
for key, value := range metadata { | ||
metadataToInsert = append(metadataToInsert, models.Metadata{ | ||
PaymentID: paymentID, | ||
Key: key, | ||
Value: value, | ||
Changelog: []models.MetadataChangelog{ | ||
{ | ||
CreatedAt: time.Now(), | ||
Value: value, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
_, err := s.db.NewInsert(). | ||
Model(&metadataToInsert). | ||
On("CONFLICT (payment_id, key) DO UPDATE"). | ||
Set("value = EXCLUDED.value"). | ||
Set("changelog = metadata.changelog || EXCLUDED.changelog"). | ||
Exec(ctx) | ||
if err != nil { | ||
return e("failed to update payment metadata", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters