Skip to content

Commit 7b284a7

Browse files
committed
Actually save demo problems
1 parent 8a02ea6 commit 7b284a7

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

api/problem.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package api
22

3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
)
9+
310
type Problem struct {
411
ID string `json:"id"`
512
TrackID string `json:"track_id"`
@@ -10,6 +17,36 @@ type Problem struct {
1017
Files map[string]string `json:"files"`
1118
}
1219

20+
func (p *Problem) String() {
21+
fmt.Sprintf("%s - %s in %s", p.ID, p.Name, p.Language)
22+
}
23+
24+
func (p *Problem) ExistsIn(dir string) bool {
25+
path := fmt.Sprintf("%s/%s", dir, p.ID)
26+
27+
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
28+
return false
29+
}
30+
return true
31+
}
32+
1333
func (p *Problem) Save(dir string) error {
34+
if p.ExistsIn(dir) {
35+
return nil
36+
}
37+
38+
for name, text := range p.Files {
39+
file := fmt.Sprintf("%s/%s/%s", dir, p.ID, name)
40+
41+
err := os.MkdirAll(filepath.Dir(file), 0755)
42+
if err != nil {
43+
return err
44+
}
45+
46+
err = ioutil.WriteFile(file, []byte(text), 0644)
47+
if err != nil {
48+
return err
49+
}
50+
}
1451
return nil
1552
}

0 commit comments

Comments
 (0)