Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Retrieves a list of assets.
### Query arguments
- **address**: string. It must be a valid polkadot address.
- **id**: string
- **blockchain**: string. It must be either 'polkadot' or 'kusama'
- **order**: string. It orders the results using a field. It must be `id`, `address` or `created_at`.
- **ascending**: bool. It defines if the order is ascending or descendingl It's used along with `order`.
- **limit**: int. The maximum number of items to return. If not specified or greater than the maximum limit, it defaults to 100.
Expand All @@ -122,10 +123,11 @@ Retrieves a list of assets.
"id": "asset_id",
"description": "asset_description",
"address": "owner",
"blockchain": "polkadot|kusama",
"image": "asset_image_url",
"social": {
"twitter": "twitter_handle",
"facebook": "facebook_handle"
"twitter": "twitter_handle",
"facebook": "facebook_handle"
}
}
]
Expand All @@ -147,6 +149,7 @@ Retrieves an asset by its ID.
"ok": true,
"data": {
"id": "asset_id",
"blockchain": "polkadot|kusama",
"description": "asset_description",
"image": "asset_image_url",
"social": {
Expand All @@ -168,6 +171,7 @@ Creates a new asset. It requires authentication.
"data": {
"id": "asset_id",
"description": "asset_description",
"blockchain": "polkadot|kusama",
"image": "asset_image_url",
"social": {
"twitter": "twitter_handle",
Expand All @@ -189,6 +193,7 @@ Creates a new asset. It requires authentication.
"data": {
"id": "asset_id",
"description": "asset_description",
"blockchain": "polkadot|kusama",
"image": "asset_image_url",
"social": {
"twitter": "twitter_handle",
Expand All @@ -205,7 +210,12 @@ It updates an asset. Only its owner can do it. It requires authentication.
#### Request Body
```json
{
"ok": true
"description": "asset_description",
"blockchain": "polkadot|kusama",
"image": "asset_image_url",
"social": {
"twitter": "twitter_handle",
"facebook": "facebook_handle"
}

```
Expand Down
4 changes: 4 additions & 0 deletions packages/api/pkg/adapters/assets/pg_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (repo *AssetsRepository) GetAssets(ctx context.Context, filters *model.GetA
query = query.Where("id = ?", *filters.ID)
}

if filters.Blockchain != nil {
query = query.Where("blockchain = ?", *filters.Blockchain)
}

if filters.Order.Order != nil {
orderClause := fmt.Sprintf("'%s' '%s'", *filters.Order.Order, "ASC")
if filters.Order.Ascending != nil && !*filters.Order.Ascending {
Expand Down
1 change: 1 addition & 0 deletions packages/api/pkg/model/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Asset struct {
ID_ *int `bun:"_id" json:"_id"`
ID string `bun:"id" json:"id"`
Address string `bun:"address" json:"address"`
Blockchain *string `bun:"blockchain" json:"blockchain"`
Description *string `bun:"description" json:"description,omitempty"`
Image *string `bun:"image" json:"image,omitempty"`
Social *map[string]string `bun:"social" json:"social,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions packages/api/pkg/model/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package model

const POLKADOT = "polkadot"
const KUSAMA = "kusama"
27 changes: 25 additions & 2 deletions packages/api/pkg/model/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func validateDescription(description *string) error {
}
return nil
}
func validateBlockchain(blockchain *string) error {
if blockchain != nil {
if *blockchain != POLKADOT && *blockchain != KUSAMA {
return errors.New("blockchain must be either 'polkadot' or 'kusama'")
}
}
return nil
}

func validateSocial(social *map[string]string) error {
if social != nil {
Expand Down Expand Up @@ -87,12 +95,14 @@ type AuthHeaders struct {

type NewAsset struct {
ID string `json:"id"`
Blockchain string `json:"blockchain"`
Description *string `json:"description"`
Image *string `json:"image" validate:"nonzero"`
Social *map[string]string `json:"social"`
}

type UpdateAsset struct {
Blockchain *string `json:"blockchain"`
Description *string `json:"description"`
Image *string `json:"image" validate:"nonzero"`
Social *map[string]string `json:"social"`
Expand All @@ -118,6 +128,10 @@ func (c *CreateAssetInput) Validate() error {
if err := validateSocial(c.Social); err != nil {
return err
}

if err := validateBlockchain(&c.Blockchain); err != nil {
return err
}
return nil
}

Expand All @@ -140,6 +154,9 @@ func (c *UpdateAssetInput) Validate() error {
if err := validateSocial(c.Social); err != nil {
return err
}
if err := validateBlockchain(c.Blockchain); err != nil {
return err
}
return nil
}

Expand All @@ -165,8 +182,9 @@ func (c *GetAssetByIDInput) Validate() error {
}

type GetAssetsInput struct {
Address *string `in:"query=address"`
ID *string `in:"query=id"`
Address *string `in:"query=address"`
ID *string `in:"query=id"`
Blockchain *string `in:"query=blockchain"`
Order
Pagination
}
Expand All @@ -186,6 +204,11 @@ func (c *GetAssetsInput) Validate() error {
return errors.New("address must only contain alphanumeric characters")
}
}
if c.Blockchain != nil {
if err := validateBlockchain(c.Blockchain); err != nil {
return err
}
}
if c.Order.Order != nil {
validOrders := map[string]bool{"id": true, "address": true, "created_at": true}
if !validOrders[*c.Order.Order] {
Expand Down
26 changes: 24 additions & 2 deletions packages/api/pkg/model/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func TestCreateAssetInputValidation(t *testing.T) {
name: "valid input",
input: model.CreateAssetInput{
NewAsset: model.NewAsset{
ID: "1a2b3c",
Image: strPtr("https://example.com/image.jpg"),
ID: "1a2b3c",
Image: strPtr("https://example.com/image.jpg"),
Blockchain: "polkadot",
},
},
wantErr: false,
Expand All @@ -43,6 +44,17 @@ func TestCreateAssetInputValidation(t *testing.T) {
},
wantErr: true,
},
{
name: "invalid blockchain",
input: model.CreateAssetInput{
NewAsset: model.NewAsset{
ID: "1a2b3c",
Image: strPtr("https://example.com/image.jpg"),
Blockchain: "invalid",
},
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -90,6 +102,16 @@ func TestUpdateAssetInputValidation(t *testing.T) {
},
wantErr: true,
},
{
name: "invalid blockchain",
input: model.UpdateAssetInput{
ID: "1a2b3c",
UpdateAsset: model.UpdateAsset{
Blockchain: strPtr("invalid"),
},
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions packages/api/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (srv *Service) CreateAsset(w http.ResponseWriter, r *http.Request) {
Image: createAsset.Image,
Social: createAsset.Social,
Address: createAsset.Address,
Blockchain: &createAsset.Blockchain,
}

asset, err := srv.assetsApp.CreateAsset(r.Context(), asset)
Expand Down Expand Up @@ -65,6 +66,7 @@ func (srv *Service) UpdateAsset(w http.ResponseWriter, r *http.Request) {
Image: updateAsset.Image,
Social: updateAsset.Social,
Address: updateAsset.Address,
Blockchain: updateAsset.Blockchain,
}

err := srv.assetsApp.UpdateAsset(r.Context(), asset)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE assets DROP COLUMN blockchain;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE assets ADD COLUMN blockchain TEXT NOT NULL;
Loading