294 lines
6.8 KiB
JavaScript
294 lines
6.8 KiB
JavaScript
const express = require('express');
|
|
const bodyparser = require('body-parser');
|
|
const app = express();
|
|
const postmodel = require('./models/post');
|
|
const usermodel = require('./models/user');
|
|
const ownermodel = require('./models/owner');
|
|
const itemmodel = require('./models/item');
|
|
const ordermodel = require('./models/order');
|
|
const mongoose = require('mongoose');
|
|
const stdrpc = require('stdrpc');
|
|
const CoinGecko = require('coingecko-api');
|
|
//const RequestIP = require('@supercharge/request-ip');
|
|
|
|
var db = require('./config/db');
|
|
mongoose.connect(db.url).then(() => {
|
|
console.log("connecting-- ", db);
|
|
}).catch(() => {
|
|
console.log("connection failed!");
|
|
});
|
|
|
|
var fullnode = require('./config/fullnode');
|
|
const rpc = stdrpc({
|
|
url: fullnode.url,
|
|
username: fullnode.username,
|
|
password: fullnode.password
|
|
});
|
|
|
|
const CoinGeckoClient = new CoinGecko();
|
|
|
|
app.use(bodyparser.json());
|
|
|
|
app.use((req, res, next) => {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS");
|
|
//req.ip = RequestIP.getClientIp(req);
|
|
next();
|
|
});
|
|
|
|
app.post('/api/posts', (req, res, next) => {
|
|
const post = new postmodel({
|
|
title: req.body.title,
|
|
content: req.body.content
|
|
});
|
|
post.save();
|
|
//console.log(req.ip);
|
|
res.status(201).json({
|
|
message: 'Post added successfully'
|
|
});
|
|
});
|
|
|
|
app.get('/api/posts', (req, res, next) => {
|
|
postmodel.find().
|
|
then((documents) => {
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'Posts Fetched successfully',
|
|
posts: documents
|
|
});
|
|
});
|
|
});
|
|
|
|
/*
|
|
* Add a new user
|
|
* @param address: String. Shielded zcash address
|
|
* @param session: String. Browser session uuid
|
|
* @param blocktime: Number. Blocktime of login zcash transaction
|
|
*/
|
|
|
|
app.post('/api/users', (req, res, next) => {
|
|
console.log('Post: /api/users');
|
|
const user = new usermodel({
|
|
address: req.body.address,
|
|
session: req.body.session,
|
|
blocktime: req.body.blocktime
|
|
});
|
|
user.save();
|
|
res.status(201).json({
|
|
message: 'User added successfully'
|
|
});
|
|
});
|
|
|
|
app.get('/api/users', (req, res, next) => {
|
|
console.log('Get: /api/users');
|
|
usermodel.find({'address': req.query.address, 'session': req.query.session}).
|
|
then((documents) => {
|
|
console.log("Searching");
|
|
res.status(200).json({
|
|
message: 'Users found successfully',
|
|
users: documents
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/getuser', (req, res, next) => {
|
|
console.log('Get: /api/getuser/', req.query.session);
|
|
usermodel.find({'session': req.query.session}).
|
|
then((documents) => {
|
|
console.log("Searching for user...");
|
|
if(documents.length > 0){
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'User found!',
|
|
user: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'User not found!',
|
|
user: null
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
app.get('/api/blockheight', (req, res, next) => {
|
|
console.log('Get: /api/blockheight');
|
|
rpc.getblock("-1", 1).then(block => {
|
|
res.status(200).json({
|
|
message: 'Found block',
|
|
height: block.height
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
app.get('/api/txs', (req, res, next) => {
|
|
console.log('Get: /api/txs');
|
|
rpc.z_listreceivedbyaddress(fullnode.addr, 5).then(txs => {
|
|
res.status(200).json({
|
|
message: 'Transactions found',
|
|
txs: txs
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/getaddr', (req, res, next) => {
|
|
console.log('Get: /api/getaddr');
|
|
res.status(200).json({
|
|
message: 'Sending address',
|
|
addr: fullnode.addr
|
|
});
|
|
});
|
|
|
|
app.get('/api/getowner', (req, res, next) => {
|
|
console.log('Get: /api/getowner');
|
|
ownermodel.find({'address': req.query.address}).then((documents) => {
|
|
if(documents.length > 0){
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'Owner found!',
|
|
owner: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'Owner not found!',
|
|
owner: null
|
|
});
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
app.post('/api/addowner', (req, res, next) => {
|
|
console.log('Post: /api/addowner');
|
|
const owner = new ownermodel(req.body);
|
|
owner.save();
|
|
res.status(201).json({
|
|
message: 'Owner added successfully'
|
|
});
|
|
});
|
|
|
|
app.post('/api/addownername', (req, res, next) => {
|
|
console.log('Post: /api/addownername');
|
|
const owner = ownermodel.findOne({address: req.body.address});
|
|
|
|
owner.name = req.body.name;
|
|
owner.save();
|
|
res.status(201).json({
|
|
message: 'Ownername added successfully'
|
|
});
|
|
});
|
|
|
|
app.get('/api/getitems', (req, res, next) => {
|
|
console.log('Get: /api/getitems');
|
|
//console.log('getitems', req.query.address);
|
|
if (req.query.address.length > 0 ) {
|
|
const items = itemmodel.find({user: req.query.address}).then((documents) => {
|
|
if(documents.length > 0){
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'items found!',
|
|
items: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'items not found!',
|
|
items: []
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'no address',
|
|
items: []
|
|
});
|
|
}
|
|
});
|
|
|
|
app.post('/api/item', (req, res, next) => {
|
|
console.log('Post: /api/item', req.body.item);
|
|
if ( req.body.item._id == null ) {
|
|
const item = new itemmodel(req.body.item);
|
|
item.save();
|
|
res.status(201).json({
|
|
message: 'Item added'
|
|
});
|
|
} else {
|
|
console.log('Editing', req.body.item._id);
|
|
itemmodel.findByIdAndUpdate(req.body.item._id, {'name': req.body.item.name, 'description': req.body.item.description, 'cost': req.body.item.cost},
|
|
function(err, docs) {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
res.status(201).json({
|
|
message: 'Item updated'
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
app.delete('/api/item/:id', (req, res, next) => {
|
|
console.log('delete endpoint', req.params.id);
|
|
itemmodel.findByIdAndDelete(req.params.id, function (err, docs) {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
res.status(200).json({
|
|
message: 'Item deleted'
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
app.get('/api/price', (req, res, next) => {
|
|
console.log('Get /api/price');
|
|
CoinGeckoClient.simple.price({
|
|
ids: ['zcash'],
|
|
vs_currencies: ['usd']
|
|
}).
|
|
then((data) => {
|
|
|
|
res.status(200).json({
|
|
message: 'price found!',
|
|
price: data.data.zcash.usd
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/order', (req, res, next) => {
|
|
console.log('Get /api/order');
|
|
if (req.query.session.length > 0) {
|
|
const order = ordermodel.findOne({session: req.query.session, closed: false}).then((documents) => {
|
|
if (documents != null) {
|
|
console.log(documents);
|
|
res.status(200).json({
|
|
message: 'order found!',
|
|
order: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'no order found!',
|
|
order: null
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'no session received',
|
|
order: null
|
|
});
|
|
}
|
|
});
|
|
|
|
app.post('/api/order', (req, res, next) => {
|
|
console.log('Post /api/order', req.body);
|
|
const order = new ordermodel(req.body.order);
|
|
order.save();
|
|
res.status(200).json({
|
|
message: 'Order added'
|
|
});
|
|
});
|
|
|
|
module.exports = app;
|