generated from rocketseat-education/gostack-template-typeorm-upload
-
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.
upload de arquivo csv e impotação - refatorando código
- Loading branch information
Showing
10 changed files
with
325 additions
and
78 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,19 @@ | ||
import crypto from 'crypto'; | ||
import multer from 'multer'; | ||
import path from 'path'; | ||
|
||
const tmpFolder = path.resolve(__dirname, '..', '..', 'tmp'); | ||
|
||
export default { | ||
directory: tmpFolder, | ||
|
||
storage: multer.diskStorage({ | ||
destination: tmpFolder, | ||
filename(request, file, callback) { | ||
const fileHash = crypto.randomBytes(10).toString('HEX'); | ||
const fileName = `${fileHash}-${file.originalname}`; | ||
|
||
return callback(null, fileName); | ||
}, | ||
}), | ||
}; |
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
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
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
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 |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import { Router } from 'express'; | ||
import { getCustomRepository } from 'typeorm'; | ||
import multer from 'multer'; | ||
import uploadConfig from '../config/upload'; | ||
|
||
// import TransactionsRepository from '../repositories/TransactionsRepository'; | ||
// import CreateTransactionService from '../services/CreateTransactionService'; | ||
// import DeleteTransactionService from '../services/DeleteTransactionService'; | ||
// import ImportTransactionsService from '../services/ImportTransactionsService'; | ||
import TransactionsRepository from '../repositories/TransactionsRepository'; | ||
import CreateTransactionService from '../services/CreateTransactionService'; | ||
import DeleteTransactionService from '../services/DeleteTransactionService'; | ||
import ImportTransactionsService from '../services/ImportTransactionsService'; | ||
|
||
const transactionsRouter = Router(); | ||
const upload = multer(uploadConfig); | ||
|
||
transactionsRouter.get('/', async (request, response) => { | ||
// TODO | ||
const transactionsRepositories = getCustomRepository(TransactionsRepository); | ||
|
||
const transactions = await transactionsRepositories.find({ | ||
select: ['id', 'title', 'value', 'type', 'category'], | ||
relations: ['category'], | ||
}); | ||
|
||
const balance = await transactionsRepositories.getBalance(); | ||
return response.json({ transactions, balance }); | ||
}); | ||
|
||
transactionsRouter.post('/', async (request, response) => { | ||
// TODO | ||
const { title, value, type, category } = request.body; | ||
|
||
const createTransationService = new CreateTransactionService(); | ||
const transaction = await createTransationService.execute({ | ||
title, | ||
value, | ||
type, | ||
category, | ||
}); | ||
|
||
return response.json(transaction); | ||
}); | ||
|
||
transactionsRouter.delete('/:id', async (request, response) => { | ||
// TODO | ||
}); | ||
const { id } = request.params; | ||
|
||
transactionsRouter.post('/import', async (request, response) => { | ||
// TODO | ||
const deleteTransactionService = new DeleteTransactionService(); | ||
|
||
await deleteTransactionService.execute({ id }); | ||
|
||
return response.status(200).send(); | ||
}); | ||
|
||
transactionsRouter.post( | ||
'/import', | ||
upload.single('file'), | ||
async (request, response) => { | ||
const importTransactionsService = new ImportTransactionsService(); | ||
const transactions = await importTransactionsService.execute({ | ||
csvFile: request.file.filename, | ||
}); | ||
|
||
return response.json(transactions); | ||
}, | ||
); | ||
|
||
export default transactionsRouter; |
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
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
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
Oops, something went wrong.