This commit is contained in:
Auth
2024-09-14 17:51:42 -04:00
5 changed files with 117 additions and 11 deletions

Binary file not shown.

0
html/index.html Normal file
View File

24
server/api/users.js Normal file
View File

@@ -0,0 +1,24 @@
import APIRoute from "../APIRoute";
export default class UsersAPI extends APIRoute {
constructor() {
super('/users');
this.addSubRoute('/create', 'post', createUser);
}
async get(req, res) {
res.send('GET request');
}
async post(req, res) {
res.send('POST request');
}
async createUser(req, res) {
let user = req.body;
let db = req.app.get('mongo').users;
}
}

View File

@@ -0,0 +1,77 @@
import mongoose from "mongoose";
import { v4 as uuidv4 } from 'uuid';
const reqString = {
type: String,
required: true
}
const recipesSchema = new mongoose.Schema({
id: reqString,
userID: String,
ingredients: Array,
productName: String,
nutritionFacts: Object,
rating: Number,
cuisine: String,
expense: Number,
mealType: Object
}, { timestamps: true });
export default class Recipes {
constructor() {
this.model = mongoose.model('recipes', recipesSchema);
this.upsert = { upsert: true };
}
async create(recipe) {
let Id = uuidv4();
await this.model.findOneAndUpdate({ id: Id }, {
id: Id,
userID: recipe.userID,
ingredients: recipe.ingredients,
productName: recipe.productName,
nutritionFacts: recipe.nutritionFacts,
rating: recipe.rating,
cuisine: recipe.cuisine,
expense: recipe.expense,
mealType: recipe.mealType
}, this.upsert);
return await this.get(user.id);
}
async get(Id) {
return await this.model.findOne({ id: Id });
}
async getAll(query) {
let data = await this.model.find(query);
return data
}
async update(Id, data) {
if(!(await this.get(Id))) return null;
await this.model.findOneAndUpdate({ id: Id }, data, this.upsert);
return await this.get(Id);
}
async delete(Id) {
let result = await this.get(Id);
if(!result) return false;
await this.model.deleteOne({ id: Id })
return true;
}
}

View File

@@ -1,4 +1,5 @@
import mongoose from "mongoose";
import { v4 as uuidv4 } from 'uuid';
const reqString = {
type: String,
@@ -6,7 +7,14 @@ const reqString = {
}
const UserSchema = new mongoose.Schema({
id: reqString,
id: reqString,
recipes: Array,
dietaryRestrictions: Array,
firstName: String,
lastName: String,
email: String
}, { timestamps: true });
@@ -16,23 +24,20 @@ export default class Users {
this.upsert = { upsert: true };
}
makeId(length) {
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(user) {
if(await this.model.findOne({ username: user.username }) || await this.model.findOne({ email: user.email })) return null;
let Id = this.makeId(5);
let Id = uuidv4();
await this.model.findOneAndUpdate({ id: Id }, {
id: Id,
recipes: user.recipes,
dietaryRestrictions: user.dietaryRestrictions,
firstName: user.firstName,
lastName: user.lastName,
email: user.Email
}, this.upsert);
return await this.get(user.id);
}