Skip to content

Commit

Permalink
tickers接口完善
Browse files Browse the repository at this point in the history
  • Loading branch information
oldfritter committed Aug 27, 2019
1 parent 2ec6f95 commit fca7bce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
27 changes: 22 additions & 5 deletions api/v1/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"encoding/json"
"net/http"
"time"

"github.com/gomodule/redigo/redis"
"github.com/labstack/echo"
Expand All @@ -11,15 +12,27 @@ import (
)

func V1GetTickers(context echo.Context) error {
mainDB := utils.MainDbBegin()
defer mainDB.DbRollback()
tickerRedis := utils.GetRedisConn("ticker")
defer tickerRedis.Close()
values, _ := redis.Values(tickerRedis.Do("HGETALL", TickersRedisKey))
var tickers []interface{}
tickers := make([]interface{}, 0)
var market Market
for i, value := range values {
if i%2 == 1 {
ticker := Ticker{}
json.Unmarshal(value.([]byte), &ticker)
ticker := Ticker{
MarketId: market.Id,
At: time.Now().Unix(),
Name: market.Name,
}
json.Unmarshal(value.([]byte), &ticker.TickerAspect)
tickers = append(tickers, ticker)
} else {
marketId, _ := redis.String(value, nil)
if mainDB.Where("id = ?", marketId).First(&market).RecordNotFound() {
return utils.BuildError("1021")
}
}
}
response := utils.SuccessResponse
Expand All @@ -37,8 +50,12 @@ func V1GetTickersMarket(context echo.Context) error {
tickerRedis := utils.GetRedisConn("ticker")
defer tickerRedis.Close()
value, _ := tickerRedis.Do("HGET", TickersRedisKey, market.Id)
ticker := Ticker{}
json.Unmarshal(value.([]byte), &ticker)
ticker := Ticker{
MarketId: market.Id,
At: time.Now().Unix(),
Name: market.Name,
}
json.Unmarshal(value.([]byte), &ticker.TickerAspect)
response := utils.SuccessResponse
response.Body = ticker
return context.JSON(http.StatusOK, response)
Expand Down
1 change: 1 addition & 0 deletions api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func V1PostUsersLogin(context echo.Context) error {
token.InitializeLoginToken()
}
mainDB.Create(&token)
mainDB.Where("user_id = ?", user.Id).Where("expire_at < ?", time.Now()).First(&user.Tokens)
mainDB.DbCommit()

response := utils.SuccessResponse
Expand Down
2 changes: 1 addition & 1 deletion models/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TickerAspect struct {

type Ticker struct {
MarketId int `json:"market_id"`
At int64 `json:"at"`
Name string `json:"name"`
Code string `json:"code"`
TickerAspect TickerAspect `json:"ticker"`
}

0 comments on commit fca7bce

Please sign in to comment.