Skip to content

Commit 79ec879

Browse files
committed
Ensure not submitted is only displayed during fetch
1 parent f8f98b9 commit 79ec879

4 files changed

Lines changed: 23 additions & 27 deletions

File tree

api/api.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *Client) Fetch(args []string) ([]*Problem, error) {
6868
}
6969

7070
if res.StatusCode != http.StatusOK {
71-
return nil, fmt.Errorf(`unable to fetch problems (HTTP: %d) - %s`, res.StatusCode, payload.Error)
71+
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
7272
}
7373

7474
return payload.Problems, nil
@@ -89,27 +89,22 @@ func (c *Client) Restore() ([]*Problem, error) {
8989
}
9090

9191
if res.StatusCode != http.StatusOK {
92-
return nil, fmt.Errorf(`unable to fetch problems (HTTP: %d) - %s`, res.StatusCode, payload.Error)
92+
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
9393
}
9494

9595
return payload.Problems, nil
9696
}
9797

9898
// Submissions gets a list of submitted exercises and their current acceptance state.
99-
func Submissions(url string) (map[string][]SubmissionInfo, error) {
100-
req, err := http.NewRequest("GET", url, nil)
101-
if err != nil {
102-
return nil, err
103-
}
104-
105-
res, err := http.DefaultClient.Do(req)
99+
func (c *Client) Submissions() (map[string][]SubmissionInfo, error) {
100+
url := fmt.Sprintf("%s/api/v1/exercises?key=%s", c.APIHost, c.APIKey)
101+
req, err := c.NewRequest("GET", url, nil)
106102
if err != nil {
107103
return nil, err
108104
}
109-
defer res.Body.Close()
110105

111-
payload := map[string][]SubmissionInfo{}
112-
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
106+
var payload map[string][]SubmissionInfo
107+
if _, err := c.Do(req, &payload); err != nil {
113108
return nil, err
114109
}
115110

@@ -153,7 +148,7 @@ func (c *Client) Demo() ([]*Problem, error) {
153148
}
154149

155150
if res.StatusCode != http.StatusOK {
156-
return nil, fmt.Errorf(`unable to fetch problems (HTTP: %d) - %s`, res.StatusCode, payload.Error)
151+
return nil, fmt.Errorf("unable to fetch problems (HTTP: %d) - %s", res.StatusCode, payload.Error)
157152
}
158153

159154
return payload.Problems, nil
@@ -179,7 +174,7 @@ func (c *Client) Submit(iter *Iteration) (*Submission, error) {
179174
}
180175

181176
if res.StatusCode != http.StatusCreated {
182-
return nil, fmt.Errorf(`unable to submit (HTTP: %d) - %s`, res.StatusCode, ps.Error)
177+
return nil, fmt.Errorf("unable to submit (HTTP: %d) - %s", res.StatusCode, ps.Error)
183178
}
184179

185180
return ps.Submission, nil

cmd/fetch.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ func Fetch(ctx *cli.Context) {
2222
log.Fatal(err)
2323
}
2424

25-
if err := setSubmissionState(c, problems); err != nil {
25+
submissionInfo, err := client.Submissions()
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
30+
if err := setSubmissionState(problems, submissionInfo); err != nil {
2631
log.Fatal(err)
2732
}
2833

@@ -31,17 +36,12 @@ func Fetch(ctx *cli.Context) {
3136
log.Fatal(err)
3237
}
3338

34-
hw.Summarize()
39+
hw.Summarize(true)
3540
}
3641

37-
func setSubmissionState(c *config.Config, problems []*api.Problem) error {
38-
submissions, err := api.Submissions(fmt.Sprintf("%s/api/v1/exercises?key=%s", c.API, c.APIKey))
39-
if err != nil {
40-
return err
41-
}
42-
42+
func setSubmissionState(problems []*api.Problem, submissionInfo map[string][]api.SubmissionInfo) error {
4343
for _, problem := range problems {
44-
langSubmissions := submissions[problem.Language]
44+
langSubmissions := submissionInfo[problem.Language]
4545
for _, submission := range langSubmissions {
4646
if submission.Slug == problem.Slug {
4747
problem.Submitted = true

cmd/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ func Restore(ctx *cli.Context) {
2727
if err := hw.Save(); err != nil {
2828
log.Fatal(err)
2929
}
30-
hw.Summarize()
30+
hw.Summarize(false)
3131
}

user/homework.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ func (hw *Homework) maxTitleWidth() int {
117117
}
118118

119119
// Summarize prints a full report of new and updated items in the set.
120-
func (hw *Homework) Summarize() {
120+
func (hw *Homework) Summarize(displayNotSubmitted bool) {
121121
hw.Report(HWUpdated)
122122
hw.Report(HWNew)
123-
hw.Report(HWNotSubmitted)
124-
123+
if displayNotSubmitted {
124+
hw.Report(HWNotSubmitted)
125+
}
125126
fresh := len(hw.ItemsMatching(HWNew))
126127
updated := len(hw.ItemsMatching(HWUpdated))
127128
unchanged := len(hw.Items) - updated - fresh

0 commit comments

Comments
 (0)