Skip to content

Commit

Permalink
feat: get toml data
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmagnus committed Oct 20, 2023
1 parent be24f1f commit 1f224b9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 68 deletions.
22 changes: 21 additions & 1 deletion backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func newAssetsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase, as useca
h.POST("/:id/image", r.uploadAssetImage)
h.POST("/generate-toml", r.generateTOML)
h.PUT("/update-toml", r.updateTOML)
h.GET("/toml-data", r.getTomlData)
}
}

Expand Down Expand Up @@ -879,7 +880,7 @@ func (r *assetsRoutes) generateTOML(c *gin.Context) {
errorResponse(c, http.StatusBadRequest, "invalid request body: %s", err)
return
}

fmt.Printf(request.Currencies[len(request.Currencies)-1].Description)
toml, err := r.as.CreateToml(request)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error creating TOML", err)
Expand Down Expand Up @@ -933,3 +934,22 @@ func (r *assetsRoutes) updateTOML(c *gin.Context) {

c.Data(http.StatusOK, "application/toml", []byte(toml))
}

// @Summary Get TOML data
// @Description Get TOML data
// @Tags Assets
// @Accept json
// @Produce json
// @Param asset_issuer path string true "Asset issuer"
// @Success 200 {object} entity.TomlData
// @Failure 500 {object} response
// @Router /assets/toml [get]
func (r *assetsRoutes) getTomlData(c *gin.Context) {
tomlContent, err := r.as.GetTomlData()
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error retrieving TOML", err)
return
}

c.JSON(http.StatusOK, tomlContent)
}
5 changes: 1 addition & 4 deletions backend/internal/controller/http/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ func NewRouter(
newVaultRoutes(groupV1, messengerController, authUseCase, vaultUc, vaultCategoryUc, walletUseCase, assetUseCase)
newContractRoutes(groupV1, messengerController, authUseCase, contractUc, vaultUc, assetUseCase)
newLogTransactionsRoutes(groupV1, walletUseCase, assetUseCase, messengerController, logUc, authUseCase)
newAssetTomlRoutes(groupV1, walletUseCase, assetUseCase, messengerController, authUseCase, logUc)
}

// Toml Route
handler.Use(CORSMiddlewareAllowAllOrigins()) // Allow all origins for the toml
newAssetTomlRoutes(handler.Group("/"), walletUseCase, assetUseCase, messengerController, authUseCase, logUc)
}
114 changes: 57 additions & 57 deletions backend/internal/entity/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,75 +20,75 @@ const (
)

type TomlData struct {
Version string `toml:"VERSION,omitempty"`
NetworkPassphrase string `toml:"NETWORK_PASSPHRASE,omitempty"`
FederationServer string `toml:"FEDERATION_SERVER,omitempty"`
TransferServer string `toml:"TRANSFER_SERVER,omitempty"`
SigningKey string `toml:"SIGNING_KEY,omitempty"`
HorizonURL string `toml:"HORIZON_URL,omitempty"`
Accounts []string `toml:"ACCOUNTS,omitempty"`
Documentation Documentation `toml:"DOCUMENTATION,omitempty"`
Principals []Principal `toml:"PRINCIPALS,omitempty"`
Currencies []Currency `toml:"CURRENCIES,omitempty"`
Validators []Validator `toml:"VALIDATORS,omitempty"`
Version string `json:"VERSION,omitempty"`
NetworkPassphrase string `json:"NETWORK_PASSPHRASE,omitempty"`
FederationServer string `json:"FEDERATION_SERVER,omitempty"`
TransferServer string `json:"TRANSFER_SERVER,omitempty"`
SigningKey string `json:"SIGNING_KEY,omitempty"`
HorizonURL string `json:"HORIZON_URL,omitempty"`
Accounts []string `json:"ACCOUNTS,omitempty"`
Documentation Documentation `json:"DOCUMENTATION,omitempty"`
Principals []Principal `json:"PRINCIPALS,omitempty"`
Currencies []Currency `json:"CURRENCIES,omitempty"`
Validators []Validator `json:"VALIDATORS,omitempty"`
}

