forked from JAVACAFE-STUDY/chainity
-
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
Showing
12 changed files
with
299 additions
and
11 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
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,43 @@ | ||
var httpStatus = require('http-status'); | ||
var APIError = require('../helpers/APIError'); | ||
var Transaction = require('../models/transaction.model'); | ||
|
||
/** | ||
* Get Transaction list. | ||
* @property {number} req.query.skip - Number of tansaction to be skipped. | ||
* @property {number} req.query.limit - Limit number of tansaction to be returned. | ||
* @returns {TokensRequest[]} | ||
*/ | ||
function list(req, res, next) { | ||
const { limit = 50, skip = 0 } = req.query; | ||
Transaction.list({ limit, skip }) | ||
.then(transaction => res.json(transaction)) | ||
.catch(e => next(e)); | ||
} | ||
|
||
/** | ||
* Create new Transaction | ||
* @property {string} req.body.txHash - The txHash of Transaction. | ||
* @property {string} req.body.toAddress - The toAddress of Transaction. | ||
* @property {string} req.body.fromAddress - The fromAddress of Transaction. | ||
* @property {string} req.body.tokenValue - The tokenValue of Transaction. | ||
* @property {string} req.body.txType - The txType of Transaction. | ||
* @returns {User} | ||
*/ | ||
function create(req, res, next) { | ||
const transaction = new Transaction({ | ||
txHash: req.body.txHash, | ||
toAddress: req.body.toAddress, | ||
fromAddress: req.body.fromAddress, | ||
tokenValue: req.body.tokenValue, | ||
txType: req.body.txType | ||
}); | ||
|
||
transaction.save() | ||
.then(savedTransaction => res.json(savedTransaction)) | ||
.catch((e) => { | ||
next(new APIError(e.message, httpStatus.BAD_REQUEST)); | ||
}); | ||
} | ||
|
||
module.exports = { list, create }; |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
var Promise = require('bluebird'); | ||
var mongoose = require('mongoose'); | ||
var httpStatus = require('http-status'); | ||
var APIError = require('../helpers/APIError'); | ||
var config = require('../config/config'); | ||
var ObjectID = require('mongodb').ObjectID | ||
|
||
mongoose.connect('mongodb://' + config.mongo.host + ':' + config.mongo.port); | ||
|
||
/** | ||
* User Schema | ||
*/ | ||
const TransactionsSchema = new mongoose.Schema({ | ||
txHash: { | ||
type: String, | ||
required: true, | ||
unique : true | ||
}, | ||
toAddress: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true | ||
}, | ||
fromAddress: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true | ||
}, | ||
tokenValue: { | ||
type: Number, | ||
required: true | ||
}, | ||
txType: { | ||
type: String, | ||
required: true | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
registeredAt: { | ||
type: Date | ||
} | ||
}, { | ||
toObject: { | ||
virtuals: true | ||
}, | ||
toJSON: { | ||
virtuals: true | ||
} | ||
}); | ||
|
||
/** | ||
* Add your | ||
* - pre-save hooks | ||
* - validations | ||
* - virtuals | ||
*/ | ||
|
||
/** | ||
* Methods | ||
*/ | ||
TransactionsSchema.method({ | ||
}); | ||
|
||
/** | ||
* Statics | ||
*/ | ||
TransactionsSchema.statics = { | ||
/** | ||
* Get transactions | ||
* @param {ObjectId} id - The objectId of transaction. | ||
* @returns {Promise<User, APIError>} | ||
*/ | ||
get(id) { | ||
return this.findOne({ id: parseInt(id) }) | ||
.populate('toAddress') | ||
.populate('fromAddress') | ||
.exec() | ||
.then((transaction) => { | ||
if (transaction) { | ||
return transaction; | ||
} | ||
const err = new APIError('No such Transaction exists!', httpStatus.NOT_FOUND); | ||
return Promise.reject(err); | ||
}); | ||
}, | ||
|
||
/** | ||
* List transactions in descending order of 'createdAt' timestamp. | ||
* @param {number} skip - Number of transactions to be skipped. | ||
* @param {number} limit - Limit number of transactions to be returned. | ||
* @returns {Promise<User[]>} | ||
*/ | ||
list({ skip = 0, limit = 50, q = {} } = {}) { | ||
return this.find(q) | ||
.populate('toAddress') | ||
.populate('fromAddress') | ||
.sort({ createdAt: -1 }) | ||
.skip(+skip) | ||
.limit(+limit) | ||
.exec(); | ||
} | ||
}; | ||
|
||
/** | ||
* @typedef Transactions | ||
*/ | ||
module.exports = mongoose.model('Transaction', TransactionsSchema); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var express = require('express'); | ||
var transactionCtrl = require('../controllers/transaction.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.route('/') | ||
// GET /api/transactions - Get list of transaction | ||
.get(transactionCtrl.list) | ||
// POST /api/transactions - Create new transaction | ||
.post(transactionCtrl.create); | ||
|
||
module.exports = router; |
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.