Skip to content

Commit

Permalink
Backend commit
Browse files Browse the repository at this point in the history
  • Loading branch information
caiquejjx committed Mar 25, 2020
0 parents commit 2c804d8
Show file tree
Hide file tree
Showing 14 changed files with 2,845 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
45 changes: 45 additions & 0 deletions backend/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Update with your config settings.

module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./src/database/db.sqlite"
},
migrations: {
directory: "./src/database/migrations"
}
},

staging: {
client: "postgresql",
connection: {
database: "my_db",
user: "username",
password: "password"
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: "knex_migrations"
}
},

production: {
client: "postgresql",
connection: {
database: "my_db",
user: "username",
password: "password"
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: "knex_migrations"
}
}
};
18 changes: 18 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"knex": "^0.20.13",
"sqlite3": "^4.1.1"
},
"devDependencies": {
"nodemon": "^2.0.2"
},
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nodemon src/index.js"
}
}
56 changes: 56 additions & 0 deletions backend/src/controllers/IncidentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const connection = require("./database/connection");

module.exports = {
async create(req, res) {
const { title, description, value } = req.body;
const ong_id = request.headers.authorization;

const [id] = await connection("incidents").insert({
title,
description,
value,
ong_id
});

return res.json({ id });
},
async index(req, res) {
const { page = 1 } = req.query;

const [count] = await connection("incidents").count();
const incidents = await connection("incidents")
.join("ongs", "ongs_id", "=", "incidents.ong_id")
.limit(5)
.offset((page - 1) * 5)
.select([
"incidents.*",
"ongs.name",
"ongs.email",
"ongs.whatsapp",
"ongs.city",
"ongs.uf"
]);

res.headers("X-Total-Count", count["count(*)"]);
return res.json(incidents);
},
async delete(req, res) {
const { id } = req.params;
const ong_id = req.headers.authorization;

const incident = await connection("incidents")
.where("id", id)
.select("ong_id")
.first();

if (incident.ong_id != ong_id) {
return res.status(401).json({ èrror: "Operation not permitted" });
}

await connection("incidents")
.where("id", id)
.delete();

return res.status(204).send();
}
};
25 changes: 25 additions & 0 deletions backend/src/controllers/OngController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const connection = require("./database/connection");
const crypto = require("crypto");

module.exports = {
async create(req, res) {
const { name, email, whatsapp, city, uf } = req.body;
const id = crypto.randomBytes(4).toString("HEX");

connection("ongs").insert({
id,
name,
email,
whatsapp,
city,
uf
});

return res.json(id);
},
async index(req, res) {
const ongs = await connection("ongs").select("*");

return res.json(ongs);
}
};
13 changes: 13 additions & 0 deletions backend/src/controllers/ProfileController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const connection = require("./database/connection");

module.exports = {
async index(req, res) {
const ong_id = req.headers.authorization;

const incidents = await connection("incidents")
.where("ong_id", ong_id)
.select("*");

return res.json(incidents);
}
};
18 changes: 18 additions & 0 deletions backend/src/controllers/SessionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const connection = require("./database/connection");

module.exports = {
async login(req, res) {
const { id } = req.body;

const ong = await connection("ongs")
.where("id", id)
.select("name")
.first();

if (!ong) {
return res.status(400).json({ error: "Ong not found" });
}

return res.json(ong);
}
};
7 changes: 7 additions & 0 deletions backend/src/database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const knex = require("knex");

const configuration = require("../../knexfile");

const connection = knex(configuration.development);

module.exports = connection;
Binary file added backend/src/database/db.sqlite
Binary file not shown.
14 changes: 14 additions & 0 deletions backend/src/database/migrations/20200324201452_create_ongs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex) {
knex.schema.createTable("ongs", function(table) {
table.string("id").primary();
table.string("name").notNullable();
table.string("email").notNullable();
table.string("whatsapp").notNullable();
table.string("city").notNullable();
table.string("uf", 2).notNullable();
});
};

exports.down = function(knex) {
knex.schema.dropTable("ongs");
};
19 changes: 19 additions & 0 deletions backend/src/database/migrations/20200324201936_create_incidents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exports.up = function(knex) {
knex.schema.createTable("incidents", function(table) {
table.incremetns();
table.string("title").notNullable();
table.string("description").notNullable();
table.decimal("value").notNullable();

table.string("ong_id").notNullable();

table
.foreign("ong_id")
.references("id")
.inTable("ongs");
});
};

exports.down = function(knex) {
knex.schema.dropTable("incidents");
};
11 changes: 11 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require("express");
const cors = require("cors");
const routes = require("./routes");

const app = express();
app.use(cors());
app.use(express.json());

app.use(routes);

app.listen(3333);
21 changes: 21 additions & 0 deletions backend/src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require("express");
const OngController = require("./controllers/OngController");

const IncidentController = require("./controllers/IncidentController");

const ProfileController = require("./controllers/ProfileController");

const SessionController = require("./controllers/SessionController");
const routes = express.Router();

routes.get("/ongs", OngController.index);
routes.post("/ongs", OngController.create);

routes.post("/incidents", IncidentsController.create);
routes.get("incidents", IncidentController.index);
routes.delete("/incidents/:id", IncidentController.delete);

routes.get("profile", ProfileController.index);

routes.post("/sessions", SessionController.login);
module.exports = routes;
Loading

0 comments on commit 2c804d8

Please sign in to comment.