Skip to content

Commit

Permalink
feat: new impl of repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon committed Nov 18, 2024
1 parent 92be8fc commit a8a78e4
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 115 deletions.
20 changes: 1 addition & 19 deletions internal/infra/repository/contract_repository_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,7 @@ func (r *ContractRepositorySqlite) CreateContract(input *entity.Contract) (*enti
if err != nil {
return nil, fmt.Errorf("failed to create contract: %w", err)
}

var result map[string]interface{}
err = r.Db.Raw("SELECT id, symbol, address, created_at, updated_at FROM contracts WHERE symbol = ? LIMIT 1", input.Symbol).
Scan(&result).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, entity.ErrContractNotFound
}
return nil, err
}

contract := &entity.Contract{
Id: uint(result["id"].(int64)),
Symbol: result["symbol"].(string),
Address: common.HexToAddress(result["address"].(string)),
CreatedAt: result["created_at"].(int64),
UpdatedAt: result["updated_at"].(int64),
}
return contract, nil
return r.FindContractBySymbol(input.Symbol)
}

func (r *ContractRepositorySqlite) FindAllContracts() ([]*entity.Contract, error) {
Expand Down
208 changes: 187 additions & 21 deletions internal/infra/repository/crowdfunding_repository_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
"github.com/tribeshq/tribes/internal/domain/entity"
"gorm.io/gorm"
)
Expand All @@ -20,47 +21,208 @@ func NewCrowdfundingRepositorySqlite(db *gorm.DB) *CrowdfundingRepositorySqlite
}

func (r *CrowdfundingRepositorySqlite) CreateCrowdfunding(input *entity.Crowdfunding) (*entity.Crowdfunding, error) {
err := r.Db.Create(&input).Error
err := r.Db.Model(&entity.Crowdfunding{}).Create(map[string]interface{}{
"creator": input.Creator.String(),
"debt_issued": input.DebtIssued.Hex(),
"max_interest_rate": input.MaxInterestRate.Hex(),
"state": input.State,
"expires_at": input.ExpiresAt,
"created_at": input.CreatedAt,
"updated_at": input.UpdatedAt,
}).Error
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create crowdfunding: %w", err)
}
return input, nil
return r.FindCrowdfundingById(input.Id)
}

func (r *CrowdfundingRepositorySqlite) FindCrowdfundingsByCreator(creator common.Address) ([]*entity.Crowdfunding, error) {
func (r *CrowdfundingRepositorySqlite) FindAllCrowdfundings() ([]*entity.Crowdfunding, error) {
var results []map[string]interface{}
err := r.Db.Raw("SELECT id, creator, debt_issued, max_interest_rate, state, expires_at, created_at, updated_at FROM crowdfundings").Scan(&results).Error
if err != nil {
return nil, err
}

var crowdfundings []*entity.Crowdfunding
err := r.Db.Preload("Orders").Where("creator = ?", creator).Find(&crowdfundings).Error
for _, result := range results {
crowdfunding := &entity.Crowdfunding{
Id: uint(result["id"].(int64)),
Creator: common.HexToAddress(result["creator"].(string)),
DebtIssued: uint256.MustFromHex(result["debt_issued"].(string)),
MaxInterestRate: uint256.MustFromHex(result["max_interest_rate"].(string)),
State: entity.CrowdfundingState(result["state"].(string)),
ExpiresAt: result["expires_at"].(int64),
CreatedAt: result["created_at"].(int64),
UpdatedAt: result["updated_at"].(int64),
}

var orderResults []map[string]interface{}
err = r.Db.Raw("SELECT id, crowdfunding_id, investor, amount, interest_rate, state, created_at, updated_at FROM orders WHERE crowdfunding_id = ?", crowdfunding.Id).Scan(&orderResults).Error
if err != nil {
return nil, fmt.Errorf("failed to load orders for crowdfunding: %w", err)
}

for _, orderResult := range orderResults {
order := &entity.Order{
Id: uint(orderResult["id"].(int64)),
CrowdfundingId: uint(orderResult["crowdfunding_id"].(int64)),
Investor: common.HexToAddress(orderResult["investor"].(string)),
Amount: uint256.MustFromHex(orderResult["amount"].(string)),
InterestRate: uint256.MustFromHex(orderResult["interest_rate"].(string)),
State: entity.OrderState(orderResult["state"].(string)),
CreatedAt: orderResult["created_at"].(int64),
UpdatedAt: orderResult["updated_at"].(int64),
}
crowdfunding.Orders = append(crowdfunding.Orders, order)
}

crowdfundings = append(crowdfundings, crowdfunding)
}
return crowdfundings, nil
}

func (r *CrowdfundingRepositorySqlite) FindCrowdfundingById(id uint) (*entity.Crowdfunding, error) {
var result map[string]interface{}
err := r.Db.Raw("SELECT id, creator, debt_issued, max_interest_rate, state, expires_at, created_at, updated_at FROM crowdfundings WHERE id = ? LIMIT 1", id).Scan(&result).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, entity.ErrCrowdfundingNotFound
}
return nil, err
}
return crowdfundings, nil
}

