-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepository.go
49 lines (34 loc) · 824 Bytes
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"encoding/json"
"io/ioutil"
)
// RepositoryInterface interface for handling shipments repository
type RepositoryInterface interface {
load() (Shipments, error)
save(s Shipments) error
archive(s Shipments)
}
// ShipmentsRepository struct to read the file
type ShipmentsRepository struct {
Path string
}
func (r ShipmentsRepository) load() (Shipments, error) {
shipments := Shipments{}
jsonString, err := ioutil.ReadFile(r.Path)
err = json.Unmarshal(jsonString, &shipments)
if err != nil {
return shipments, err
}
return shipments, nil
}
func (r ShipmentsRepository) save(s Shipments) error {
b, err := json.MarshalIndent(s, "", " ")
if err == nil {
err = ioutil.WriteFile(r.Path, b, 0644)
}
return err
}
func (r ShipmentsRepository) archive() error {
return nil
}