Sign In Prompt
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import mongoose from "mongoose";
|
||||
import Users from "./collections/users.js";
|
||||
import Recipes from "./collections/recipes.js";
|
||||
|
||||
export default class Mongo {
|
||||
constructor(uri) {
|
||||
this.connect(uri);
|
||||
this.users = new Users();
|
||||
this.recipes = new Recipes();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ const recipesSchema = new mongoose.Schema({
|
||||
rating: Number,
|
||||
cuisine: String,
|
||||
expense: Number,
|
||||
mealType: Object
|
||||
mealType: Object,
|
||||
ratingCount: Number
|
||||
}, { timestamps: true });
|
||||
|
||||
export default class Recipes {
|
||||
@@ -43,7 +44,9 @@ export default class Recipes {
|
||||
cuisine: recipe.cuisine,
|
||||
expense: recipe.expense,
|
||||
mealType: recipe.mealType,
|
||||
instructions: recipe.instructions
|
||||
instructions: recipe.instructions,
|
||||
ratingCount: 0,
|
||||
rating: 0
|
||||
}, this.upsert);
|
||||
return await this.get(Id);
|
||||
}
|
||||
@@ -63,6 +66,13 @@ export default class Recipes {
|
||||
return await this.get(Id);
|
||||
}
|
||||
|
||||
async increment(Id, field, amount) {
|
||||
let result = await this.get(Id);
|
||||
if(!result) return null;
|
||||
|
||||
return await this.get(Id);
|
||||
}
|
||||
|
||||
async delete(Id) {
|
||||
let result = await this.get(Id);
|
||||
if(!result) return false;
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import mongoose from "mongoose";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
const reqString = {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
|
||||
const UserSchema = new mongoose.Schema({
|
||||
id: reqString,
|
||||
recipes: Array,
|
||||
dietaryRestrictions: Array,
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
email: String
|
||||
}, { timestamps: true });
|
||||
|
||||
export default class Users {
|
||||
constructor() {
|
||||
this.model = mongoose.model('users', UserSchema);
|
||||
this.upsert = { upsert: true };
|
||||
}
|
||||
|
||||
|
||||
async create(user) {
|
||||
if(await this.model.findOne({ username: user.username }) || await this.model.findOne({ email: user.email })) return null;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
async get(Id) {
|
||||
let data = await this.model.findOne({ id: Id });
|
||||
if(data) data.password = undefined;
|
||||
return data;
|
||||
}
|
||||
|
||||
async getByUsername(username) {
|
||||
let data = await this.model.findOne({ username: username }) || await this.model.findOne({ email: username });
|
||||
if(data) data.password = undefined;
|
||||
return data;
|
||||
}
|
||||
|
||||
async getAll(query) {
|
||||
let data = await this.model.find(query);
|
||||
data.forEach(user => {
|
||||
user.password = undefined;
|
||||
});
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user