From 2abe1a68342c246c90d87d90e06b50ab0c9793c2 Mon Sep 17 00:00:00 2001 From: PieLord757 Date: Sat, 14 Sep 2024 17:36:43 -0400 Subject: [PATCH 1/2] Recipes and User JS files for MongoDB --- Backend/src/__pycache__/db.cpython-312.pyc | Bin 0 -> 1906 bytes server/storage/collections/recipes.js | 77 +++++++++++++++++++++ server/storage/collections/users.js | 27 +++++--- 3 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 Backend/src/__pycache__/db.cpython-312.pyc create mode 100644 server/storage/collections/recipes.js diff --git a/Backend/src/__pycache__/db.cpython-312.pyc b/Backend/src/__pycache__/db.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1aa74691ac8d837a09e5098547a86790f56df6d5 GIT binary patch literal 1906 zcmcIk&2JM&6rcU_S3U?Ku5Ch_R47STA>IPLR4oNj+CU|Ygi0etl~&H~CfU?po0)Zy zkt5}hLk{J@$#8-bBI45jqbDnsVr_G)a^OblLnKbTH?zAq<)h-zC;7K;-@G^L_j|vY zKZb@f1nl|a{pEd`kiYRIGBtF1KZDaIHnF7&StK%kLpe;%~rb0}MV=|8= zAeM|{3Xi2AmX2d;Q_?ffahsBb7?~70y-{%5#DTBaWKp)IO|qyoWnBr9*K6KN?aE~< z{2ROg|Fiv(>Usx~d*X}6d|S)eueBYD%Y z+`7YpBo&S>2P$(a%R$1bdCTq!!=5H5Uw&usI)kmczn1GYYqeahmn)T~aiwN8s_+SB z%)9>8#**>Vk5|jqFKq7YIpZ=oj%OQ;T86z;tT%&{X}X^4o95k-Z~(<9f<&+{0XE29 zequL2+sV&9{r>l_I%h8KcCtFWXZCWRJn$c_KV09*&26do z275?LX?~o0;dfM0?mLY45l8reb2WkPw1{+Z(Iiy1q9NT?IoH9m%nez zbQt}U0O2e}yV(ETVLsG&1enPt05(W>bPOm?cC&@{!u^Ha>{KT^^?0R|EdtRQ&Pa)# zfJ~Z0c+DdA%o`s9CuULB43WbrftA>zBlhl-nh+ATw77=AH4+l}12}IGDFZdpzNDq} z6J;aiI09}h72a^L+ysfZwXOv!$|M+Yy%p-%c!XFm=z7ej4a;|H9t#q*?89oH;z80i z!7_43ClNkHI1Rw?e@~+I@qSG76ceAt#2Ek}ek>2fr@Q0RTk4;weD~C6_$+ik%0YO5 zuii_)X8<3=E)-!5_l#t$X$3I=T=_s54`K<@5?Rdt|082mj;?UU3LRsBjsv_Y;F0kD zikZaz{w3kuc>B)%JG;4=PHyIDx|2JHcg1@c#C|ElDcmb!P}a^125|-Ejd5{jaLIGp zdF9aq@iO(bxPZQ)(@=r3WS;}PP$WtEiwqyCs#H860Ec6WWE>EH!{LNfI3NIrBZ_qX JABc%>zXCAORo4Ij literal 0 HcmV?d00001 diff --git a/server/storage/collections/recipes.js b/server/storage/collections/recipes.js new file mode 100644 index 0000000..e121e33 --- /dev/null +++ b/server/storage/collections/recipes.js @@ -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; + } +} \ No newline at end of file diff --git a/server/storage/collections/users.js b/server/storage/collections/users.js index 5593f57..7689b76 100644 --- a/server/storage/collections/users.js +++ b/server/storage/collections/users.js @@ -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); } From 2b28768d8658742cff9a923fab74221ca2444f5e Mon Sep 17 00:00:00 2001 From: GamerBoss101 Date: Sat, 14 Sep 2024 17:40:29 -0400 Subject: [PATCH 2/2] index.html commit --- html/index.html | 0 server/api/users.js | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 html/index.html create mode 100644 server/api/users.js diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..e69de29 diff --git a/server/api/users.js b/server/api/users.js new file mode 100644 index 0000000..744238e --- /dev/null +++ b/server/api/users.js @@ -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; + + } +} \ No newline at end of file