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

Project setup #358

Open
wants to merge 3 commits into
base: master
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Building an API using a Relational Database
## Building an API using a Relational Database

## Topics

Expand Down
Binary file added data/lambda.sqlite3
Binary file not shown.
105 changes: 105 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Manage Roles (id, name)
const express = require("express");
const helmet = require("helmet");
const knex = require("knex");

const knexConfig = {
client: "sqlite3",
connection: {
filename: "./data/lambda.sqlite3"
},
useNullAsDefault: true // needed for sqlite
};
const db = knex(knexConfig);

const server = express();

server.use(helmet());
server.use(express.json());

server.get("/", (req, res) => {
res.send("Server Works.");
});

server.get("/api/cohorts", async (req, res) => {
try {
const cohorts = await db("cohorts");
res.status(200).json(cohorts);
} catch (err) {
res.status(500).json({
error: "Could not get cohorts."
});
}
});

server.post("/api/cohorts", async (req, res) => {
try {
if (!req.body.name) {
res.status(500).json({
message: "Please provide a name."
});
} else {
const [id] = await db("cohorts").insert(req.body);
const cohort = await db("cohorts")
.where({ id })
.first();
res.status(201).json(cohort);
}
} catch (err) {
res.status(500).json({
error: "Could not create new cohort."
});
}
});

server.get("/api/cohorts/:id", async (req, res) => {
try {
const role = await db("cohorts")
.where({ id: req.params.id })
.first();
res.status(200).json(role);
} catch (error) {
res.status(500).json(error);
}
});

server.put("/api/cohorts/:id", async (req, res) => {
try {
const count = await db("cohorts")
.where({ id: req.params.id })
.update(req.body);

if (count > 0) {
const cohort = await db("cohorts")
.where({ id: req.params.id })
.first();

res.status(200).json(cohort);
} else {
res.status(404).json({ message: "Cohort not found" });
}
} catch (error) {}
});

server.delete("/api/cohorts/:id", async (req, res) => {
try {
const count = await db("cohorts")
.where({ id: req.params.id })
.del();

if (count > 0) {
res.status(204).json({
message: "Cohort deleted."
});
} else {
res.status(404).json({
message: "Cohort not found"
});
}
} catch (err) {}
});

const port = process.env.PORT || 5000;
server.listen(port, () =>
console.log(`\nrunning on http://localhost:${port}\n`)
);
11 changes: 11 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Update with your config settings.

module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./data/lambda.sqlite3"
},
useNullAsDefault: true
}
};
13 changes: 13 additions & 0 deletions migrations/20190220124826_cohorts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(knex, Promise) {
return knex.schema.createTable("cohorts", function(tbl) {
tbl.increments();
tbl
.string("name", 128)
.notNullable()
.unique();
});
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists("cohorts");
};
13 changes: 13 additions & 0 deletions migrations/20190220125424_students.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(knex, Promise) {
return knex.schema.createTable("students", function(tbl) {
tbl.increments();
tbl
.string("name", 128)
.notNullable()
.unique();
});
};

exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists("students");
};
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "rolex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"server": "nodemon index.js"
},
"keywords": [],
"author": "Web XVI",
"license": "ISC",
"dependencies": {
"express": "^4.16.4",
"helmet": "^3.15.1",
"knex": "^0.16.3",
"sqlite3": "^4.0.6"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
13 changes: 13 additions & 0 deletions seeds/cohorts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('table_name').del()
.then(function () {
// Inserts seed entries
return knex('table_name').insert([
{id: 1, colName: 'rowValue1'},
{id: 2, colName: 'rowValue2'},
{id: 3, colName: 'rowValue3'}
]);
});
};
Loading