So I am trying to make first full-stack app and I have a question related to the database and this seemed the best place to ask. So in my users need to create accounts and then they have a "cookbook" which is a list of recipes that get randomly chosen from and sent to their email weekly. What is the best way for me to associate the recipes with their respective user? I have a simple MongoDB backend built but in it, the recipes and users are saved in different places. I was thinking of linking the recipes to the user by giving the recipe its creators UUID but I also want users to be able to share their recipes and have other people add that recipe to their "cookbook". is there a way I can each user in the database an array of recipes and add the recipe to array when they create one or add someone else's or is there a better way?
here is the code for my simple mongodb backend
Recipe saving in the database
const router = require("express").Router();
let Recipe = require("../models/recipe.model");
router.route("/").get((req, res) => {
Recipe.find()
.then(rec => res.json(rec))
.catch(err => res.status(400).json("Error: "+err))
})
router.route("/add").post((req, res) => {
const name = req.body.name;
const description = req.body.description;
const imageLink = req.body.imageLink || "";
const ingredients = Array.from(req.body.ingredients);
const steps = Array.from(req.body.steps);
const isPrivate = Boolean(req.body.private);
const newRecipe = new Recipe({
name,
description,
imageLink,
ingredients,
steps,
isPrivate
});
newRecipe.save()
.then(() => res.json("Recipe added"))
.catch(err => res.status(400).json("Error: " + err))
})
router.route("/:id").get((req, res) => {
Recipe.findById(req.params.id)
.then(rec => res.json(rec))
.catch(err => res.status(400).json("Error: "+err))
})
router.route("/:id").delete((req, res) => {
Recipe.findByIdAndDelete(req.params.id)
.then(() => res.json("Recipe Deleted"))
.catch(err => res.status(400).json("Error: " + err))
})
router.route("/update/:id").post((req, res) => {
Recipe.findById(req.params.id)
.then(rec => {
rec.name = req.body.name;
rec.description = req.body.description;
rec.imageLink = req.body.imageLink;
rec.ingredients = Array.from(req.body.ingredients);
rec.steps = Array.from(req.body.steps);
rec.isPrivate = Boolean(req.body.isPrivate);
rec.save()
.then(() => res.json("Recipe updated"))
.catch(err => res.status(400).json("Error: "+err))
})
.catch(err => res.status(400).json("Error: " + err))
})
module.exports = router
and saving users into the database
const router = require("express").Router()
let User = require("../models/user.model")
router.route("/").get((req, res) => {
User.find()
.then(users => res.json(users))
.catch(err => res.status(400).json("Error: "+err));
});
router.route("/add").post((req, res) => {
const username = req.body.username;
const email = req.body.email;
const newUser = new User({username, email})
newUser.save()
.then(() => res.json("User added"))
.catch(err => res.status(400).json("Error: "+err))
})
module.exports = router