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

Initial #84

Open
wants to merge 12 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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ In meeting the minimum viable product (MVP) specifications listed below, your pr

### Task 1: Project Set Up

- [ ] Fork and clone this repository. **If you are repeating this Course, delete your old fork from Github and re-fork and re-clone.**
- [ ] Create a new branch: `git checkout -b <firstName-lastName>`.
- [ ] Implement the project on your newly created branch, committing changes regularly.
- [ ] Push commits: `git push origin <firstName-lastName>`.
- [ ] **RUN** `npm install` to install your dependencies.
- [x] Fork and clone this repository. **If you are repeating this Course, delete your old fork from Github and re-fork and re-clone.**
- [x] Create a new branch: `git checkout -b <firstName-lastName>`.
- [x] Implement the project on your newly created branch, committing changes regularly.
- [x] Push commits: `git push origin <firstName-lastName>`.
- [x] **RUN** `npm install` to install your dependencies.

### Task 2: CodeGrade Setup

- [ ] Follow [instructions](https://www.notion.so/lambdaschool/Submitting-an-assignment-via-Code-Grade-A-Step-by-Step-Walkthrough-07bd65f5f8364e709ecb5064735ce374) to set up Codegrade's Webhook and Deploy Key, making sure your deployment is set to your `<firstName-lastName>` branch.
- [ ] Make a commit and push it to Github.
- [ ] Check to see that Codegrade has accepted your git submission.
- [x] Make a commit and push it to Github.
- [x] Check to see that Codegrade has accepted your git submission.

### Task 3: Project Requirements (MVP)

Expand Down
61 changes: 60 additions & 1 deletion api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
// Write your "actions" router here!
const express = require('express')
const {
validateActionId,
validateAction
} = require('../middleware')

const Action = require('./actions-model')

const router = express.Router()

router.get('/', (req, res) => {
Action.get()
.then(actions => {
res.json(actions)
})
})

router.get('/:id', validateActionId, (req, res) => {
res.json(req.action)
})

router.post('/', validateAction, (req, res, next) => {
Action.insert({
project_id: req.project_id,
description: req.description,
notes: req.notes,
completed: req.completed
})
.then(newAction => {
res.status(201).json(newAction)
})
.catch(next)
})

router.put('/:id', validateActionId, validateAction, (req, res, next) => {
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
99 changes: 99 additions & 0 deletions api/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const Project = require('./projects/projects-model')
const Action = require('./actions/actions-model')

function logger(req, res, next) {
const timestamp = new Date().toLocaleString()
const method = req.method
const url = req.originalUrl
console.log(`[${timestamp}] [${method}] to ${url}`)
next()
}

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

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()
}
}

async function validateActionId(req, res, next) {
try {
const action = await Action.get(req.params.id)
if (!action) {
next({
status: 404,
message: 'action not found'
})
} else {
req.action = action
next()
}
} catch (err) {
res.status(500).json({
message: 'problem finding action'
})
}
}

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 = {
logger,
validateProjectId,
validateProject,
validateActionId,
validateAction
}
79 changes: 79 additions & 0 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
// Write your "projects" router here!
const express = require('express')
const {
validateProjectId,
validateProject
} = require('../middleware')

const Project = require('./projects-model')

const router = express.Router()

router.get('/', (req, res, next) => {
Project.get()
.then(projects => {
res.json(projects)
})
.catch(next)
})

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

router.post('/', validateProject, (req, res, next) => {
Project.insert({
name: req.name,
description: req.description,
completed: req.completed
})
.then(newProject => {
res.status(201).json(newProject)
})
.catch(next)
})

router.put('/:id', validateProjectId, validateProject, (req, res, next) => {
Project.update(req.params.id, {
name: req.name,
description: req.description,
completed: req.completed
})
.then(() => {
return Project.get(req.params.id)
})
.then(project => {
res.json(project)
})
.catch(next)
})

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

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

router.use((err, req, res, next) => { //eslint-disable-line
res.status(err.status || 500).json({
customMessage: 'something tragic happened',
message: err.message,
stack: err.stack,
})
})

module.exports = router
14 changes: 10 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const express = require('express');
const { logger } = require('./middleware')
const server = express();
const projectsRouter = require(`./projects/projects-router`)
const actionsRouter = require(`./actions/actions-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!
server.use(express.json())
server.use(logger)
server.use('/api/projects', projectsRouter)
server.use('/api/actions', actionsRouter)
server.get('/', (req, res) => {
res.send(`<h2>Hello!</h2>`)
})

module.exports = server;
Binary file modified data/lambda.db3
Binary file not shown.
17 changes: 6 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/*
play this: https://www.youtube.com/watch?v=d-diB65scQU
const express = require('express')

Sing along:
const server = require('./api/server')

here's a little code I wrote, please read the README word for word, don't worry, you got this
in every task there may be trouble, but if you worry you make it double, don't worry, you got this
ain't got no sense of what is REST? just concentrate on learning Express, don't worry, you got this
your file is getting way too big, bring a Router and make it thin, don't worry, be crafty
there is no data on that route, just write some code, you'll sort it out… don't worry, just hack it…
I need this code, but don't know where, perhaps should make some middleware, don't worry, just hack it
server.use(express.json())

Pull your server into this file and start it!
*/
server.listen(4000, () => {
console.log('Server listening on', 4000)
})
Loading