1
+ // Package storage implements persistent storages for different types of business models.
2
+ // It also uses different structures to store data in filesystem.
3
+ // It's not required but implemented to demonstrate layer separation.
1
4
package storage
2
5
3
- import (
4
- "encoding/json"
5
- "errors"
6
- "fmt"
7
- "io/ioutil"
8
- "time"
6
+ import "time"
9
7
10
- "github.com/idexter/dip-visa-app/pkg/visa "
11
- )
8
+ const defaultApplicationsDB = "data/applications.json "
9
+ const defaultVisasDB = "data/visas.json"
12
10
13
11
// StoredApplication describes Visa Application stored in Database.
14
12
type StoredApplication struct {
@@ -19,90 +17,10 @@ type StoredApplication struct {
19
17
Money float64 `json:"money"`
20
18
}
21
19
22
- const defaultApplicationsDB = "data/applications.json"
23
- const defaultVisasDB = "data/visas.json"
24
-
25
- // FileApplicationsStorage implements file Visa Applications storage.
26
- type FileApplicationsStorage struct {
27
- Database string
28
- }
29
-
30
- // NewFileApplicationsStorage creates new FileApplicationsStorage.
31
- func NewFileApplicationsStorage () * FileApplicationsStorage {
32
- return & FileApplicationsStorage {Database : defaultApplicationsDB }
33
- }
34
-
35
- // GetVisaApplication gets visa application by application id.
36
- func (as * FileApplicationsStorage ) GetVisaApplication (id int ) (* visa.Application , error ) {
37
-
38
- var apps []StoredApplication
39
- b , err := ioutil .ReadFile (as .Database )
40
- if err != nil {
41
- return nil , fmt .Errorf ("couldn't read applications database %w" , err )
42
- }
43
-
44
- if err := json .Unmarshal (b , & apps ); err != nil {
45
- return nil , fmt .Errorf ("couldn't unmarshal applications database %w" , err )
46
- }
47
-
48
- for _ , v := range apps {
49
- if v .ID == id {
50
- return & visa.Application {
51
- ID : v .ID ,
52
- Name : v .Name ,
53
- Arrival : v .Arrival ,
54
- Departure : v .Departure ,
55
- Money : v .Money ,
56
- }, nil
57
- }
58
- }
59
-
60
- return nil , errors .New ("application was not found" )
61
- }
62
-
63
20
// StoredVisa describes Visa stored in database.
64
21
type StoredVisa struct {
65
22
From time.Time `json:"from"`
66
23
To time.Time `json:"to"`
67
24
Arrival time.Time `json:"arrival"`
68
25
Departure time.Time `json:"departure"`
69
26
}
70
-
71
- // FileVisasStorage implements Visas database based on files.
72
- type FileVisasStorage struct {
73
- Database string
74
- }
75
-
76
- // NewFileVisasStorage creates new instance for FileVisasStorage.
77
- func NewFileVisasStorage () * FileVisasStorage {
78
- return & FileVisasStorage {Database : defaultVisasDB }
79
- }
80
-
81
- // GetPreviousVisas gets previous Visas for provided applicant.
82
- func (vs * FileVisasStorage ) GetPreviousVisas (applicantName string ) ([]visa.Visa , error ) {
83
-
84
- var visas map [string ][]StoredVisa
85
- b , err := ioutil .ReadFile (vs .Database )
86
- if err != nil {
87
- return nil , fmt .Errorf ("couldn't read visas database %w" , err )
88
- }
89
-
90
- if err := json .Unmarshal (b , & visas ); err != nil {
91
- return nil , fmt .Errorf ("couldn't unmarshal visas database %w" , err )
92
- }
93
-
94
- var ret = make ([]visa.Visa , len (visas ))
95
-
96
- if v , ok := visas [applicantName ]; ok {
97
- for _ , current := range v {
98
- ret = append (ret , visa.Visa {
99
- From : current .From ,
100
- To : current .To ,
101
- Arrival : current .Departure ,
102
- Departure : current .Arrival ,
103
- })
104
- }
105
- }
106
-
107
- return ret , nil
108
- }
0 commit comments