Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3. var bodyParser = require('body-parser');
  4. var multer = require('multer');
  5. var upload = multer();
  6. var fs = require("fs");
  7. const mysql = require('mysql');
  8. const dauria = require('dauria');
  9.  
  10. const con = mysql.createConnection({
  11.     host : "localhost",
  12.     port : "3306",
  13.     user : "root",
  14.     password : "irena",
  15.     database : "apitest"
  16. });
  17.  
  18. con.connect(function(err){
  19.     if(err) throw err;
  20.     console.log("Connected!");
  21. });
  22.  
  23. app.use(bodyParser.json());
  24. app.use(bodyParser.urlencoded({ extended: true }));
  25.  
  26. app.get('/api/post',function(req,res){
  27.    
  28.     con.query("SELECT post_id,title,content,create_on,file_id,path,categories FROM post,image,categories WHERE post.post_id=image.file_id AND post.c=categories.id",function(err,results,fields){
  29.         if(err) throw err;
  30.  
  31.         console.log(results);
  32.    
  33.         res.send(results);
  34.    
  35.     });
  36.  
  37. }); //-OK-
  38.  
  39. app.post('/api/post',upload.array(),function(req,res,next){
  40.  
  41.     const title = req.body.title;
  42.     const content = req.body.content;
  43.     const Categories = "\"" + req.body.categories + "\"";
  44.     const uri = req.body.uri;
  45.     const time = Math.floor(new Date().valueOf() / 1000);
  46.     var Cid;
  47.  
  48.     con.query("SELECT MAX(post_id) AS id FROM post",function(err,results,fields){
  49.         if(err) throw err;
  50.  
  51.         var id = results[0].id + 1;
  52.         console.log(id);
  53.         con.query("SELECT id FROM categories WHERE categories = ?",Categories,function(err1,results1,fields1){
  54.             if (err1) throw err1;
  55.             if(!results){
  56.                
  57.                 con.query("SELECT MAX(id) AS Cid FROM categories",function(err2,results2,fields2){
  58.                     if(err2) throw err2;
  59.                     Cid = results2[0].Cid + 1;
  60.                     const paramC = [Cid,Categories];
  61.  
  62.                     con.query("INSERT INTO categories (id,categories) VALUES (?,?)",paramC,function(err3,results3,fields3){
  63.                         if(err3) throw err3;
  64.                         else console.log("categories insert OK!");
  65.                     });
  66.                
  67.                 });
  68.  
  69.             }
  70.             else {
  71.                 Cid = results1;
  72.                 console.log("categories 已經存在!");
  73.             }
  74.            
  75.             const paramP = [id,title,content,time,Cid];
  76.            
  77.             con.query("INSERT INTO post (post_id,title,content,create_on,c) VALUES (?,?,?,?,?)",paramP,function(err1,results1,fields1){
  78.                 if(err1) throw err1;
  79.                 else console.log("post insert OK!");
  80.             });
  81.        
  82.         });
  83.  
  84.        
  85.         if(uri){
  86.             const path = "image" + id +".png";
  87.             fs.writeFile(path,dauria.parseDataURI(uri).buffer,function(err1){
  88.                 if(err1) throw err1;
  89.                 const path1 = "\"" +path + "\"" ;
  90.                 const uri1 =  "\"" + uri + "\"" ;
  91.                 const paramI = [id,uri1,path1];
  92.                 con.query("INSERT INTO image (file_id,uri,path) VALUES (?,?,?)",paramI,function(err2,results1,fields1){
  93.                     if(err2) throw err2;
  94.                     else console.log("image insert OK!");
  95.                 });
  96.             });
  97.         }
  98.         else {
  99.             console.log("No image!");
  100.             con.query("INSERT INTO image (file_id) VALUES (?)",id,function(err2,results1,fields1){
  101.                 if(err2) throw err2;
  102.                 else console.log("image insert OK!(only id)");
  103.             });
  104.         }
  105.  
  106.     });
  107.    
  108. });
  109.  
  110. app.get('/api/post/:id',function(req,res){
  111.  
  112.     const sql = "SELECT post_id,title,content,create_on,file_id,path,categories FROM post,image,categories WHERE post.post_id = image.file_id AND image.file_id = ? AND post.c = Categories.id";
  113.  
  114.     con.query(sql,req.params.id,function(err,results,fields){
  115.         if(err) throw err;
  116.  
  117.         console.log(results);
  118.         res.send(results);
  119.     });
  120.  
  121. });//-OK-
  122.  
  123. app.patch('/api/post/:id',function(req,res){
  124.    
  125.     const id = req.params.id;
  126.     const title = req.body.title;
  127.     const content = req.body.content;
  128.     const Categories = req.body.categories;
  129.     const uri = req.body.uri;
  130.  
  131.     if (title) {
  132.         const sql = "UPDATE post SET title = \""+title+"\" WHERE post_id = " + id;
  133.         con.query(sql,function(err,results,fields){
  134.             if(err) throw err;
  135.             console.log("title 更改OK!");
  136.         });
  137.     } else {
  138.         console.log("title 沒有變動!");
  139.     }
  140.    
  141.     if (content) {
  142.         const sql = "UPDATE post SET content = \""+content+"\" WHERE post_id = " + id;
  143.         con.query(sql,function(err,results,fields){
  144.             if(err) throw err;
  145.             console.log("content 更改OK!");
  146.         });
  147.     } else {
  148.         console.log("content 沒有變動!");
  149.     }
  150.    
  151.     if (Categories) {
  152.         const sql = "SELECT id FROM categories WHERE categories = \"" + Categories + "\"";
  153.         con.query(sql,function(err,results,fields){
  154.             if(err) throw err;
  155.             if(results){
  156.                 console.log(results);
  157.                 console.log(results[0].id);
  158.                 console.log(typeof results[0].id);
  159.                 con.query("UPDATE post SET c = ? WHERE post_id = ?",results[0].id,id,function(err1,results1,fields1){
  160.                     if(err1) throw err1;
  161.                     console.log("Categories 更改OK!(Categories存在)");
  162.                 });
  163.            
  164.             }
  165.             else{
  166.                 con.query("SELECT MAX(id) FROM categories",function(err1,results1,fields1){
  167.  
  168.                     const Cid = results1 +1;
  169.                     con.query("INSERT INTO categories (id,categories) VALUES (?,?)",Cid,Categories,function(err2,results2,fields2){
  170.                         if(err) throw err;
  171.                         console.log("Categories 新增OK!");
  172.                     });
  173.  
  174.                     con.query("UPDATE post SET c = ? WHERE id = ?",Cid,id,function(err2,results2,fields2){
  175.                         if(err) throw err;
  176.                         console.log("Categories 更改OK!");
  177.                     });
  178.                 });
  179.             }            
  180.         });
  181.     } else {
  182.         console.log("categories 沒有變動!");
  183.     }
  184.    
  185.     if (uri) {
  186.         const path = "image" + id +".png";
  187.         fs.writeFile(path,dauria.parseDataURI(uri).buffer,function(err1){
  188.             if(err1) throw err1;
  189.             const sql = "UPDATE image SET uri = \""+uri+"\",path = \""+path+"\" WHERE file_id = " + id;
  190.             con.query(sql,function(err2,results2,fields2){
  191.                 if(err2) throw err2;
  192.                 else console.log("image 更改OK!");
  193.             });
  194.         });
  195.     } else {
  196.         console.log("image 沒有變動!");
  197.     }
  198.  
  199.     const sql = "SELECT post_id,title,content,create_on,file_id,path,categories FROM post,image,categories WHERE post.post_id = image.file_id AND image.file_id = ? AND post.c = Categories.id";
  200.  
  201.     con.query(sql,req.params.id,function(err,results,fields){
  202.         if(err) throw err;
  203.  
  204.         console.log(results);
  205.         res.send(results);
  206.     });
  207.    
  208. }); //-少categories-
  209.  
  210. app.delete('/api/post/:id',function(req,res){
  211.     const id = req.params.id;
  212.     con.query("DELETE FROM post WHERE post_id =?",id,function(err,results,fields){
  213.         if(err) throw err;
  214.         console.log("delete post!");
  215.     });
  216.     con.query("DELETE FROM image WHERE file_id =?",id,function(err,results,fields){
  217.         if(err) throw err;
  218.         console.log("delete image!");
  219.     });
  220. });//-OK-
  221. app.get('/api/page/post',function(req,res){
  222.     con.query("SELECT post_id,title,content,create_on,uri,categories FROM post,image,categories WHERE post.post_id=image.file_id AND post.c=categories.id",function(err,results,fields){
  223.         if(err) throw err;
  224.  
  225.         console.log(results);
  226.    
  227.         res.send(results);
  228.    
  229.     });
  230. });//-OK-
  231. app.get('/api/page/post/:id',function(req,res){
  232.    
  233.     const sql = "SELECT post_id,title,content,create_on,uri,categories FROM post,image,categories WHERE post.post_id = image.file_id AND image.file_id = ? AND post.c = Categories.id";
  234.  
  235.     con.query(sql,req.params.id,function(err,results,fields){
  236.         if(err) throw err;
  237.  
  238.         console.log(results);
  239.         res.send(results);
  240.     });
  241.  
  242. });//-OK-
  243. app.get('/api/post/categories',function(req,res){
  244.     con.query("SELECT * FROM categories",function(err,results,fields) {
  245.         if(err) throw err;
  246.  
  247.         console.log(results);
  248.        
  249.         res.send(results);
  250.    
  251.     });
  252. });
  253.  
  254. const server = app.listen(3000, function () {
  255.     console.log("connected localhost:3000");
  256. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement