Skip to content

Commit

Permalink
backend js ok
Browse files Browse the repository at this point in the history
  • Loading branch information
edenoscherer committed May 16, 2019
1 parent 3505c3f commit 365a8da
Show file tree
Hide file tree
Showing 14 changed files with 3,761 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
19 changes: 19 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "airbnb-base",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"class-methods-use-this": ["error", { "exceptMethods": ["store", "show"] }],
"no-underscore-dangle": ["error", { "allow": ["_id"] }]
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ typings/

# next.js build output
.next

# vscode
.vscode

# upload files
tmp/*
!tmp/.gitkeep
106 changes: 106 additions & 0 deletions Box-React.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"info": {
"_postman_id": "7de34d26-553a-4a71-be91-b22462e0fe99",
"name": "Box-React",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "boxes-store",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"title\":\"teste\"\n}"
},
"url": {
"raw": "{{host}}/boxes",
"host": [
"{{host}}"
],
"path": [
"boxes"
]
}
},
"response": []
},
{
"name": "boxes view",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/x-www-form-urlencoded",
"type": "text"
}
],
"url": {
"raw": "{{host}}/boxes/5cdcc793620f164d4e3fe9d5",
"host": [
"{{host}}"
],
"path": [
"boxes",
"5cdcc793620f164d4e3fe9d5"
]
}
},
"response": []
},
{
"name": "boxes store file",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/x-www-form-urlencoded",
"type": "text"
}
],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"src": "/home/edeno/Imagens/Screenshot_2019-03-29-08-34-10-951_com.android.chrome.jpg"
}
]
},
"url": {
"raw": "{{host}}/boxes/5cdcc793620f164d4e3fe9d5/files",
"host": [
"{{host}}"
],
"path": [
"boxes",
"5cdcc793620f164d4e3fe9d5",
"files"
]
}
},
"response": []
}
],
"variable": [
{
"id": "9d3d4442-6273-41c8-a2c1-9b965135c770",
"key": "host",
"value": "http://localhost:3333",
"type": "string"
}
]
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nodemon src/server.js",
"start": "node src/server.js"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.16.4",
"mongoose": "^5.5.8",
"multer": "^1.4.1",
"socket.io": "^2.2.0"
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.13.0",
"jslint": "^0.12.1",
"nodemon": "^1.19.0"
}
}
21 changes: 21 additions & 0 deletions src/config/multer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const multer = require('multer');
const path = require('path');
const crypto = require('crypto');

const storePath = path.resolve(__dirname, '..', '..', 'tmp');

module.exports = {
dest: storePath,
storage: multer.diskStorage({
destination: (req, file, callBack) => {
callBack(null, storePath);
},
filename: (req, file, callBack) => {
crypto.randomBytes(16, (err, hash) => {
if (err) callBack(err);
file.key = `${hash.toString('hex')}-${file.originalname}`;
callBack(null, file.key);
});
},
}),
};
18 changes: 18 additions & 0 deletions src/controllers/BoxController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Box = require('../models/Box');

class BoxController {
async store(req, res) {
const box = await Box.create({ title: req.body.title });
return res.json(box);
}

async show(req, res) {
const box = await Box.findById(req.params.id).populate({
path: 'files',
options: { sort: { createdAr: -1 } },
});
return res.json(box);
}
}

module.exports = new BoxController();
15 changes: 15 additions & 0 deletions src/controllers/FileController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const File = require('../models/File');
const Box = require('../models/Box');

class FileController {
async store(req, res) {
const box = await Box.findById(req.params.id);
const file = await File.create({ title: req.file.originalname, path: req.file.key });
box.files.push(file);
await box.save();
req.io.sockets.in(box._id).emit('file', file);
return res.json(file);
}
}

module.exports = new FileController();
16 changes: 16 additions & 0 deletions src/models/Box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');

const Box = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'File' }],
},
{
timestamps: true,
},
);

module.exports = mongoose.model('Box', Box);
28 changes: 28 additions & 0 deletions src/models/File.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mongoose = require('mongoose');

const File = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
path: {
type: String,
required: true,
},
},
{
timestamps: true,
toObject: { virtuals: true },
toJSON: { virtuals: true },
},
);

function VirtualUrl() {
const url = process.env.URL || 'http://localhost:3333';
return `${url}/files/${encodeURIComponent(this.path)}`;
}

File.virtual('url').get(VirtualUrl);

module.exports = mongoose.model('File', File);
14 changes: 14 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require('express');
const multer = require('multer');
const multerConfig = require('../config/multer');

const BoxController = require('../controllers/BoxController');
const FileController = require('../controllers/FileController');

const routes = express.Router();

routes.post('/boxes', BoxController.store);
routes.get('/boxes/:id', BoxController.show);
routes.post('/boxes/:id/files', multer(multerConfig).single('file'), FileController.store);

module.exports = routes;
32 changes: 32 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express');
const path = require('path');
const cors = require('cors');
const mongoose = require('mongoose');

const app = express();
app.use(cors());
const server = require('http').Server(app);
const io = require('socket.io')(server);
const routes = require('./routes');

io.on('connection', (socket) => {
socket.on('connectRoom', (box) => {
socket.join(box);
});
});

mongoose.connect('mongodb+srv://omnistack:[email protected]/omnistack?retryWrites=true', {
useNewUrlParser: true,
});

app.use((req, res, next) => {
req.io = io;
return next();
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/files', express.static(path.resolve(__dirname, '..', 'tmp')));

app.use(routes);

server.listen(process.env.PORT || 3333);
Empty file added tmp/.gitkeep
Empty file.
Loading

0 comments on commit 365a8da

Please sign in to comment.