From 1e878f6bb77b1c2e4b3a7ce9b7fa6741031528dd Mon Sep 17 00:00:00 2001 From: Lucas Magnus Date: Fri, 3 Nov 2023 17:53:57 -0300 Subject: [PATCH] list contracts --- .../internal/controller/http/v1/contracts.go | 4 +-- .../usecase/repo/contract_postgres.go | 25 ++++++++++++++++--- .../000029_asset_contract_id.down.sql | 1 + .../000029_asset_contract_id.up.sql | 1 + 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 backend/migrations/000029_asset_contract_id.down.sql create mode 100644 backend/migrations/000029_asset_contract_id.up.sql diff --git a/backend/internal/controller/http/v1/contracts.go b/backend/internal/controller/http/v1/contracts.go index 1ef5a5fa..a3a2fbc5 100644 --- a/backend/internal/controller/http/v1/contracts.go +++ b/backend/internal/controller/http/v1/contracts.go @@ -26,7 +26,7 @@ func newContractRoutes(handler *gin.RouterGroup, m HTTPControllerMessenger, a us { h.GET("/:id", r.getContractById) h.POST("", r.createContract) - h.GET("list", r.getAllContracts) + h.GET("/list", r.getAllContracts) } } @@ -108,7 +108,7 @@ func (r *contractRoutes) createContract(c *gin.Context) { // @Failure 500 {object} response // @Router / [get] func (r *contractRoutes) getAllContracts(c *gin.Context) { - contract, err := r.v.GetAll() + contract, err := r.c.GetAll() if err != nil { errorResponse(c, http.StatusInternalServerError, "error getting contract", err) return diff --git a/backend/internal/usecase/repo/contract_postgres.go b/backend/internal/usecase/repo/contract_postgres.go index 119bf80f..7ac0cfbd 100644 --- a/backend/internal/usecase/repo/contract_postgres.go +++ b/backend/internal/usecase/repo/contract_postgres.go @@ -24,12 +24,22 @@ func (r ContractRepo) GetContracts() ([]entity.Contract, error) { v.id AS vault_id, v.name AS vault_name, vc.id AS vault_category_id, vc.name as vault_category_name, w.id AS wallet_id, w.type AS wallet_type, w.funded AS wallet_funded, - wk.id AS wallet_key_id, wk.public_key AS wallet_key_public_key, wk.weight AS wallet_key_weight + wk.id AS wallet_key_id, wk.public_key AS wallet_key_public_key, wk.weight AS wallet_key_weight, + a.id AS asset_id, a.name AS asset_name, a.asset_type, a.code as asset_code, a.image, + d.id AS distributor_id, d.type AS distributor_type, d.funded AS distributor_funded, + dk.id AS distributor_key_id, dk.public_key AS distributor_key_public_key, dk.weight AS distributor_key_weight, + i.id AS issuer_id, i.type AS issuer_type, i.funded AS issuer_funded, + ik.id AS issuer_key_id, ik.public_key AS issuer_key_public_key, ik.weight AS issuer_key_weight FROM contracts c JOIN vault v ON c.vault_id = v.id JOIN vaultcategory vc ON v.vault_category_id = vc.id JOIN wallet w ON v.wallet_id = w.id - JOIN key wk ON w.id = wk.wallet_id; + JOIN key wk ON w.id = wk.wallet_id + JOIN asset a ON a.id = c.asset_id + JOIN wallet d ON a.distributor_id = d.id + JOIN key dk ON d.id = dk.wallet_id + JOIN wallet i ON a.issuer_id = i.id + JOIN key ik ON i.id = ik.wallet_id ` rows, err := r.Db.Query(query) @@ -46,6 +56,8 @@ func (r ContractRepo) GetContracts() ([]entity.Contract, error) { var vault entity.Vault var asset entity.Asset var wallet entity.Wallet + var assetDistributor entity.Wallet + var assetIssuer entity.Wallet err := rows.Scan( &contract.Id, &contract.Name, &contract.Address, &contract.YieldRate, &contract.Term, &contract.MinDeposit, &contract.PenaltyRate, @@ -53,6 +65,11 @@ func (r ContractRepo) GetContracts() ([]entity.Contract, error) { &vaultCategory.Id, &vaultCategory.Name, &wallet.Id, &wallet.Type, &wallet.Funded, &wallet.Key.Id, &wallet.Key.PublicKey, &wallet.Key.Weight, + &asset.Id, &asset.Name, &asset.AssetType, &asset.Code, &asset.Image, + &assetDistributor.Id, &assetDistributor.Type, &assetDistributor.Funded, + &assetDistributor.Key.Id, &assetDistributor.Key.PublicKey, &assetDistributor.Key.Weight, + &assetIssuer.Id, &assetIssuer.Type, &assetIssuer.Funded, + &assetIssuer.Key.Id, &assetIssuer.Key.PublicKey, &assetIssuer.Key.Weight, ) if err != nil { return nil, fmt.Errorf("ContractRepo - GetContractCategories - row.Scan: %w", err) @@ -60,6 +77,8 @@ func (r ContractRepo) GetContracts() ([]entity.Contract, error) { vault.Wallet = wallet vault.VaultCategory = vaultCategory + asset.Distributor = assetDistributor + asset.Issuer = assetIssuer contract.Asset = asset contract.Vault = vault @@ -118,7 +137,7 @@ func (r ContractRepo) GetContractById(id string) (entity.Contract, error) { func (r ContractRepo) CreateContract(data entity.Contract) (entity.Contract, error) { res := data - stmt := `INSERT INTO Contract (name, address, yield_rate, term, min_deposit, penalty_rate, vault_id, asset_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id;` + stmt := `INSERT INTO Contracts (name, address, yield_rate, term, min_deposit, penalty_rate, vault_id, asset_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id;` err := r.Db.QueryRow(stmt, data.Name, data.Address, data.YieldRate, data.Term, data.MinDeposit, data.PenaltyRate, data.Vault.Id, data.Asset.Id).Scan(&res.Id) if err != nil { return entity.Contract{}, fmt.Errorf("ContractRepo - Contract - db.QueryRow: %w", err) diff --git a/backend/migrations/000029_asset_contract_id.down.sql b/backend/migrations/000029_asset_contract_id.down.sql new file mode 100644 index 00000000..e72b665f --- /dev/null +++ b/backend/migrations/000029_asset_contract_id.down.sql @@ -0,0 +1 @@ +ALTER TABLE asset DROP COLUMN contract_id; \ No newline at end of file diff --git a/backend/migrations/000029_asset_contract_id.up.sql b/backend/migrations/000029_asset_contract_id.up.sql new file mode 100644 index 00000000..56c3d750 --- /dev/null +++ b/backend/migrations/000029_asset_contract_id.up.sql @@ -0,0 +1 @@ +ALTER TABLE asset ADD COLUMN contract_id varchar(255); \ No newline at end of file