Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Oct 31, 2024
1 parent 3318174 commit ba52fe6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
58 changes: 20 additions & 38 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,31 @@ func (a *api) getSymbolList() (*SymbolList, error) {

type pdate time.Time

func (pd *pdate) String() string {
return time.Time(*pd).Format("2006-01-02")
}

func (pd *pdate) Unix() int64 {
return time.Time(*pd).Unix()
}

func (pd *pdate) Set(dt time.Time) {
d, _ := time.Parse(time.DateOnly, dt.Format("2006-01-02"))
d = d.Add(time.Hour * 20)

*pd = pdate(d)
}

func (pd pdate) String() string {
return time.Time(pd).Format("2006-01-02")
}

func (pd pdate) Unix() int64 {
return time.Time(pd).Unix()
}

type period struct {
begin, end pdate
}

func (p *period) SetBegin(dt time.Time) {
p.begin.Set(dt)
}

func (p *period) SetEnd(dt time.Time) {
p.end.Set(dt)
func (p *period) Begin() *pdate {
return &p.begin
}

func (p *period) Begin() pdate {
return p.begin
}

func (p *period) End() pdate {
return p.end
func (p *period) End() *pdate {
return &p.end
}

func (p *period) IsSingleDay() bool {
Expand All @@ -113,14 +105,14 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
switch len(dates) {
case 0:
dtToday := time.Now()
p.SetBegin(dtToday)
p.SetEnd(dtToday)
p.Begin().Set(dtToday)
p.End().Set(dtToday)
case 1:
p.SetBegin(dates[0])
p.SetBegin(dates[0])
p.Begin().Set(dates[0])
p.End().Set(dates[0])
default:
p.SetBegin(dates[0])
p.SetEnd(dates[1])
p.Begin().Set(dates[0])
p.End().Set(dates[1])
}

quoteList := &QuoteList{
Expand Down Expand Up @@ -154,12 +146,7 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
if !p.IsSingleDay() {
h := History{}

if len(data.Chart.Result[0].Indicators.Adjclose) == 0 {
q.SetError(errHistoryDataNotFound)
return
}

if len(data.Chart.Result[0].Indicators.Adjclose[0].Adjclose) == 0 {
if !data.adjCloseCheck() {
q.SetError(errHistoryDataNotFound)
return
}
Expand All @@ -172,12 +159,7 @@ func (a *api) getQuote(symbols []string, dates ...time.Time) (*QuoteList, error)
return
}

if len(data.Chart.Result[0].Indicators.Adjclose) == 0 {
q.SetError(errHistoryDataNotFound)
return
}

if len(data.Chart.Result[0].Indicators.Adjclose[0].Adjclose) == 0 {
if !data.adjCloseCheck() {
q.SetError(errHistoryDataNotFound)
return
}
Expand Down
12 changes: 12 additions & 0 deletions dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ type quoteDTO struct {
} `json:"chart"`
}

func (q quoteDTO) adjCloseCheck() bool {
if len(q.Chart.Result[0].Indicators.Adjclose) == 0 {
return false
}

if len(q.Chart.Result[0].Indicators.Adjclose[0].Adjclose) == 0 {
return false
}

return true
}

type symbolListResponse struct {
TotalCount int `json:"totalCount"`
Data []struct {
Expand Down
2 changes: 1 addition & 1 deletion type.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (ql QuoteList) ToJson() string {

func parseSymbolData(i int, d []string) Symbol {
s := Symbol{
Id: i,
Id: i + 1,
}

if len(d) != 3 {
Expand Down

0 comments on commit ba52fe6

Please sign in to comment.