|
6 | 6 | "os" |
7 | 7 | "path/filepath" |
8 | 8 |
|
9 | | - "github.com/goccy/go-yaml" |
10 | | - |
11 | 9 | "github.com/DanWlker/remind/internal/config" |
12 | 10 | "github.com/DanWlker/remind/internal/data" |
13 | 11 | i_error "github.com/DanWlker/remind/internal/error" |
@@ -87,29 +85,38 @@ func SetFileContents(items []RecordEntity) (err error) { |
87 | 85 | } |
88 | 86 | }() |
89 | 87 |
|
90 | | - enc := yaml.NewEncoder(f) |
91 | | - if err := enc.Encode(items); err != nil { |
92 | | - return fmt.Errorf("enc.Encode: %w", err) |
| 88 | + err = shared.FWriteStructToYaml[RecordEntity](f, items) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("shared.FWriteStructToYaml: %w", err) |
93 | 91 | } |
94 | 92 |
|
95 | 93 | return nil |
96 | 94 | } |
97 | 95 |
|
| 96 | +func FGetRecordIdentityWithIdentifier(items []RecordEntity, identifier string) (RecordEntity, error) { |
| 97 | + for _, record := range items { |
| 98 | + if record.Path == identifier { |
| 99 | + return record, nil |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return RecordEntity{}, i_error.RecordDoesNotExistError{ |
| 104 | + ID: identifier, |
| 105 | + } |
| 106 | +} |
| 107 | + |
98 | 108 | func GetRecordEntityWithIdentifier(homeRemovedPath string) (RecordEntity, error) { |
99 | 109 | allRecords, err := GetFileContents() |
100 | 110 | if err != nil { |
101 | 111 | return RecordEntity{}, fmt.Errorf("GetFileContents: %w", err) |
102 | 112 | } |
103 | 113 |
|
104 | | - for _, record := range allRecords { |
105 | | - if record.Path == homeRemovedPath { |
106 | | - return record, nil |
107 | | - } |
| 114 | + res, err := FGetRecordIdentityWithIdentifier(allRecords, homeRemovedPath) |
| 115 | + if err != nil { |
| 116 | + return RecordEntity{}, fmt.Errorf("FGetRecordIdentityWithIdentifier: %w", err) |
108 | 117 | } |
109 | 118 |
|
110 | | - return RecordEntity{}, i_error.RecordDoesNotExistError{ |
111 | | - ID: homeRemovedPath, |
112 | | - } |
| 119 | + return res, nil |
113 | 120 | } |
114 | 121 |
|
115 | 122 | func CreateNewRecord(pathIdentifier string) (RecordEntity, error) { |
|
0 commit comments