Skip to content

Commit

Permalink
Currencies endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tolyo committed Dec 5, 2023
1 parent b3d9269 commit 28d6cf8
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 35 deletions.
28 changes: 5 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"open-outcry/pkg/conf"
"open-outcry/pkg/db"
"open-outcry/pkg/models"
"open-outcry/pkg/services"
"open-outcry/pkg/rest"
"os"
"sync"

log "github.com/sirupsen/logrus"
)
Expand All @@ -30,26 +29,9 @@ func main() {
models.LoadFees(fees)
}

// sample code to generate 100 trades
_, tradingAccount1 := services.Acc("test")
_, tradingAccount2 := services.Acc("test2")

var mu sync.Mutex
var wg sync.WaitGroup
wg.Add(100)

for i := 0; i < 100; i++ {

go func() {
mu.Lock()
log.Info(services.ProcessTradeOrder(tradingAccount1, "BTC_EUR", "LIMIT", "SELL", 1, 10, "GTC"))
log.Info(services.ProcessTradeOrder(tradingAccount2, "BTC_EUR", "MARKET", "BUY", 0, 10, "GTC"))
mu.Unlock()

wg.Done()
}()
server := rest.NewServer()
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}

wg.Wait()

}
2 changes: 1 addition & 1 deletion pkg/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ info:
version: 1.0.0
title: OPEN OUTCRY API
servers:
- url: https://your.public.url
- url: http://localhost:4000
security:
- basicAuth: []
paths:
Expand Down
2 changes: 2 additions & 0 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var mapper = map[EnvName]string{
type configuration struct {
DBDsn string
UpdateFees bool
RestPort string
}

var config *configuration
Expand Down Expand Up @@ -67,6 +68,7 @@ func LoadConfig(conf string) (*configuration, error) {
viper.GetString("POSTGRES_PORT"),
),
UpdateFees: viper.GetBool("UPDATE_FEES"),
RestPort: viper.GetString("REST_PORT"),
}

return config, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/conf/dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ POSTGRES_PASSWORD=postgres
POSTGRES_PORT=5432
POSTGRES_HOST=localhost

REST_PORT=4000
UPDATE_FEE=true
1 change: 1 addition & 0 deletions pkg/conf/docker.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
REST_PORT=4000
ENV=PROD
POSTGRES_HOST=db
8 changes: 8 additions & 0 deletions pkg/models/currency.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "open-outcry/pkg/db"

type CurrencyName string

type CurrencyPrecision int
Expand All @@ -8,3 +10,9 @@ type Currency struct {
Name CurrencyName
Precision CurrencyPrecision
}

// GetCurrencies returns a list of available currencies
func GetCurrencies() []Currency {
res := db.QueryList[Currency](`SELECT * FROM currency`)
return res
}
6 changes: 6 additions & 0 deletions pkg/models/currency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

func (assert *ModelsTestSuite) TestGetCurrencies() {
// expect currenceis to be popullated
assert.GreaterOrEqual(3, len(GetCurrencies()))
}
2 changes: 2 additions & 0 deletions pkg/rest/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

api_currencies_service.go
23 changes: 12 additions & 11 deletions pkg/rest/api/api_currencies_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ package api

import (
"context"
"errors"
"net/http"
"open-outcry/pkg/models"
)

// CurrenciesAPIService is a service that implements the logic for the CurrenciesAPIServicer
Expand All @@ -28,14 +28,15 @@ func NewCurrenciesAPIService() CurrenciesAPIServicer {

// GetCurrencies - currencies list
func (s *CurrenciesAPIService) GetCurrencies(ctx context.Context) (ImplResponse, error) {
// TODO - update GetCurrencies with the required logic for this service method.
// Add api_currencies_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

// TODO: Uncomment the next line to return response Response(200, interface{}{}) or use other options such as http.Ok ...
// return Response(200, interface{}{}), nil

// TODO: Uncomment the next line to return response Response(500, {}) or use other options such as http.Ok ...
// return Response(500, nil),nil

return Response(http.StatusNotImplemented, nil), errors.New("GetCurrencies method not implemented")
currencies := models.GetCurrencies()
res := make([]Currency, 0)
for _, v := range currencies {
cur := Currency{
Name: NewInterface(v.Name),
Precision: NewInterface(v.Precision),
}
res = append(res, cur)
}

return Response(http.StatusOK, res), nil
}
6 changes: 6 additions & 0 deletions pkg/rest/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) err
}
return nil
}

// NewInterface creates a new empty interface with a given value.
func NewInterface(value interface{}) *interface{} {
v := value
return &v
}
17 changes: 17 additions & 0 deletions pkg/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package rest

import (
"net/http"
"open-outcry/pkg/conf"
"open-outcry/pkg/rest/api"
)

func NewServer() http.Server {
router := api.NewRouter(
api.NewCurrenciesAPIController(api.NewCurrenciesAPIService()),
)
return http.Server{
Addr: ":" + conf.Get().RestPort,
Handler: router,
}
}

0 comments on commit 28d6cf8

Please sign in to comment.