diff --git a/config/connection.js b/config/connection.js index b89f6b7..7f2fd66 100644 --- a/config/connection.js +++ b/config/connection.js @@ -1,9 +1,10 @@ const Sequelize = require('sequelize'); - require('dotenv').config() -module.exports = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { +const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { host: 'localhost', dialect: 'mysql', port: 3306 -}) \ No newline at end of file +}) + +module.exports = sequelize; \ No newline at end of file diff --git a/controllers/api/user-routes b/controllers/api/user-routes deleted file mode 100644 index 32316d5..0000000 --- a/controllers/api/user-routes +++ /dev/null @@ -1,6 +0,0 @@ -const router = require('express').Router() -const { User, Monthly, Yearly, Single, Income } = require('../../models') - - - -module.exports = router \ No newline at end of file diff --git a/controllers/api/user-routes.js b/controllers/api/user-routes.js new file mode 100644 index 0000000..321bcad --- /dev/null +++ b/controllers/api/user-routes.js @@ -0,0 +1,84 @@ +const router = require('express').Router() +const { User, Monthly, Yearly, Single, Income } = require('../../models') + +// GET /api/users +router.get("/", (req, res) => { + // Finds all the info for Users on the table. + User.findAll({ + // Prevents the password from being pulled + attributes: { exclude: ["password"] }, + }) + // Sends the info back to the client + .then((dbUserData) => res.json(dbUserData)) + // If there is an error, send the error back to the client + .catch((err) => { + console.log(err); + res.status(500).json(err); + }); +}); + +// GET /api/users/1 +router.get("/:id", (req, res) => { + // Finds a specified user + User.findOne({ + // Prevents the password from being pulled + attributes: { exclude: ["password"] }, + }) + .then((dbUserData) => { + if (!dbUserData) { + res.status(404).json({ message: "No user found with this id" }); + return; + } + res.json(dbUserData); + }) + .catch((err) => { + console.log(err); + res.status(500).json(err); + }); +}); + +// POST /api/users +router.post("/", (req, res) => { + // should expect {usernam: 'value', email: 'value@value.com', password: 'value'} + console.log(req.body); + // Sets the information that must be put in + User.create({ + // requires the username, email, and password + username: req.body.username, + email: req.body.email, + password: req.body.password, + }).then(dbUserData => { + console.log(dbUserData); + res.json(dbUserData); + }) .catch(err => { + console.log(err); + res.status(500).json(err); +}); +}); + +// DELETE /api/users/1 +// Deletes the information stored in user at this id +router.delete("/:id", (req, res) => { + // Sets the table to be deleted + User.destroy({ + where: { + // Specifies the exact location where it will delete data from + id: req.params.id, + }, + }) + .then((dbUserData) => { + if (!dbUserData) { + // Will send a message saying it cannot find the user + res.status(404).json({ message: "No user found with this id" }); + return; + } + res.json(dbUserData); + }) + // Returns a response should there be an error + .catch((err) => { + console.log(err); + res.status(500).json(err); + }); +}); + +module.exports = router \ No newline at end of file diff --git a/frameworks/Dashboard.png b/frameworks/Dashboard.png new file mode 100644 index 0000000..bb4ee53 Binary files /dev/null and b/frameworks/Dashboard.png differ diff --git a/frameworks/Homepage.png b/frameworks/Homepage.png new file mode 100644 index 0000000..b0cf86b Binary files /dev/null and b/frameworks/Homepage.png differ diff --git a/models/User.js b/models/User.js index cb159bb..583189d 100644 --- a/models/User.js +++ b/models/User.js @@ -67,4 +67,4 @@ User.init( } ); -module.export = User; \ No newline at end of file +module.exports = User; \ No newline at end of file diff --git a/package.json b/package.json index 5c05913..8ce6a7f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "server.js", "scripts": { - "start": "node server.js" + "start": "node server.js", + "seed": "node seeds/index.js" }, "repository": { "type": "git", diff --git a/server.js b/server.js index 92dd729..4363e1d 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ const sequelize = require('./config/connection') const routes = require('./controllers') const app = express() -const PORT = process.env.PORT || 3001 +const PORT = process.env.PORT || 3006 app.use(express.json()); app.use(express.urlencoded({ extended: true }));