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

Webdb-iii-Challenge #361

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Stretch problems
  • Loading branch information
Kseniyapl committed Mar 20, 2019
commit 4189423f856dd9afbfbba7c8aa8a2e4a58a722e1
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ V `[POST] /api/cohorts` This route should save a new cohort to the database.
V `[GET] /api/cohorts` This route will return an array of all cohorts.
V `[GET] /api/cohorts/:id` This route will return the cohort with the matching `id`.
V `[GET] /api/cohorts/:id/students` returns all students for the cohort with the specified `id`.
- `[PUT] /api/cohorts/:id` This route will update the cohort with the matching `id` using information sent in the body of the request.
- `[DELETE] /api/cohorts/:id` This route should delete the specified cohort.
V `[PUT] /api/cohorts/:id` This route will update the cohort with the matching `id` using information sent in the body of the request.
V `[DELETE] /api/cohorts/:id` This route should delete the specified cohort.

## Stretch Problem

Add the following endpoints.

- `[POST] /students` This route should save a new student to the database.
- `[GET] /students` This route will return an array of all students.
- `[GET] /students/:id` This route will return the student with the matching `id`.
- `[PUT] /students/:id` This route will update the student with the matching `id` using information sent in the body of the request.
- `[DELETE] /students/:id` This route should delete the specified student.
V `[POST] /students` This route should save a new student to the database.
V `[GET] /students/:id` This route will return the student with the matching `id`.
V `[PUT] /students/:id` This route will update the student with the matching `id` using information sent in the body of the request.
V `[DELETE] /students/:id` This route should delete the specified student.

Have the student returned by the `[GET] /students/:id` endpoint include the cohort name and remove the `cohort_id` fields. The returned object should look like this:

Expand Down
Binary file modified data/lambda.sqlite3
Binary file not shown.
69 changes: 66 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const server = express();
server.use(helmet());
server.use(express.json());

// list all roles /GET
// list all cohorts /GET
server.get('/api/cohorts', async (req, res) => {
// get the roles from the database
// get the cohorts from the database
try {
const cohorts = await db('cohorts'); // all the records from the table
res.status(200).json(cohorts);
Expand All @@ -26,7 +26,7 @@ server.get('/api/cohorts', async (req, res) => {
//GET BY ID

server.get('/api/cohorts/:id', async (req, res) => {
// get the roles from the database
// get the cohorts from the database
try {
const cohort = await db('cohorts')
.where({ id: req.params.id })
Expand Down Expand Up @@ -100,5 +100,68 @@ server.delete('/api/cohorts/:id', async (req, res) => {
} catch (error) {}
});


//Stretch Problem


// list all students /GET
server.get('/api/students', async (req, res) => {
// get the roles from the database
try {
const students = await db('students'); // all the records from the table
res.status(200).json(students);
} catch (error) {
res.status(500).json(error);
}
});

//GET BY ID

server.get('/api/students/:id', async (req, res) => {
// get the roles from the database
try {
const student = await db('students')
.where({ id: req.params.id })
.first();
res.status(200).json(student);
} catch (error) {
res.status(500).json(error);
}
});

//POST
server.post('/api/students', async (req, res) => {
try {
const [id] = await db('students').insert(req.body);

const role = await db('students')
.where({ id })
.first();

res.status(201).json(role);
}catch(err) {
res.status(500).json(err);
};
});


// remove the student (inactivate the student)
server.delete('/api/students/:id', async (req, res) => {
try {
const count = await db('students')
.where({ id: req.params.id })
.del();

if (count > 0) {
res.status(204).end();
} else {
res.status(404).json({ message: 'Records not found' });
}
} catch (error) {}
});




const port = process.env.PORT || 9090;
server.listen(port, () => console.log(`\nrunning on ${port}\n`));