This commit is contained in:
metacryst
2025-09-30 17:44:39 -05:00
parent 7c8fd24b49
commit faf2041b7f
12 changed files with 421 additions and 270 deletions

View File

@@ -10,24 +10,31 @@ import (
"github.com/alexedwards/argon2id"
)
var DB map[string]interface{}
type User struct {
Email string `json:"email"`
Password string `json:"password"`
// Other fields as needed
}
var DB map[string]User
type GetService struct{}
var Get = GetService{}
func (g GetService) User(id string) (map[string]interface{}, error) {
raw, ok := DB[id]
if !ok {
return nil, errors.New("user not found")
}
userData, ok := raw.(map[string]interface{})
log.Println(userData)
if !ok {
return nil, errors.New("user data is not in expected format")
}
func (g GetService) UserByEmail(email string) (map[string]interface{}, error) {
for key, value := range DB {
if value.Email == email {
log.Println("found")
return map[string]interface{}{
"key": key,
"email": value.Email,
"password": value.Password,
}, nil
}
fmt.Printf("Key: %s, Value: %v\n", key, value)
}
return userData, nil
return nil, errors.New("user not found")
}
func InitDB() error {
@@ -38,19 +45,12 @@ func InitDB() error {
}
defer file.Close()
var data interface{}
err = json.NewDecoder(file).Decode(&data)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return errors.New("Failed to decode db")
}
result, ok := data.(map[string]interface{})
if !ok {
fmt.Println("Data is not a JSON object (map)")
return errors.New("Db is in the wrong format")
}
var result map[string]User
err = json.NewDecoder(file).Decode(&result)
if err != nil {
fmt.Println("Error decoding JSON:", err)
return errors.New("failed to decode db")
}
DB = result