func (r *CrowdfundingRepositorySqlite) FindCrowdfundingsByInvestor(investor common.Address) ([]*entity.Crowdfunding, error) {
return nil, nil
crowdfunding := &entity.Crowdfunding{
Id: uint(result["id"].(int64)),
Creator: common.HexToAddress(result["creator"].(string)),
DebtIssued: uint256.MustFromHex(result["debt_issued"].(string)),
MaxInterestRate: uint256.MustFromHex(result["max_interest_rate"].(string)),
State: entity.CrowdfundingState(result["state"].(string)),
ExpiresAt: result["expires_at"].(int64),
CreatedAt: result["created_at"].(int64),
UpdatedAt: result["updated_at"].(int64),
}

var orderResults []map[string]interface{}
err = r.Db.Raw("SELECT id, crowdfunding_id, investor, amount, interest_rate, state, created_at, updated_at FROM orders WHERE crowdfunding_id = ?", crowdfunding.Id).Scan(&orderResults).Error
if err != nil {
return nil, fmt.Errorf("failed to load orders for crowdfunding: %w", err)
}

for _, orderResult := range orderResults {
order := &entity.Order{
Id: uint(orderResult["id"].(int64)),
CrowdfundingId: uint(orderResult["crowdfunding_id"].(int64)),
Investor: common.HexToAddress(orderResult["investor"].(string)),
Amount: uint256.MustFromHex(orderResult["amount"].(string)),
InterestRate: uint256.MustFromHex(orderResult["interest_rate"].(string)),
State: entity.OrderState(orderResult["state"].(string)),
CreatedAt: orderResult["created_at"].(int64),
UpdatedAt: orderResult["updated_at"].(int64),
}
crowdfunding.Orders = append(crowdfunding.Orders, order)
}

return crowdfunding, nil
}

func (r *CrowdfundingRepositorySqlite) FindCrowdfundingById(id uint) (*entity.Crowdfunding, error) {
var crowdfunding entity.Crowdfunding
err := r.Db.Preload("Orders").First(&crowdfunding, id).Error
func (r *CrowdfundingRepositorySqlite) FindCrowdfundingsByCreator(creator common.Address) ([]*entity.Crowdfunding, error) {
var results []map[string]interface{}
err := r.Db.Raw("SELECT id, creator, debt_issued, max_interest_rate, state, expires_at, created_at, updated_at FROM crowdfundings WHERE creator = ?", creator.String()).Scan(&results).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, entity.ErrCrowdfundingNotFound
}
return nil, err
}
return &crowdfunding, nil
}

func (r *CrowdfundingRepositorySqlite) FindAllCrowdfundings() ([]*entity.Crowdfunding, error) {
var crowdfundings []*entity.Crowdfunding
err := r.Db.Preload("Orders").Find(&crowdfundings).Error
for _, result := range results {
crowdfunding := &entity.Crowdfunding{
Id: uint(result["id"].(int64)),
Creator: common.HexToAddress(result["creator"].(string)),
DebtIssued: uint256.MustFromHex(result["debt_issued"].(string)),
MaxInterestRate: uint256.MustFromHex(result["max_interest_rate"].(string)),
State: entity.CrowdfundingState(result["state"].(string)),
ExpiresAt: result["expires_at"].(int64),
CreatedAt: result["created_at"].(int64),
UpdatedAt: result["updated_at"].(int64),
}

var orderResults []map[string]interface{}
err = r.Db.Raw("SELECT id, crowdfunding_id, investor, amount, interest_rate, state, created_at, updated_at FROM orders WHERE crowdfunding_id = ?", crowdfunding.Id).Scan(&orderResults).Error
if err != nil {
return nil, fmt.Errorf("failed to load orders for crowdfunding: %w", err)
}

for _, orderResult := range orderResults {
order := &entity.Order{
Id: uint(orderResult["id"].(int64)),
CrowdfundingId: uint(orderResult["crowdfunding_id"].(int64)),
Investor: common.HexToAddress(orderResult["investor"].(string)),
Amount: uint256.MustFromHex(orderResult["amount"].(string)),
InterestRate: uint256.MustFromHex(orderResult["interest_rate"].(string)),
State: entity.OrderState(orderResult["state"].(string)),
CreatedAt: orderResult["created_at"].(int64),
UpdatedAt: orderResult["updated_at"].(int64),
}
crowdfunding.Orders = append(crowdfunding.Orders, order)
}

crowdfundings = append(crowdfundings, crowdfunding)
}
return crowdfundings, nil
}

