From eda64391aa877ee808868e600d186363bf84a259 Mon Sep 17 00:00:00 2001 From: felixtanhm Date: Wed, 8 May 2024 14:40:23 +0800 Subject: [PATCH] feat: load user state --- .../server/controllers/pokeController.js | 1 - .../server/controllers/userController.js | 13 +++++++++++++ .../server/models/favorites.js | 19 ------------------- .../server/models/users.js | 8 ++++++++ .../server/populateDb.js | 6 ------ .../server/routes/users.js | 7 +++---- 6 files changed, 24 insertions(+), 30 deletions(-) create mode 100644 full-stack-javascript/21-inventory-application/server/controllers/userController.js delete mode 100644 full-stack-javascript/21-inventory-application/server/models/favorites.js diff --git a/full-stack-javascript/21-inventory-application/server/controllers/pokeController.js b/full-stack-javascript/21-inventory-application/server/controllers/pokeController.js index 3953a1e..a09979e 100644 --- a/full-stack-javascript/21-inventory-application/server/controllers/pokeController.js +++ b/full-stack-javascript/21-inventory-application/server/controllers/pokeController.js @@ -1,4 +1,3 @@ -const axios = require("axios"); const Pokemons = require("../models/pokemons"); const PokeDetails = require("../models/pokeDetails"); diff --git a/full-stack-javascript/21-inventory-application/server/controllers/userController.js b/full-stack-javascript/21-inventory-application/server/controllers/userController.js new file mode 100644 index 0000000..7217300 --- /dev/null +++ b/full-stack-javascript/21-inventory-application/server/controllers/userController.js @@ -0,0 +1,13 @@ +const Users = require("../models/users"); + +exports.getUser = async function (req, res, next) { + try { + const currUser = await Users.findOne({ email: "felixtanhm@gmail.com" }) + .populate("favorites") + .exec(); + + res.status(200).json(currUser); + } catch (error) { + return next(error); + } +}; diff --git a/full-stack-javascript/21-inventory-application/server/models/favorites.js b/full-stack-javascript/21-inventory-application/server/models/favorites.js deleted file mode 100644 index 8d7bc16..0000000 --- a/full-stack-javascript/21-inventory-application/server/models/favorites.js +++ /dev/null @@ -1,19 +0,0 @@ -const mongoose = require("mongoose"); - -const Schema = mongoose.Schema; - -const FavoriteSchema = new Schema({ - user: { type: Schema.Types.ObjectId, required: true }, - favorites: { - type: [Schema.Types.ObjectId], - ref: "Pokemon", - default: () => { - return null; - }, - }, - created_at: { type: Date, default: Date.now() }, - last_modified: { type: Date, default: Date.now() }, -}); - -// Export model -module.exports = mongoose.model("Favorites", FavoriteSchema); diff --git a/full-stack-javascript/21-inventory-application/server/models/users.js b/full-stack-javascript/21-inventory-application/server/models/users.js index 583460a..f8c5509 100644 --- a/full-stack-javascript/21-inventory-application/server/models/users.js +++ b/full-stack-javascript/21-inventory-application/server/models/users.js @@ -5,6 +5,14 @@ const Schema = mongoose.Schema; const UserSchema = new Schema({ name: { type: String, required: true }, email: { type: String, required: true }, + favorites: { + type: [Schema.Types.ObjectId], + ref: "Pokemon", + default: () => { + return null; + }, + }, + created_at: { type: Date, default: Date.now() }, last_modified: { type: Date, default: Date.now() }, }); diff --git a/full-stack-javascript/21-inventory-application/server/populateDb.js b/full-stack-javascript/21-inventory-application/server/populateDb.js index d63cff5..cd9c72f 100644 --- a/full-stack-javascript/21-inventory-application/server/populateDb.js +++ b/full-stack-javascript/21-inventory-application/server/populateDb.js @@ -6,7 +6,6 @@ const userArgs = process.argv.slice(2); const Pokemons = require("./models/pokemons"); const PokeDetails = require("./models/pokeDetails"); -const Favorites = require("./models/favorites"); const Users = require("./models/users"); const mongoose = require("mongoose"); @@ -109,11 +108,6 @@ async function createNewUser() { email: "felixtanhm@gmail.com", }); - const newFavorites = new Favorites({ - user: newUser, - }); - await newUser.save(); - await newFavorites.save(); console.log("Initial User Created"); } diff --git a/full-stack-javascript/21-inventory-application/server/routes/users.js b/full-stack-javascript/21-inventory-application/server/routes/users.js index 623e430..af4333b 100644 --- a/full-stack-javascript/21-inventory-application/server/routes/users.js +++ b/full-stack-javascript/21-inventory-application/server/routes/users.js @@ -1,9 +1,8 @@ -var express = require('express'); +var express = require("express"); var router = express.Router(); +const userController = require("../controllers/userController"); /* GET users listing. */ -router.get('/', function(req, res, next) { - res.send('respond with a resource'); -}); +router.get("/", userController.getUser); module.exports = router;