Skip to content

Commit

Permalink
First Commit - Back end developed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo S. de Paula committed Mar 28, 2020
0 parents commit 05519aa
Show file tree
Hide file tree
Showing 35 changed files with 14,170 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# dependencies

/node_modules
21 changes: 21 additions & 0 deletions backend/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Gustavo S. de Paula

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# BeTheHero-backend
48 changes: 48 additions & 0 deletions backend/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './src/database/db.sqlite'
},
migrations:{
directory: "./src/database/migrations"
},
useNullAsDefault: true
},

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'
}
}

};
19 changes: 19 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"author": "Gustavo S. de Paula",
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"knex": "^0.20.13",
"sqlite3": "^4.1.1"
},
"scripts": {
"dev": "nodemon src/index.js"
},
"devDependencies": {
"nodemon": "^2.0.2"
}
}
69 changes: 69 additions & 0 deletions backend/src/controllers/IncidentsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const connection = require('../database/connection')

module.exports = {

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.header('X-Total-Count', count['count(*)'])
return res.json(incidents)
},

async create (req, res) {
const {title, description, value } = req.body;

const ong_id = req.headers.authorization

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

return res.json(
{
id
}
);
},

async delete (req, res) {
const { id } = req.params;
const ong_id = req.headers.authorization

// console.log(ong_id)

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

// console.log(incident.ong_id)

if(incident.ong_id != ong_id){
return res.status(401).json({error: "Não autorizado"})
console.log(ong_id)
}
// console.log(ong_id)
await connection('incidents')
.where('id', id)
.select('ong_id').delete()

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

module.exports = {

async index (req, res) {
const ongs = await connection('ongs').select("*");

return res.json(ongs)
},

async create (req, res) {
const {name, email, whatsapp, city, uf} = req.body;

const id = crypto.randomBytes(4).toString('HEX')

// console.log(data)

await connection('ongs').insert({
id,
name,
email,
whatsapp,
city,
uf
})

return res.json(
{
id
}
);
}
}
58 changes: 58 additions & 0 deletions backend/src/controllers/ProfileController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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)
},

async create (req, res) {
const {title, description, value } = req.body;

const ong_id = req.headers.authorization

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

return res.json(
{
id
}
);
},

async delete (req, res) {
const { id } = req.params;
const ong_id = req.headers.authorization

// console.log(ong_id)

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

// console.log(incident.ong_id)

if(incident.ong_id != ong_id){
return res.status(401).json({error: "Não autorizado"})
console.log(ong_id)
}
// console.log(ong_id)
await connection('incidents')
.where('id', id)
.select('ong_id').delete()

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

module.exports = {

async create (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: "No NON found with this ID"})
}

return res.json(ong);
},

async delete (req, res) {
const { id } = req.params;
const ong_id = req.headers.authorization

// console.log(ong_id)

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

// console.log(incident.ong_id)

if(incident.ong_id != ong_id){
return res.status(401).json({error: "Não autorizado"})
console.log(ong_id)
}
// console.log(ong_id)
await connection('incidents')
.where('id', id)
.select('ong_id').delete()

return res.status(204).send()
}
}
6 changes: 6 additions & 0 deletions backend/src/database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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.
17 changes: 17 additions & 0 deletions backend/src/database/migrations/20200328152415_create_ongs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

exports.up = function(knex) {
return 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();

table.timestamps();
})
};

exports.down = function(knex) {
return knex.schema.dropTable('ongs');
};
18 changes: 18 additions & 0 deletions backend/src/database/migrations/20200328154318_create_incidents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

exports.up = function(knex) {
return knex.schema.createTable('incidents', function (table) {
table.increments();

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) {
return knex.schema.dropTable("incidents");
};
39 changes: 39 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const express = require('express');
const cors = require('cors');

const routes = require('./routes');

const app = express();

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

/**
* Rotas /routes
*/

/**
* Metodos HTTP
* GET: Acessar conteudos
* POST: Criar uma nova informação
* PUT: Alterar informação
* DELETE: Deletar
*/

/**
* Parametros
* Query: Parametros nomeados enviados na rota e servem para filtros e paginação.
* Route: Parametros para identificar recursos (sem nome)
* Body: Corpo da requisição
*/

/**
* Banco de Dados
* SQL: MySql, SQLite
* NoSQL: MongoDB
*/

app.use(routes);

app.listen(3333);

Loading

0 comments on commit 05519aa

Please sign in to comment.