type Documentation struct {
OrgName string `toml:"ORG_NAME,omitempty"`
OrgDBA string `toml:"ORG_DBA,omitempty"`
OrgURL string `toml:"ORG_URL,omitempty"`
OrgLogo string `toml:"ORG_LOGO,omitempty"`
OrgDescription string `toml:"ORG_DESCRIPTION,omitempty"`
OrgPhysicalAddress string `toml:"ORG_PHYSICAL_ADDRESS,omitempty"`
OrgPhysicalAddressAttestation string `toml:"ORG_PHYSICAL_ADDRESS_ATTESTATION,omitempty"`
OrgPhoneNumber string `toml:"ORG_PHONE_NUMBER,omitempty"`
OrgPhoneNumberAttestation string `toml:"ORG_PHONE_NUMBER_ATTESTATION,omitempty"`
OrgKeybase string `toml:"ORG_KEYBASE,omitempty"`
OrgTwitter string `toml:"ORG_TWITTER,omitempty"`
OrgGithub string `toml:"ORG_GITHUB,omitempty"`
OrgOfficialEmail string `toml:"ORG_OFFICIAL_EMAIL,omitempty"`
OrgName string `json:"ORG_NAME,omitempty"`
OrgDBA string `json:"ORG_DBA,omitempty"`
OrgURL string `json:"ORG_URL,omitempty"`
OrgLogo string `json:"ORG_LOGO,omitempty"`
OrgDescription string `json:"ORG_DESCRIPTION,omitempty"`
OrgPhysicalAddress string `json:"ORG_PHYSICAL_ADDRESS,omitempty"`
OrgPhysicalAddressAttestation string `json:"ORG_PHYSICAL_ADDRESS_ATTESTATION,omitempty"`
OrgPhoneNumber string `json:"ORG_PHONE_NUMBER,omitempty"`
OrgPhoneNumberAttestation string `json:"ORG_PHONE_NUMBER_ATTESTATION,omitempty"`
OrgKeybase string `json:"ORG_KEYBASE,omitempty"`
OrgTwitter string `json:"ORG_TWITTER,omitempty"`
OrgGithub string `json:"ORG_GITHUB,omitempty"`
OrgOfficialEmail string `json:"ORG_OFFICIAL_EMAIL,omitempty"`
}

type Principal struct {
Name string `toml:"name,omitempty"`
Email string `toml:"email,omitempty"`
Keybase string `toml:"keybase,omitempty"`
Twitter string `toml:"twitter,omitempty"`
Github string `toml:"github,omitempty"`
IDPhotoHash string `toml:"id_photo_hash,omitempty"`
VerificationPhotoHash string `toml:"verification_photo_hash,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Keybase string `json:"keybase,omitempty"`
Twitter string `json:"twitter,omitempty"`
Github string `json:"github,omitempty"`
IDPhotoHash string `json:"id_photo_hash,omitempty"`
VerificationPhotoHash string `json:"verification_photo_hash,omitempty"`
}

type Currency struct {
Code string `toml:"code,omitempty"`
Issuer string `toml:"issuer,omitempty"`
DisplayDecimals int `toml:"display_decimals,omitempty"`
Name string `toml:"name,omitempty"`
Description string `toml:"desc,omitempty"`
Conditions string `toml:"conditions,omitempty"`
Image string `toml:"image,omitempty"`
FixedNumber int `toml:"fixed_number,omitempty"`
MaxNumber int `toml:"max_number,omitempty"`
IsUnlimited bool `toml:"is_unlimited,omitempty"`
IsAssetAnchored bool `toml:"is_asset_anchored,omitempty"`
AnchorAssetType string `toml:"anchor_asset_type,omitempty"`
AnchorAsset string `toml:"anchor_asset,omitempty"`
AttestationOfReserve string `toml:"attestation_of_reserve,omitempty"`
RedemptionInstructions string `toml:"redemption_instructions,omitempty"`
CollateralAddresses []string `toml:"collateral_addresses,omitempty"`
CollateralAddressMessages []string `toml:"collateral_address_messages,omitempty"`
CollateralAddressSignatures []string `toml:"collateral_address_signatures,omitempty"`
Regulated bool `toml:"regulated,omitempty"`
ApprovalServer string `toml:"approval_server,omitempty"`
ApprovalCriteria string `toml:"approval_criteria,omitempty"`
Code string `json:"code,omitempty"`
Issuer string `json:"issuer,omitempty"`
DisplayDecimals int `json:"display_decimals,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"desc,omitempty"`
Conditions string `json:"conditions,omitempty"`
Image string `json:"image,omitempty"`
FixedNumber int `json:"fixed_number,omitempty"`
MaxNumber int `json:"max_number,omitempty"`
IsUnlimited bool `json:"is_unlimited,omitempty"`
IsAssetAnchored bool `json:"is_asset_anchored,omitempty"`
AnchorAssetType string `json:"anchor_asset_type,omitempty"`
AnchorAsset string `json:"anchor_asset,omitempty"`
AttestationOfReserve string `json:"attestation_of_reserve,omitempty"`
RedemptionInstructions string `json:"redemption_instructions,omitempty"`
CollateralAddresses []string `json:"collateral_addresses,omitempty"`
CollateralAddressMessages []string `json:"collateral_address_messages,omitempty"`
CollateralAddressSignatures []string `json:"collateral_address_signatures,omitempty"`
Regulated bool `json:"regulated,omitempty"`
ApprovalServer string `json:"approval_server,omitempty"`
ApprovalCriteria string `json:"approval_criteria,omitempty"`
}

