Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tamara Taylor #119

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ A _"resetdb"_ script exists that allows you to reset the database to its origina
- [ ] Install _nodemon_ as a development dependency that would not be used in production.

#### Environment Variables

- [ ] Bring the port number from the `process.env` variable, falling back to `5000` if `process.env.PORT` is undefined **!!!**

#### Endpoints
Expand Down
34 changes: 34 additions & 0 deletions api/actions/actions-middlware.js
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
// add middlewares here related to actions
const Action = require('./actions-model')

async function validateActionId (req, res, next){
try{
const action = await Actions.get(req.params.id)
if(!action){
res.status(404).json({message: 'No action found with that ID'})
}else{
req.action = action
next()
}
}catch(err){
res.status(500).json({message: 'Having trouble accessing the database'})
}
}

async function validateAction (req, res, next){
const {project_id, description, notes, completed} = req.body
if(!project_id || !project_id){
res.status(400).json({message: 'missing required project id'})
}else if(!description || !description.trim()){
res.status(400).json({message: 'missing required description field'})
}else if(!notes || !notes.trim()){
res.status(400).json({message: 'missing required notes field'})
}else{
req.project_id = project_id
req.description = description.trim()
req.notes = notes.trim()
req.completed = completed
next()
}
}

module.exports = {validateActionId, validateAction}
62 changes: 62 additions & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
// Write your "actions" router here!
const express = require('express')
const Action = require('./actions-model')
const {validateActionId, validateAction} = require('./actions-middlware')
const router = express.Router()

router.get('/', async (req, res) => {
try{
const actions = await Actions.get()
res.status(200).json(actions)
}catch(err){
next(err)
}
})

router.get('/id:', validateActionId, async (req, res, next) => {
try{
res.json(req.action)
}catch(err){
next(err)
}
})

router.post('/', async (req, res) => {
try{
const newAction = await Action.insert({
project_id: req.project_id,
description: req.description,
notes: req.notes,
completed: req.completed
})
res.status(201).json(newAction)
}catch(err){
next(err)
}
})

router.put('/:id', validateActionId, validateAction, async (req, res) => {
Action.update(req.params.id, {
project_id: req.project_id,
description: req.description,
notes: req.notes,
completed: req.completed
})
.then(() => {
return Action.get(req.params.id)
})
.then(action => {
res.json(action)
})
.catch(next)
})

router.delete('/:id', validateActionId, async (req, res, next) => {
try{
await Action.remove(req.params.id)
res.json(res.Action)
}catch(err){
next(err)
}
})

module.exports = router
32 changes: 32 additions & 0 deletions api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
// add middlewares here related to projects
const Projects = require('../projects/projects-model')

async function validateProjectId (req, res, next){
try{
const {id} = req.params
const project = await Projects.get(id)
if(project){
req.project = project
next()
}else{
next({status: 404, message: 'project not found'})
}
}catch(err){
next(err)
}
}

async function validateProject (req, res, next){
const {name, description, completed} = req.body
if(!name || !name.trim()){
res.status(400).json({message: 'missing required name field'})
}else if(!description || !description.trim()){
res.status(400).json({message: 'missing required description field'})
}else{
req.name = name.trim()
req.description = description.trim()
req.completed = completed
next()
}
}

module.exports = {validateProjectId, validateProject}
73 changes: 73 additions & 0 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
// Write your "projects" router here!
const express = require('express')
const Projects = require('./projects-model')
const router = express.Router()
const {validateProjectId, validateProject} = require('./projects-middleware')

router.get('/', async (req, res, next) => {
try{
const projects = await Projects.get()
res.status(200).json(projects)
}catch(err){
next(err)
}
})

router.get('/:id', validateProjectId, (req, res, next) => {
try{
res.json(req.project)
}catch(err){
next(err)
}
})

router.post('/', async (req, res) => {
try{
const newProject = await Projects.insert({
name: req.name,
description: req.description,
completed: req.completed
})
res.status(201).json(newProject)
}catch(err){
next(err)
}
})

router.put('/:id', validateProjectId, validateProject (req, res, next => {
const {name, description, completed} = req.body
if(!name || !description, !completed){
res.status(400).json({message: 'The project with that ID does not exist'})
}else{
Projects.update(req.params.id, req.body)
.then(() => {
return Projects.get(req.params.id)
})
.then(project => {
res.json(project)
})
.catch(next)
}
}))

router.delete('/:id', validateProjectId, async (req, res, next) => {
try{
await Projects.remove(req.params.id)
res.json(res.Projects)
}catch(err){
next(err)
}
})

router.get('/:id/actions', validateProjectId, async (req, res, next) => {
Projects.getProjectActions(req.params.id)
.then(actions => {
if(actions.length > 0){
res.status(200).json(actions)
}else{
res.status(404).json((actions))
}
})
.catch(next)
})

module.exports = router
23 changes: 23 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
const express = require('express');
const server = express();
const helmet = require('helmet')
const cors = require('cors')
const actionsRouter = require('./actions/actions-router')
const projectsRouter = require('./projects/projects-router')

// Configure your server here
// Build your actions router in /api/actions/actions-router.js
// Build your projects router in /api/projects/projects-router.js
// Do NOT `server.listen()` inside this file!
function logger(req, res, next){
console.log(`${req.method} request`)
next()
}

const errHandling = (err, req, res, next) => {
const status = err.status || 500
res.status(status).json({message: err.message})
}

server.use(express.json())
server.use(helmet())
server.use(cors())
server.use('/api/actions', logger, actionsRouter)
server.use('/api/projects', logger, projectsRouter)

server.get('/', (req, res) => {
res.send(`<h2>API is running</h2>`)
})

module.exports = server;
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ I need this code, but don't know where, perhaps should make some middleware, don

Pull your server into this file and start it!
*/
const server = require('./api/server')
const PORT = process.env.PORT || 5000
require('dotenv').config()

server.use(express.json())

server.listen(PORT, () => {
console.log(`Server is listening on port ${port}`)
})
Loading