Skip to content

Commit

Permalink
feat(SPV-1341): change delete paymail endpoint to be more restful (#854)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Kowalski <155538368+jakubmkowalski@users.noreply.github.com>
  • Loading branch information
dzolt-4chain and jakubmkowalski authored Jan 20, 2025
1 parent bd6d183 commit b06ac5a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 35 deletions.
15 changes: 3 additions & 12 deletions actions/admin/paymail_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func paymailCreateAddress(c *gin.Context, _ *reqctx.AdminContext) {
// @Description Delete paymail
// @Tags Admin
// @Produce json
// @Param PaymailAddress body PaymailAddress false "PaymailAddress model containing paymail address to delete"
// @Param id path string true "id of the paymail"
// @Success 200
// @Failure 400 "Bad request - Error while parsing PaymailAddress from request body or if address is missing"
// @Failure 500 "Internal Server Error - Error while deleting paymail address"
Expand All @@ -170,21 +170,12 @@ func paymailCreateAddress(c *gin.Context, _ *reqctx.AdminContext) {
func paymailDeleteAddress(c *gin.Context, _ *reqctx.AdminContext) {
logger := reqctx.Logger(c)
engine := reqctx.Engine(c)
var requestBody PaymailAddress
if err := c.ShouldBindJSON(&requestBody); err != nil {
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.WithTrace(err), logger)
return
}

if requestBody.Address == "" {
spverrors.ErrorResponse(c, spverrors.ErrMissingAddress, logger)
return
}
id := c.Param("id")

opts := engine.DefaultModelOptions()

// Delete a new paymail address
err := engine.DeletePaymailAddress(c.Request.Context(), requestBody.Address, opts...)
err := engine.DeletePaymailAddressByID(c.Request.Context(), id, opts...)
if err != nil {
spverrors.ErrorResponse(c, spverrors.ErrDeletePaymailAddress.WithTrace(err), logger)
return
Expand Down
6 changes: 0 additions & 6 deletions actions/admin/paymail_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ func TestPaymailLivecycle(t *testing.T) {

// when:
res, _ := client.R().
SetBody(map[string]any{
"address": newPaymail,
}).
Delete("/api/v1/admin/paymails/" + testState.newPaymailID)

// then:
Expand All @@ -217,9 +214,6 @@ func TestPaymailLivecycle(t *testing.T) {

// when:
res, _ := client.R().
SetBody(map[string]any{
"address": newPaymail,
}).
Delete("/api/v1/admin/paymails/" + testState.newPaymailID)

// then:
Expand Down
11 changes: 5 additions & 6 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,11 @@ const docTemplate = `{
"summary": "Delete paymail",
"parameters": [
{
"description": "PaymailAddress model containing paymail address to delete",
"name": "PaymailAddress",
"in": "body",
"schema": {
"$ref": "#/definitions/admin.PaymailAddress"
}
"type": "string",
"description": "id of the paymail",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
Expand Down
11 changes: 5 additions & 6 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,11 @@
"summary": "Delete paymail",
"parameters": [
{
"description": "PaymailAddress model containing paymail address to delete",
"name": "PaymailAddress",
"in": "body",
"schema": {
"$ref": "#/definitions/admin.PaymailAddress"
}
"type": "string",
"description": "id of the paymail",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
Expand Down
10 changes: 5 additions & 5 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3154,11 +3154,11 @@ paths:
delete:
description: Delete paymail
parameters:
- description: PaymailAddress model containing paymail address to delete
in: body
name: PaymailAddress
schema:
$ref: '#/definitions/admin.PaymailAddress'
- description: id of the paymail
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
Expand Down
22 changes: 22 additions & 0 deletions engine/action_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, public
return paymailAddress, nil
}

// DeletePaymailAddressByID will delete a paymail address by its id
func (c *Client) DeletePaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) error {

// Get the paymail address
paymailAddress, err := getPaymailAddressByID(ctx, id, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return err
} else if paymailAddress == nil {
return spverrors.ErrCouldNotFindPaymail
}

paymailAddress.DeletedAt.Valid = true
paymailAddress.DeletedAt.Time = time.Now()

tx := c.Datastore().DB().Save(&paymailAddress)
if tx.Error != nil {
return spverrors.ErrDeletePaymailAddress.Wrap(tx.Error)
}

return nil
}

// DeletePaymailAddress will delete a paymail address
func (c *Client) DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error {

Expand Down
1 change: 1 addition & 0 deletions engine/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type ModelService interface {
// PaymailService is the paymail actions & services
type PaymailService interface {
DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error
DeletePaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) error
GetPaymailConfig() *PaymailServerOptions
GetPaymailAddress(ctx context.Context, address string, opts ...ModelOps) (*PaymailAddress, error)
GetPaymailAddressByID(ctx context.Context, id string, opts ...ModelOps) (*PaymailAddress, error)
Expand Down

0 comments on commit b06ac5a

Please sign in to comment.