-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2c804d8
Showing
14 changed files
with
2,845 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
14 changes: 14 additions & 0 deletions
14
backend/src/database/migrations/20200324201452_create_ongs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
backend/src/database/migrations/20200324201936_create_incidents.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.