type Validator struct {
Alias string `toml:"ALIAS"`
DisplayName string `toml:"DISPLAY_NAME"`
Host string `toml:"HOST"`
PublicKey string `toml:"PUBLIC_KEY"`
History string `toml:"HISTORY"`
Alias string `json:"ALIAS"`
DisplayName string `json:"DISPLAY_NAME"`
Host string `json:"HOST"`
PublicKey string `json:"PUBLIC_KEY"`
History string `json:"HISTORY"`
}

type Toml struct {
Expand Down
14 changes: 14 additions & 0 deletions backend/internal/usecase/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ func (uc *AssetUseCase) UpdateToml(updatedToml entity.TomlData) (string, error)

return tomlCreated, err
}

func (uc *AssetUseCase) GetTomlData() (entity.TomlData, error) {
tomlDb, err := uc.tRepo.GetToml()
if err != nil {
return entity.TomlData{}, fmt.Errorf("AssetUseCase - GetTomlData - uc.repo.GetToml: %w", err)
}

tomParsed, err := uc.tInt.RetrieveToml(tomlDb) // Parse old toml data
if err != nil {
return entity.TomlData{}, fmt.Errorf("AssetUseCase - GetTomlData - uc.tInt.RetrieveToml: %w", err)
}

return tomParsed, err
}
6 changes: 2 additions & 4 deletions backend/internal/usecase/repo/toml_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func (r TomlRepoInterface) CreateToml(content string) (string, error) {
func (r TomlRepoInterface) GetToml() (string, error) {
var res string
stmt := `SELECT content FROM toml ORDER BY id DESC LIMIT 1;`
err := r.Db.QueryRow(stmt).Scan(&res)
if err != nil {
return "", fmt.Errorf("AssetRepo - GetToml - db.QueryRow: %w", err)
}
r.Db.QueryRow(stmt).Scan(&res)

Check failure on line 32 in backend/internal/usecase/repo/toml_postgres.go

View workflow job for this annotation

GitHub Actions / Code Quality (1.20)

Error return value of `(*database/sql.Row).Scan` is not checked (errcheck)

Check failure on line 32 in backend/internal/usecase/repo/toml_postgres.go

View workflow job for this annotation

GitHub Actions / Code Quality (1.20)

Error return value of `(*database/sql.Row).Scan` is not checked (errcheck)

return res, nil
}
5 changes: 3 additions & 2 deletions backend/pkg/toml/toml_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func updateFieldsIfEmpty(fields []FieldConfig) {
func appendIfNotExists[T any](existing []T, newItems []T, equals func(T, T) bool) []T {
for _, newItem := range newItems {
found := false
for _, item := range existing {
for index, item := range existing {
if equals(item, newItem) {
found = true
existing[index] = newItem
break
}
}
Expand All @@ -41,7 +42,7 @@ func appendIfNotExistsPrincipal(existing []entity.Principal, newItems []entity.P

func appendIfNotExistsCurrency(existing []entity.Currency, newItems []entity.Currency) []entity.Currency {
return appendIfNotExists(existing, newItems, func(item1, item2 entity.Currency) bool {
return item1.Code == item2.Code
return item1.Code == item2.Code && item1.Issuer == item2.Issuer
})
}

Expand Down

0 comments on commit 1f224b9

Please sign in to comment.