Kaynağa Gözat

some cleaning

Ivan Asmer 5 yıl önce
ebeveyn
işleme
6420454204
1 değiştirilmiş dosya ile 9 ekleme ve 71 silme
  1. 9 71
      index.js

+ 9 - 71
index.js

@@ -16,18 +16,6 @@ var sequelize = new Sequelize('test', '', '',
 var Post = sequelize.define('post', {
     title: Sequelize.STRING,
     text:  Sequelize.TEXT,
-    key:  {type: Sequelize.STRING, 
-            unique: true,
-            get: function(){
-                var currentValue = this.getDataValue('key')
-                if (!currentValue){
-                    var shajs = require('sha.js')
-                    this.setDataValue('key',shajs('sha256').update(`${Math.random}${(new Date()).toString()}${this.text}${this.title}`).digest('hex'))
-                }
-                return this.getDataValue('key')
-            },
-            set: function(){
-            }}
 },{
  getterMethods: {
     tagz() {
@@ -39,14 +27,6 @@ var Post = sequelize.define('post', {
   },
 })
 
-Post.beforeCreate(function(model, options) {
-    return new Promise ((resolve, reject) => {
-        var shajs = require('sha.js')
-        model.key = shajs('sha256').update(`${Math.random}${(new Date()).toString()}${this.text}${this.title}`).digest('hex')
-        resolve(model, options)
-    });
-})
-
 var Comment = sequelize.define('comment',{
     text: Sequelize.TEXT
 })
@@ -69,6 +49,9 @@ Post.belongsToMany(Tag, {through: 'PostTag'})
 
 async function fillDB(){
     await sequelize.sync()
+    if (await Post.count()){
+        return;
+    }
     var post1 = await Post.create( {
                             title: 'First Post',
                             text: 'First Post BLah-blah-blah'
@@ -113,45 +96,8 @@ async function fillDB(){
 
 }
 
-//fillDB()
-//
-//
-async function getData(){
-    var tag1 = await Tag.findOne({
-        where: {
-            title: 'tag1'
-        },
-        //include: [
-            //{model: Post,
-                //limit: 1}
-        //]
-    })
-
-    console.log(await tag1.getPosts({limit: 1, offset: 1}))
+fillDB()
 
-    //for (let post of await tag1.getPosts()){
-        //console.log('TAG1', post.title)
-    //}
-
-    //var comment1 = await Comment.findOne({
-        //where: {
-            //id: 1
-        //}
-    //})
-
-    //for (let comment of await comment1.getComments()){
-        //console.log('comment1 sub comment:', comment.text)
-        //console.log('parent', (await comment.getComment()).text)
-    //}
-
-    //var post1 =(await comment1.getPost());
-
-    //console.log(`${post1.text} at ${post1.createdAt}`)
-    //console.log(await post1.tagz)
-    //console.log(post1.age)
-}
-
-getData()
 
 var cookieSession = require('cookie-session')
 var express = require('express')
@@ -166,14 +112,14 @@ var { buildSchema } = require('graphql');
 //
 var schema = buildSchema(`
     type Query {
-        post(id: String!): Post
+        post(id: Int!): Post
         comments(id: Int!): [Comment]
         subComments(id: Int!): [Comment]
         posts: [Post]
     }
     type Mutation {
         createPost(title: String!, text: String!): Post
-        createComment(postID: Int!, text: String!): Post
+        createComment(postID: Int!,  text: String!, commentID: Int): Comment
     }
 
     type Post {
@@ -184,7 +130,6 @@ var schema = buildSchema(`
         tagz:  [String]
         comments: [Comment]
         timestamp: Int
-        key: String
     }
     type Comment {
         id: Int
@@ -198,18 +143,11 @@ async function getPost({id}, {session}){
     console.log(session)
     session.lastId = id;
     console.log(session)
-    //return Post.findById(id).then( post => (post.comments = post.getComments(), post) )
     let post = await Post.findOne({
         where: {
              id
         }
     })
-    console.log(post.age)
-    //if (post){
-        //post.comments = await post.getComments()
-        //post.timestamp = post.createdAt.getTime()/1000
-    //}
-    //console.log(post.createdAt, typeof post.createdAt, post.createdAt.getTime())
     return post;
 }
 
@@ -241,9 +179,9 @@ async function createPost({title, text}){
     return Post.create({title, text})
 }
 
-async function createComment({postID, text}){
+async function createComment({postID, text, commentID}){
     let post    = await Post.findById(postID)
-    let comment = await Comment.create({text})
+    let comment = await Comment.create({text, commentId: commentID})
     post.addComment(comment)
     return comment
 }
@@ -282,7 +220,7 @@ app.use('/graphql', express_graphql(req => ({
     context: (console.log(req.session),req.session.counter = (req.session.counter || 0) +1, ({session: req.session}))
 })));
 
-app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
+app.listen(4000, '0.0.0.0', () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));