func (r *CrowdfundingRepositorySqlite) FindCrowdfundingsByInvestor(investor common.Address) ([]*entity.Crowdfunding, error) {
var results []map[string]interface{}
err := r.Db.Raw(`
SELECT c.id, c.creator, c.debt_issued, c.max_interest_rate, c.state, c.expires_at, c.created_at, c.updated_at
FROM crowdfundings c
JOIN crowdfunding_investors ci ON c.id = ci.crowdfunding_id
WHERE ci.investor = ?
`, investor.String()).Scan(&results).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, entity.ErrCrowdfundingNotFound
}
return nil, err
}

var crowdfundings []*entity.Crowdfunding
for _, result := range results {
crowdfunding := &entity.Crowdfunding{
Id: uint(result["id"].(int64)),
Creator: common.HexToAddress(result["creator"].(string)),
DebtIssued: uint256.MustFromHex(result["debt_issued"].(string)),
MaxInterestRate: uint256.MustFromHex(result["max_interest_rate"].(string)),
State: entity.CrowdfundingState(result["state"].(string)),
ExpiresAt: result["expires_at"].(int64),
CreatedAt: result["created_at"].(int64),
UpdatedAt: result["updated_at"].(int64),
}

var orderResults []map[string]interface{}
err = r.Db.Raw("SELECT id, crowdfunding_id, investor, amount, interest_rate, state, created_at, updated_at FROM orders WHERE crowdfunding_id = ?", crowdfunding.Id).Scan(&orderResults).Error
if err != nil {
return nil, fmt.Errorf("failed to load orders for crowdfunding: %w", err)
}

for _, orderResult := range orderResults {
order := &entity.Order{
Id: uint(orderResult["id"].(int64)),
CrowdfundingId: uint(orderResult["crowdfunding_id"].(int64)),
Investor: common.HexToAddress(orderResult["investor"].(string)),
Amount: uint256.MustFromHex(orderResult["amount"].(string)),
InterestRate: uint256.MustFromHex(orderResult["interest_rate"].(string)),
State: entity.OrderState(orderResult["state"].(string)),
CreatedAt: orderResult["created_at"].(int64),
UpdatedAt: orderResult["updated_at"].(int64),
}
crowdfunding.Orders = append(crowdfunding.Orders, order)
}

crowdfundings = append(crowdfundings, crowdfunding)
}
return crowdfundings, nil
}

Expand All @@ -82,7 +244,8 @@ func (r *CrowdfundingRepositorySqlite) UpdateCrowdfunding(input *entity.Crowdfun
return nil, err
}

crowdfundingJSON["max_interest_rate"] = input.MaxInterestRate
crowdfundingJSON["debt_issued"] = input.DebtIssued.Hex()
crowdfundingJSON["max_interest_rate"] = input.MaxInterestRate.Hex()
crowdfundingJSON["state"] = input.State
crowdfundingJSON["expires_at"] = input.ExpiresAt
crowdfundingJSON["updated_at"] = input.UpdatedAt
Expand All @@ -100,17 +263,20 @@ func (r *CrowdfundingRepositorySqlite) UpdateCrowdfunding(input *entity.Crowdfun
return nil, err
}

res := r.Db.Save(crowdfundingJSON)
res := r.Db.Save(&crowdfunding)
if res.Error != nil {
return nil, fmt.Errorf("failed to update crowdfunding: %w", res.Error)
}
return &crowdfunding, nil
}

func (r *CrowdfundingRepositorySqlite) DeleteCrowdfunding(id uint) error {
err := r.Db.Delete(&entity.Crowdfunding{}, "id = ?", id).Error
if err != nil {
return err
res := r.Db.Delete(&entity.Crowdfunding{}, "id = ?", id)
if res.Error != nil {
return fmt.Errorf("failed to delete crowdfunding: %w", res.Error)
}
if res.RowsAffected == 0 {
return entity.ErrCrowdfundingNotFound
}
return nil
}
Loading

0 comments on commit a8a78e4

Please sign in to comment.