Javascript
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
// "github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
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) 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 nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
func InitDB() error {
|
||||
file, err := os.Open("../db/users.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return errors.New("Failed to read db")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
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
|
||||
|
||||
return nil
|
||||
}
|
||||
31
server/db/db.js
Normal file
31
server/db/db.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import QuillDB from "../_/quilldb.js"
|
||||
|
||||
export default class Database extends QuillDB {
|
||||
|
||||
get = {
|
||||
user: (id) => {
|
||||
return this.nodes[id]
|
||||
},
|
||||
userByEmail: (email) => {
|
||||
for (const id of this.labels["User"]) {
|
||||
const user = this.get.user(id);
|
||||
if (user.email === email) {
|
||||
return { id, ...user }
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
id++;
|
||||
}
|
||||
return `user-${id}`; // O(1) most of the time
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
return { nodes: this.nodes }
|
||||
}
|
||||
}
|
||||
14
server/db/model/User.js
Normal file
14
server/db/model/User.js
Normal file
@@ -0,0 +1,14 @@
|
||||
export default function User(node) {
|
||||
let traits = [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"password"
|
||||
]
|
||||
for(let i = 0; i < traits.length; i++) {
|
||||
if(!node[traits[i]]) {
|
||||
if (traits[i] == "lastName") { continue; } // Ignores optional Last Name
|
||||
throw new Error(`User ${node.email} is missing trait ${traits[i]}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user