Skip to content

Commit

Permalink
list contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmagnus committed Nov 3, 2023
1 parent cb6383c commit 1e878f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/internal/controller/http/v1/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions backend/internal/usecase/repo/contract_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,20 +56,29 @@ 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,
&vault.Id, &vault.Name,
&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)
}

vault.Wallet = wallet
vault.VaultCategory = vaultCategory
asset.Distributor = assetDistributor
asset.Issuer = assetIssuer
contract.Asset = asset
contract.Vault = vault

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions backend/migrations/000029_asset_contract_id.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE asset DROP COLUMN contract_id;
1 change: 1 addition & 0 deletions backend/migrations/000029_asset_contract_id.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE asset ADD COLUMN contract_id varchar(255);

0 comments on commit 1e878f6

Please sign in to comment.