Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. //geo lotto
  2. //
  3. ////
  4. ////
  5. ////
  6. ////
  7. ////
  8. //
  9.  
  10. const express = require("express");
  11. const mysql = require("mysql");
  12. const app = express();
  13.  
  14. function connectToDB() {
  15. const con = mysql.createConnection(
  16. process.env["DATABASE"] ||
  17. "mysql://uc5pedch5k37uuom:q491vkdlzk275756@gk90usy5ik2otcvi.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/d9g1kaehhpss06yw"
  18. );
  19. con.connect(err => {
  20. //if (err) throw err;
  21. console.log("Succesfully connected to database");
  22. });
  23. return con;
  24. }
  25.  
  26. app.use((req, res, next) => {
  27. res.append("Access-Control-Allow-Origin", ["*"]);
  28. res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
  29. res.append("Access-Control-Allow-Headers", "Content-Type");
  30. next();
  31. });
  32.  
  33. app.get("/", (req, res) => {});
  34.  
  35. app.get("/order/:id", (req, res) => {
  36. //nazwa, telefon, adres
  37. const id = req.params.id;
  38. const con = connectToDB();
  39. const sql = /*sql*/ `
  40. SELECT distinct r.name, r.phone as number, a.city, a.street, a.nr from restaurants r, adres a, dish d where d.id_restaurant = r.id_restaurant and r.id_adres = a.id_adres and d.id_dish = ?
  41. `;
  42. con.query(sql, [id], (err, result) => {
  43. if (err) throw err;
  44. if (result.length == 0) {
  45. res.status(404);
  46. res.send("");
  47. return;
  48. }
  49. res.send(result[0]);
  50. });
  51. });
  52.  
  53. app.get("/dish/:id", (req, res) => {
  54. const id = req.params.id;
  55. const con = connectToDB();
  56. const sql = /*sql*/ `
  57. SELECT d.name, d.description, d.price , r.id_restaurant as rid, r.name as rname, a.city, a.street, a.nr, a.longitude, a.latitude, r.phone from dish d, restaurants r, adres a
  58. where r.id_restaurant = d.id_restaurant and r.id_adres = a.id_adres and d.id_dish = ?
  59. `;
  60. con.query(sql, [id], (err, result) => {
  61. if (err) throw err;
  62. if (result.length == 0) {
  63. con.end();
  64. res.status(404);
  65. res.send("");
  66. return;
  67. }
  68. res.header("Content-Type", "application/json");
  69. const dish = result[0];
  70. const sql = /*sql*/ `
  71. SELECT url from imagesdish where id_dish = ?
  72. `;
  73. con.query(sql, [id], (err, result) => {
  74. let retresult = [];
  75. for (const u of result) {
  76. retresult.push({
  77. name: dish.name,
  78. description: dish.description,
  79. price: dish.price,
  80. restaurant: {
  81. name: dish.rname,
  82. adres: {
  83. city: dish.city,
  84. street: dish.street,
  85. nr: dish.nr,
  86. longitude: dish.longitude,
  87. latitude: dish.latitude
  88. }
  89. },
  90. illustration: u.url
  91. });
  92. }
  93. const sql = /*sql*/ `
  94. SELECT url from imagesrestaurant where id_restaurant = ?
  95. `;
  96.  
  97. con.query(sql, [dish.rid], (err, result) => {
  98. for (const u of result) {
  99. retresult.push({
  100. name: dish.name,
  101. description: dish.description,
  102. price: dish.price,
  103. restaurant: {
  104. name: dish.rname,
  105. adres: {
  106. city: dish.city,
  107. street: dish.street,
  108. nr: dish.nr,
  109. longitude: dish.longitude,
  110. latitude: dish.latitude
  111. }
  112. },
  113. illustration: u.url
  114. });
  115. }
  116. res.send(retresult);
  117. });
  118. con.end();
  119. });
  120. });
  121. });
  122.  
  123. app.get("/dishes/:long/:lat", (req, res) => {
  124. const con = connectToDB();
  125. const sql = /*sql*/ `
  126. SELECT distinct d.id_dish as id, d.name as name, d.price as price, r.name as restaurantName, di.url as img FROM dish d, imagesdish di, restaurants r WHERE di.id_dish = d.id_dish and r.id_restaurant = d.id_restaurant
  127. `;
  128. res.header("Content-Type", "application/json");
  129. con.query(sql, (err, result) => {
  130. res.send(result);
  131. });
  132. con.end();
  133. });
  134.  
  135. const port = process.env["PORT"] || 5000;
  136. app.listen(port, err => {
  137. if (err) throw err;
  138. console.log(`listening on port ${port}...`);
  139. });
  140.  
  141.  
  142.  
  143.  
  144. //lets code
  145. //
  146. ////
  147. ////
  148. ////
  149. ////
  150. ////
  151. ////
  152. ////
  153. ////
  154. ////
  155. ////
  156. ////
  157. ////
  158. ////
  159. //
  160.  
  161.  
  162. const express = require("express");
  163. const mysql = require("mysql");
  164. const cookieParser = require("cookie-parser");
  165. const session = require("express-session");
  166. const md5 = require("md5");
  167. const Joi = require("joi");
  168. const app = express();
  169.  
  170. function connectToDB() {
  171. let conn = mysql.createConnection({
  172. host: "db4free.net",
  173. user: "geolotto",
  174. password: "geolotto",
  175. database: "geolotto"
  176. });
  177. conn.connect(err => {
  178. if (err) throw err;
  179. console.log("Conneceted to database");
  180. });
  181. return conn;
  182. }
  183.  
  184. app.use(express.json());
  185.  
  186. app.use((req, res, next) => {
  187. res.append("Access-Control-Allow-Origin", ["*"]);
  188. res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
  189. res.append("Access-Control-Allow-Headers", "Content-Type");
  190. next();
  191. });
  192.  
  193. app.use(cookieParser());
  194.  
  195. app.use(
  196. session({
  197. path: "/",
  198. httpOnly: true,
  199. secure: false,
  200. maxAge: null,
  201. secret: "cookie_secret",
  202. name: "cookie_name",
  203. resave: true,
  204. saveUninitialized: true
  205. })
  206. );
  207.  
  208.  
  209.  
  210.  
  211. app.post("/login", (req, res) => {
  212. let schema = Joi.object().keys({
  213. login: Joi.required(),
  214. password: Joi.required()
  215. });
  216. Joi.validate(req.body, schema);
  217. let { login, password } = req.body;
  218. password=md5(md5(password));
  219. let conn = connectToDB();
  220. let sql = /*sql*/ `
  221. SELECT login, password from users where login = ? and password = ?
  222. `;
  223. conn.query(sql, [login, password], (error, result) => {
  224. if (error) throw error;
  225. if (result.length > 0) {
  226. if (!req.session.login) req.session.login = req.body.login;
  227. if (!req.session.password)
  228. req.session.password = password;
  229. res.status(200).send("sthsth123");
  230. }
  231. else res.status(500).send("fail");
  232. });
  233. });
  234.  
  235.  
  236.  
  237. app.post("/register", (req, res) => {
  238.  
  239. req.header("Content-Type", "application/json");
  240. res.header("Content-Type", "application/json");
  241. console.log(req.body);
  242. let schema = Joi.object().keys({
  243. login: Joi.string()
  244. .min(3)
  245. .max(49)
  246. .required(),
  247. password: Joi.string()
  248. .min(8)
  249. .max(49)
  250. .required(),
  251. email: Joi.string().email().required(),
  252. name: Joi.string()
  253. .min(3)
  254. .max(49)
  255. .required(),
  256. surname: Joi.string()
  257. .min(3)
  258. .max(49)
  259. .required()
  260. });
  261.  
  262. Joi.validate(req.body, schema, (err, res) => {
  263. if (err) {
  264. res.status(500).send({text:"texthaha"});
  265. throw err;
  266. }
  267. });
  268.  
  269. const { login, email } = req.body;
  270.  
  271. let conn = connectToDB();
  272. let sql = /*sql*/ `
  273. SELECT login, email from users where login = ? or email = ?
  274. `;
  275. conn.query(sql, [login, email], (error, result) => {
  276. if (error) throw error;
  277. if (result.length > 0) {
  278. res.status(505).send("CHUJOWO");
  279. } else {
  280. req.body.password = md5(md5(req.body.password));
  281. let sql = /*sql*/ `
  282. INSERT INTO users SET ?
  283. `;
  284. conn.query(sql, req.body, (err, result) => {
  285. if (err) throw err;
  286. res.status(200).send("CHUJ");
  287. });
  288. }
  289. });
  290. });
  291.  
  292. const PORT = process.env.PORT || 5000;
  293. app.listen(PORT, err => {
  294. if (err) throw err;
  295. console.log(`listening on port ${PORT}`);
  296. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement