Skip to content

completed mvp #135

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

Open
wants to merge 1 commit 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
56 changes: 56 additions & 0 deletions api/actions/actions-middlware.js
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
// add middlewares here related to actions
const Actions = require('./actions-model')
const Project = require('../projects/projects-model')


async function checkActionId(req, res, next){
try {
const action = await Actions.get(req.params.id);
if(action){
req.action = action;
next();
}else{
next({ status: 404, message: `Action ${req.params.id} not found`})
}
} catch (error) {
next(error)
}
}

async function checkNewAction(req,res,next){
const { description, notes } = req.body;
if(description !== undefined &&
typeof description === 'string' &&
description.length &&
description.trim().length &&
description.length < 129 &&
notes !== undefined &&
notes.length &&
notes.trim().length){
next()
}else{
res.status(400).json({
message: 'Action needs a name, valid project id and description'
})
}
}

async function checkValidProject(req,res,next){
const { project_id } = req.body
const validProject = await Project.get(project_id)
try {
if(validProject){
next();
}else{
next({ status: 404, message: `Project ${project_id} not found`})
}
} catch (error) {
next(error)
}
}


module.exports = {
checkActionId,
checkNewAction,
checkValidProject
}
54 changes: 54 additions & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
// Write your "actions" router here!
const express = require('express');
const Actions = require('./actions-model')
const { checkActionId, checkNewAction, checkValidProject } = require('./actions-middlware')

const router = express.Router();

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

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

router.post('/', [checkValidProject,checkNewAction], async (req,res,next) => {
const newPost = await Actions.insert(req.body)
try {
res.status(201).json(newPost)
} catch (error) {
next(error)
}
})

router.put('/:id', [checkActionId, checkNewAction], async (req, res, next) => {
const success = await Actions.update(req.params.id, req.body)
try {
res.status(200).json(success)
} catch (error) {
next(error)
}
})

router.delete('/:id', checkActionId, (req, res, next) => {
Actions.remove(req.params.id)
.then(() => {
res.status(200).json()
})
.catch(error => next(error))
})

router.use((error,req,res,next) => {
res.status(error.status || 500).json({
message: error.message,
customMessage: "error within the Actions router"
})
})


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

async function checkProjectId(req, res, next){
try {
const project = await Projects.get(req.params.id);
if(project){
req.project = project;
next();
}else{
next({ status: 404, message: `Project ${req.params.id} not found`})
}
} catch (error) {
next(error)
}
}

async function checkNewProject(req,res,next){
const { name, description, completed } = req.body;
if(name !== undefined &&
typeof name === 'string' &&
name.length &&
name.trim().length &&
description !== undefined &&
description.length &&
description.trim().length &&
completed !== undefined){
next()
}else{
res.status(400).json({
message: 'project needs a name and description'
})
}
}

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

const router = express.Router();

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

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

router.post('/', [checkNewProject], async (req,res,next) => {
const newPost = await Projects.insert(req.body)
try {
res.status(201).json(newPost)
} catch (error) {
next(error)
}
})

router.put('/:id', checkProjectId,checkNewProject, async (req,res,next) => {
const success = await Projects.update(req.params.id, req.body)
try {
res.status(200).json(success)
} catch (error) {
next(error)
}
})

router.delete('/:id', checkProjectId, (req, res, next) => {
Projects.remove(req.params.id)
.then(() => {
res.status(200).json()
})
.catch(error => next(error))
})

router.get('/:id/actions', checkProjectId, async(req,res,next) => {
const actions = await Projects.getProjectActions(req.params.id)
try {
res.status(200).json(actions)
} catch (error) {
next(error)
}
})

router.use((error,req,res,next) => {
res.status(error.status || 500).json({
message: error.message,
customMessage: "error within the Projects router"
})
})
module.exports = router
8 changes: 8 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const express = require('express');
const server = express();
const morgan = require('morgan')
const projectsRouter = require('./projects/projects-router');
const actionsRouter = require('./actions/actions-router')

server.use(express.json());
server.use(morgan('dev'))
server.use('/api/projects', projectsRouter);
server.use('/api/actions', actionsRouter);

// Configure your server here
// Build your actions router in /api/actions/actions-router.js
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ 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 || 9000;

server.listen(port, () => {
console.log(`\n.:: Server Running on http://localhost:${port} ::.\n`)
})
Loading