-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
48 lines (39 loc) · 1.06 KB
/
pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package coingecko
import (
"github.com/dezswap/dezswap-api/api/service"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type pairService struct {
chainId string
*gorm.DB
}
func NewPairService(chainId string, db *gorm.DB) service.Getter[Pair] {
return &pairService{chainId, db}
}
// Get implements Getter
func (s *pairService) Get(key string) (*Pair, error) {
pair := &Pair{}
if tx := s.Table("pair").Where("chain_id = ? and contract = ?", s.chainId, key).Select(
"concat(asset0, '_', asset1) ticker_id," +
"asset0 base," +
"asset1 target," +
"contract pool_id",
).Scan(&pair); tx.Error != nil {
return nil, errors.Wrap(tx.Error, "pairService.Get")
}
return pair, nil
}
// GetAll implements Getter
func (s *pairService) GetAll() ([]Pair, error) {
pairs := []Pair{}
if tx := s.Table("pair").Where("chain_id = ?", s.chainId).Select(
"concat(asset0, '_', asset1) ticker_id," +
"asset0 base," +
"asset1 target," +
"contract pool_id",
).Scan(&pairs); tx.Error != nil {
return nil, errors.Wrap(tx.Error, "pairService.GetAll")
}
return pairs, nil
}