import pool from './db';
import dateformat from 'dateformat';

export default class {
    constructor(post){
        this.title = post.title;
        this.content = post.content;
        this.author = post.author;
        this.createDate= new Date()
    }

    save(){

        let post = {
            title: this.title,
            content: this.content,
            author: this.autor,
            createDate: this.createDate,
            updateDate: new Date()
        }
        console.log('插入文章', this.title);
        return new Promise((resolve, reject) => {
            pool.getConnection((err, connection) =>{
                connection.query('INSERT INTO posts SET ?', post, (err, result) => {
                    if(err){
                        reject(err);
                    }
                    resolve(result);
                    connection.release();
            });
        })
        });
        
        
    }

    static getPostByTag(tag) {

        let contentTags =`
            SELECT GROUP_CONCAT(distinct tagname) AS tags,title, content, createDate ,author, posts.id
            FROM tags
            LEFT JOIN posttag
            ON tags.tagid = posttag.tagid
            RIGHT JOIN posts
            ON posttag.postid = posts.id
            GROUP BY posts.id
            WHERE tagname = '${tag}'
            LIMIT 0,6
        `;
        return new Promise( (resolve, reject) => {
            pool.getConnection((err, connection) => {
                connection.query(contentTags, (err, result) => {
                    if(err){
                        reject(err);
                    }
                    console.log(result)

                    resolve(result.map((post) => {
                        post.createDate = dateformat(post.createDate, 'yyyy年mm月dd日 HH:MM:ss');
                        post.tags = post.tags ? post.tags.split(',') : [];
                        return post;
                    }));
                    connection.release();
                });
            })
            
        });
    }

    static getTags() {
        let sql = `
            SELECT tagName,tags.tagId
            FROM tags
            left JOIN postTag
            ON tags.tagId = postTag.tagId
            GROUP BY tags.tagId
        `;
        return new Promise( (resolve, reject) => {
            pool.getConnection((err, connection) => {
                connection.query(sql, (err, result) =>{
                if(err){
                        reject(err);
                    }
                    resolve(result);
                    connection.release();
                });
            });
        });
    }

    
}
@@ -95,20 +95,19 @@
 tructor(
-post
+tag
 )%7B%0A     
@@ -119,359 +119,48 @@
 is.t
-itle = post.title;%0A        this.content = post.content;%0A        this.author = post.author;%0A        this.createDate= new Date()%0A    %7D%0A%0A    save()%7B%0A%0A        let post = %7B%0A            title: this.title,%0A            content: this.content,%0A            author: this.autor,%0A            createDate: this.createDate,%0A            updateDate: new Date()%0A        %7D
+agName = tag.tagName;%0A    %7D%0A%0A    save()%7B
 %0A   
@@ -181,12 +181,13 @@
 og('
-%E6%8F%92%E5%85%A5%E6%96%87%E7%AB%A0
+%E6%96%B0%E5%BB%BAtag
 ', t
@@ -191,19 +191,21 @@
 , this.t
-itl
+agNam
 e);%0A    
@@ -349,20 +349,19 @@
 RT INTO 
-post
+tag
 s SET ?'
@@ -362,20 +362,19 @@
 SET ?', 
-post
+tag
 , (err, 
@@ -557,36 +557,44 @@
 ();%0A            
-%7D);%0A
+    %7D);%0A    
         %7D)%0A     
@@ -604,26 +604,8 @@
 %7D);%0A
-        %0A        %0A
     
@@ -1025,32 +1025,64 @@
  0,6%0A        %60;%0A
+        console.log('tag', tag)%0A
         return n

修改 tag