Skip to content

Saeed khan #95

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 4 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
47 changes: 47 additions & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
// Write your "actions" router here!
const express = require("express")

const router = express.Router();

const Action = require("./actions-model.js");

router.get('/', (req, res) => {
Action.get(req.query)
.then(actions => {
res.status(200).json(actions)
})
.catch(error => {
res.status(500).json(error)
})
})

router.get('/:id', (req, res) => {
Action.get(req.params.id)
.then(actionId => {
res.status(200).json(actionId)
})
.catch(() => {
res.status(404).json({ message: "There is no action with this ID" })
})
})

router.post('/', (req, res) => {
Action.insert(req.body)
.then(post => {
res.status(201).json(post)
})
.catch(error => {
res.status(400).json({ message: error.message })
})
})

router.delete('/:id', (req, res) => {
Action.remove(req.params.id)
.then(() => {
res.status(200).json({ message: "action has been deleted!" })
})
.catch(() => {
res.status(404).json({ message: "unable to delete action, ID for action does not exist" })
})
})

module.exports = router
69 changes: 69 additions & 0 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
// Write your "projects" router here!
const express = require("express")

const router = express.Router();

const Project = require("./projects-model.js");

router.get('/', (req, res) => {
Project.get(req.query)
.then(project => {
res.status(200).json(project)
})
.catch(error => {
res.status(500).json({ message: error.message })
})
})

router.get('/:id', (req, res) =>{
const { id } = req.params
Project.get(id)
.then(project => {
res.status(200).json(project)
})
.catch(error => {
res.status(404).json({ message: error.message })
})
})

router.get('/:id/actions', (req, res) =>{
Project.getProjectActions(req.params.id)
.then(actions => {
res.status(200).json(actions)
})
.catch(error => {
res.status(404).json({ message: error.message })
})
})

router.post('/', (req, res) => {
Project.insert(req.body)
.then(post => {
res.status(201).json(post)
})
.catch(error => {
res.status(500).json(error)
})
})

router.put('/:id', (req, res) => {
const changes = req.body;
Project.update(req.params.id, changes)
.then(project => {
res.status(200).json(project)
})
.catch(error => {
res.status(500).json({ message: error.message })
})
})

router.delete('/:id', (req, res) => {
Project.remove(req.params.id)
.then(() => {
res.status(200).json({ message: "Delete complete" })
})
.catch(error => {
res.status(404).json({ message: error.message })
})
})

module.exports = router;
12 changes: 8 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const express = require('express');
const server = express();

// 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!
const projectRouter = require("./projects/projects-router.js");
const actionsRouter = require("./actions/actions-router.js");

server.use(express.json());
server.use('/api/projects', projectRouter)
server.use('/api/actions', actionsRouter)



module.exports = server;
Binary file modified data/lambda.db3
Binary file not shown.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ 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 = 5000;

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