Database and User Creation Update

This commit is contained in:
2025-04-12 23:02:32 -04:00
parent 268250b53b
commit b8259eca00
4 changed files with 98 additions and 39 deletions

View File

@@ -1,39 +1,82 @@
import mongoose from "mongoose";
const reqString = {
type: String,
required: true,
}
type: String,
required: true,
};
const userSchema = new mongoose.Schema({
email: reqString,
username: reqString,
points: Number,
inventory : Array,
id: reqString,
friends: Array,
requests: Array,
})
id: reqString,
email: reqString,
username: reqString,
avatar: String,
points: Number,
inventory: Array,
friends: Array<String>,
requests: Array<String>,
});
export class User {
model: mongoose.Model<any>;
upsert: any;
constructor() {
this.model = mongoose.model('user', userSchema);
this.upsert = { upsert: true };
}
async create(email: string, username: string, points: number, inventory: Array<any>, id: string, friends: Array<any>, requests: Array<any>) {
const newEntry = new this.model({
email: email,
username: username,
points: points,
inventory: inventory,
id: id,
friends: friends,
requests: requests,
});
await newEntry.save();
return newEntry;
}
}
model: mongoose.Model<any>;
upsert: any;
constructor() {
this.model = mongoose.model("users", userSchema);
this.upsert = { upsert: true };
}
makeId(length: number) {
var result = [];
var characters = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
}
return result.join('');
}
async create(
email: string,
username: string
) {
const id = this.makeId(5);
const newEntry = new this.model({
id: id,
email: email,
username: username,
avatar: null,
points: 0,
inventory: [],
friends: [],
requests: []
});
await newEntry.save();
return newEntry;
}
async findById(id: string) {
return await this.model.findOne({ id: id });
}
async findByEmail(email: string) {
return await this.model.findOne({ email: email });
}
async findByUsername(username: string) {
return await this.model.findOne({ username: username });
}
async update(Id: string, data: any) {
return await this.model.updateOne({ id: Id }, { $set: data }, this.upsert);
}
async delete(Id: string) {
return await this.model.deleteOne({ id: Id });
}
async getAll() {
return await this.model.find({});
}
}
export default User;