-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data manager for storing app data and add input for adding user id
- Loading branch information
1 parent
2ed8397
commit 7a4d6cc
Showing
10 changed files
with
220 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// File for storing, reading, and deleting data for the app | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
) | ||
|
||
var AppDataManager = NewDataManger() | ||
|
||
type AppData struct { | ||
UserId string `json:"userId"` | ||
} | ||
|
||
type DataManager struct { | ||
// Path to file storing app data | ||
path string | ||
} | ||
|
||
func NewDataManger() DataManager { | ||
return DataManager{ | ||
path: "data.json", | ||
} | ||
} | ||
|
||
func (dm DataManager) FileCheck() { | ||
if _, err := os.Stat(dm.path); errors.Is(err, os.ErrNotExist) { | ||
dm.SetData(AppData{}) | ||
} | ||
} | ||
|
||
func (dm DataManager) GetData() (AppData, error) { | ||
dm.FileCheck() | ||
|
||
bytes, err := os.ReadFile(dm.path) | ||
if err != nil { | ||
return AppData{}, err | ||
} | ||
|
||
var data AppData | ||
err = json.Unmarshal([]byte(bytes), &data) | ||
|
||
if err != nil { | ||
return AppData{}, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (dm DataManager) SetData(data AppData) error { | ||
dataBytes, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
os.WriteFile(dm.path, dataBytes, 0644) | ||
return nil | ||
} | ||
|
||
// - Property Methods - | ||
func (dm DataManager) GetUserId() (string, error) { | ||
data, err := dm.GetData() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return data.UserId, nil | ||
} | ||
|
||
func (dm DataManager) SetUserId(id string) error { | ||
data, err := dm.GetData() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data.UserId = id | ||
|
||
return dm.SetData(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Logging and debugging things | ||
package common | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// TODO: Make a logging manager or something | ||
|
||
func log(string string) { | ||
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
if _, err := f.WriteString(string + "\n"); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func errorLog(string string) { | ||
log("ERROR: " + string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.