├── README.md ├── .gitignore ├── api.http ├── app ├── config │ └── db.config.js ├── models │ ├── index.js │ ├── wallet_info.model.js │ └── pair_info.model.js ├── routes │ ├── pair_info.routes.js │ └── wallet_info.routes.js └── controllers │ ├── pair_info.js │ └── wallet_info.js ├── package.json └── server.js /README.md: -------------------------------------------------------------------------------- 1 | # telegram-bot-backend -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /api.http: -------------------------------------------------------------------------------- 1 | 2 | 3 | DELETE http://localhost:8080/api/pair_infos/:66238090dac831d77e4b6c83 -------------------------------------------------------------------------------- /app/config/db.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | url: "mongodb://0.0.0.0:27017/ton_bot_db" 3 | }; 4 | -------------------------------------------------------------------------------- /app/models/index.js: -------------------------------------------------------------------------------- 1 | const dbConfig = require("../config/db.config.js"); 2 | 3 | const mongoose = require("mongoose"); 4 | mongoose.Promise = global.Promise; 5 | 6 | const db = {}; 7 | db.mongoose = mongoose; 8 | db.url = dbConfig.url; 9 | db.pair_infos = require("./pair_info.model.js")(mongoose); 10 | db.wallet_infos = require("./wallet_info.model.js")(mongoose); 11 | 12 | module.exports = db; 13 | 14 | -------------------------------------------------------------------------------- /app/models/wallet_info.model.js: -------------------------------------------------------------------------------- 1 | module.exports = mongoose => { 2 | var schema = mongoose.Schema( 3 | { 4 | wallet_address: String, 5 | mnemonics: String, 6 | }, 7 | { timestamps: true } 8 | ); 9 | 10 | schema.method("toJSON", function() { 11 | const { __v, _id, ...object } = this.toObject(); 12 | object.id = _id; 13 | return object; 14 | }); 15 | 16 | const Wallet_Info = mongoose.model("wallet_info", schema); 17 | return Wallet_Info; 18 | }; 19 | -------------------------------------------------------------------------------- /app/models/pair_info.model.js: -------------------------------------------------------------------------------- 1 | module.exports = mongoose => { 2 | var schema = mongoose.Schema( 3 | { 4 | jetton0: String, 5 | jetton1: String, 6 | sell_limit: Number, 7 | buy_limit: Number, 8 | }, 9 | { timestamps: true } 10 | ); 11 | 12 | schema.method("toJSON", function() { 13 | const { __v, _id, ...object } = this.toObject(); 14 | object.id = _id; 15 | return object; 16 | }); 17 | 18 | const Pair_Info = mongoose.model("pair_info", schema); 19 | return Pair_Info; 20 | }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-express-mongodb", 3 | "version": "1.0.0", 4 | "description": "Node.js Restful CRUD API with Node.js, Express and MongoDB", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "nodejs", 11 | "express", 12 | "rest", 13 | "api", 14 | "mongodb" 15 | ], 16 | "author": "bezkoder", 17 | "license": "ISC", 18 | "dependencies": { 19 | "cors": "^2.8.5", 20 | "express": "^4.18.2", 21 | "mongoose": "^6.11.1" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/routes/pair_info.routes.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | const pair_infos = require("../controllers/pair_info.js"); 3 | 4 | var router = require("express").Router(); 5 | 6 | // Create a new Tutorial 7 | router.post("/", pair_infos.create); 8 | 9 | // Retrieve all pair_infos 10 | router.get("/", pair_infos.findAll); 11 | 12 | // Retrieve a single Tutorial with id 13 | router.get("/:id", pair_infos.findOne); 14 | 15 | // Update a Tutorial with id 16 | router.put("/:id", pair_infos.update); 17 | 18 | // Delete a Tutorial with id 19 | router.delete("/:id", pair_infos.delete); 20 | 21 | // Create a new Tutorial 22 | router.delete("/", pair_infos.deleteAll); 23 | 24 | app.use("/api/pair_infos", router); 25 | }; 26 | -------------------------------------------------------------------------------- /app/routes/wallet_info.routes.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | const wallet_infos = require("../controllers/wallet_info.js"); 3 | 4 | var router = require("express").Router(); 5 | 6 | // Create a new Tutorial 7 | router.post("/", wallet_infos.create); 8 | 9 | // Retrieve all wallet_infos 10 | router.get("/", wallet_infos.findOne); 11 | 12 | // Retrieve a single Tutorial with id 13 | // router.get("/:id", wallet_infos.findOne); 14 | 15 | // Update a Tutorial with id 16 | router.put("/:id", wallet_infos.update); 17 | 18 | // Delete a Tutorial with id 19 | router.delete("/:id", wallet_infos.delete); 20 | 21 | // Create a new Tutorial 22 | router.delete("/", wallet_infos.deleteAll); 23 | 24 | app.use("/api/wallet_infos", router); 25 | }; 26 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const cors = require("cors"); 3 | 4 | const app = express(); 5 | 6 | app.use(cors({})); 7 | 8 | // parse requests of content-type - application/json 9 | app.use(express.json()); 10 | 11 | // parse requests of content-type - application/x-www-form-urlencoded 12 | app.use(express.urlencoded({ extended: true })); 13 | 14 | const db = require("./app/models"); 15 | db.mongoose 16 | .connect(db.url, { 17 | useNewUrlParser: true, 18 | useUnifiedTopology: true 19 | }) 20 | .then(() => { 21 | console.log("Connected to the database!"); 22 | }) 23 | .catch(err => { 24 | console.log("Cannot connect to the database!", err); 25 | process.exit(); 26 | }); 27 | 28 | // simple route 29 | app.get("/", (req, res) => { 30 | res.json({ message: "Welcome to ton_bot_db application." }); 31 | }); 32 | 33 | require("./app/routes/pair_info.routes")(app); 34 | require("./app/routes/wallet_info.routes")(app); 35 | 36 | // set port, listen for requests 37 | const PORT = process.env.PORT || 8080; 38 | app.listen(PORT, () => { 39 | console.log(`Server is running on port ${PORT}.`); 40 | }); 41 | -------------------------------------------------------------------------------- /app/controllers/pair_info.js: -------------------------------------------------------------------------------- 1 | const db = require("../models"); 2 | const Pair_Info = db.pair_infos; 3 | 4 | // Create and Save a new Pair_Info 5 | exports.create = (req, res) => { 6 | // Validate request 7 | if (!req.body.jetton0) { 8 | res.status(400).send({ message: "Content can not be empty!" }); 9 | return; 10 | } 11 | 12 | // Create a Pair_Info 13 | const pair_info = new Pair_Info({ 14 | jetton0: req.body.jetton0, 15 | jetton1: req.body.jetton1, 16 | sell_limit: req.body.sell_limit, 17 | buy_limit: req.body.buy_limit 18 | }); 19 | 20 | // Save Pair_Info in the database 21 | pair_info.save(pair_info) 22 | .then(data => { 23 | res.send(data); 24 | }) 25 | .catch(err => { 26 | res.status(500).send({ 27 | message: 28 | err.message || "Some error occurred while creating the Pair_Info." 29 | }); 30 | }); 31 | }; 32 | 33 | // Retrieve all Pair_Infos from the database. 34 | exports.findAll = (req, res) => { 35 | const jetton0 = req.query.jetton0; 36 | var condition = jetton0 ? { jetton0: { $regex: new RegExp(jetton0), $options: "i" } } : {}; 37 | 38 | Pair_Info.find(condition) 39 | .then(data => { 40 | res.send(data); 41 | }) 42 | .catch(err => { 43 | res.status(500).send({ 44 | message: 45 | err.message || "Some error occurred while retrieving Pair_Infos." 46 | }); 47 | }); 48 | }; 49 | 50 | // Find a single Pair_Info with an id 51 | exports.findOne = (req, res) => { 52 | const id = req.params.id; 53 | 54 | Pair_Info.findById(id) 55 | .then(data => { 56 | if (!data) 57 | res.status(404).send({ message: "Not found Pair_Info with id " + id }); 58 | else res.send(data); 59 | }) 60 | .catch(err => { 61 | res 62 | .status(500) 63 | .send({ message: "Error retrieving Pair_Info with id=" + id }); 64 | }); 65 | }; 66 | 67 | // Update a Pair_Info by the id in the request 68 | exports.update = (req, res) => { 69 | if (!req.body) { 70 | return res.status(400).send({ 71 | message: "Data to update can not be empty!" 72 | }); 73 | } 74 | 75 | const id = req.params.id; 76 | 77 | Pair_Info.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) 78 | .then(data => { 79 | if (!data) { 80 | res.status(404).send({ 81 | message: `Cannot update Pair_Info with id=${id}. Maybe Pair_Info was not found!` 82 | }); 83 | } else res.send(data); 84 | }) 85 | .catch(err => { 86 | res.status(500).send({ 87 | message: "Error updating Pair_Info with id=" + id 88 | }); 89 | }); 90 | }; 91 | 92 | // Delete a Pair_Info with the specified id in the request 93 | exports.delete = (req, res) => { 94 | const id = req.params.id; 95 | 96 | Pair_Info.findByIdAndRemove(id, { useFindAndModify: false }) 97 | .then(data => { 98 | if (!data) { 99 | res.status(404).send({ 100 | message: `Cannot delete Pair_Info with id=${id}. Maybe Pair_Info was not found!` 101 | }); 102 | } else { 103 | res.send({ 104 | message: "Pair_Info was deleted successfully!" 105 | }); 106 | } 107 | }) 108 | .catch(err => { 109 | res.status(500).send({ 110 | message: "Could not delete Pair_Info with id=" + id 111 | }); 112 | }); 113 | }; 114 | 115 | // Delete all Pair_Infos from the database. 116 | exports.deleteAll = (req, res) => { 117 | Pair_Info.deleteMany({}) 118 | .then(data => { 119 | res.send({ 120 | message: `${data.deletedCount} Pair_Infos were deleted successfully!` 121 | }); 122 | }) 123 | .catch(err => { 124 | res.status(500).send({ 125 | message: 126 | err.message || "Some error occurred while removing all Pair_Infos." 127 | }); 128 | }); 129 | }; 130 | -------------------------------------------------------------------------------- /app/controllers/wallet_info.js: -------------------------------------------------------------------------------- 1 | const db = require("../models"); 2 | const Wallet_Info = db.wallet_infos; 3 | 4 | // Create and Save a new Wallet_Info 5 | exports.create = (req, res) => { 6 | // Validate request 7 | if (!req.body.wallet_address) { 8 | res.status(400).send({ message: "Content can not be empty!" }); 9 | return; 10 | } 11 | 12 | // Create a Wallet_Info 13 | const wallet_Info = new Wallet_Info({ 14 | wallet_address: req.body.wallet_address, 15 | mnemonics: req.body.mnemonics, 16 | }); 17 | 18 | // Save Wallet_Info in the database 19 | wallet_Info 20 | .save(wallet_Info) 21 | .then(data => { 22 | res.send(data); 23 | }) 24 | .catch(err => { 25 | res.status(500).send({ 26 | message: 27 | err.message || "Some error occurred while creating the Wallet_Info." 28 | }); 29 | }); 30 | }; 31 | 32 | // Retrieve all Wallet_Infos from the database. 33 | exports.findAll = (req, res) => { 34 | const wallet_address = req.query.wallet_address; 35 | var condition = wallet_address ? { wallet_address: { $regex: new RegExp(wallet_address), $options: "i" } } : {}; 36 | 37 | wallet_Info.find(condition) 38 | .then(data => { 39 | res.send(data); 40 | }) 41 | .catch(err => { 42 | res.status(500).send({ 43 | message: 44 | err.message || "Some error occurred while retrieving Wallet_Infos." 45 | }); 46 | }); 47 | }; 48 | 49 | // Find a single Wallet_Info with an id 50 | exports.findOne = (req, res) => { 51 | // const id = req.params.id; 52 | 53 | Wallet_Info.find().sort({ $natural: -1 }).limit(1) 54 | .then(data => { 55 | if (data.length === 0) 56 | res.status(404).send({ message: "No Wallet_Info found" }); 57 | else res.send(data[0]); 58 | }) 59 | .catch(err => { 60 | res 61 | .status(500) 62 | .send({ message: "Error retrieving last Wallet_Info" }); 63 | }); 64 | 65 | }; 66 | 67 | // Update a Wallet_Info by the id in the request 68 | exports.update = (req, res) => { 69 | if (!req.body) { 70 | return res.status(400).send({ 71 | message: "Data to update can not be empty!" 72 | }); 73 | } 74 | 75 | const id = req.params.id; 76 | 77 | wallet_Info.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) 78 | .then(data => { 79 | if (!data) { 80 | res.status(404).send({ 81 | message: `Cannot update Wallet_Info with id=${id}. Maybe Wallet_Info was not found!` 82 | }); 83 | } else res.send({ message: "Wallet_Info was updated successfully." }); 84 | }) 85 | .catch(err => { 86 | res.status(500).send({ 87 | message: "Error updating Wallet_Info with id=" + id 88 | }); 89 | }); 90 | }; 91 | 92 | // Delete a Wallet_Info with the specified id in the request 93 | exports.delete = (req, res) => { 94 | const id = req.params.id; 95 | 96 | wallet_Info.findByIdAndRemove(id, { useFindAndModify: false }) 97 | .then(data => { 98 | if (!data) { 99 | res.status(404).send({ 100 | message: `Cannot delete Wallet_Info with id=${id}. Maybe Wallet_Info was not found!` 101 | }); 102 | } else { 103 | res.send({ 104 | message: "Wallet_Info was deleted successfully!" 105 | }); 106 | } 107 | }) 108 | .catch(err => { 109 | res.status(500).send({ 110 | message: "Could not delete Wallet_Info with id=" + id 111 | }); 112 | }); 113 | }; 114 | 115 | // Delete all Wallet_Infos from the database. 116 | exports.deleteAll = (req, res) => { 117 | wallet_Info.deleteMany({}) 118 | .then(data => { 119 | res.send({ 120 | message: `${data.deletedCount} Wallet_Infos were deleted successfully!` 121 | }); 122 | }) 123 | .catch(err => { 124 | res.status(500).send({ 125 | message: 126 | err.message || "Some error occurred while removing all Wallet_Infos." 127 | }); 128 | }); 129 | }; 130 | --------------------------------------------------------------------------------