Ivan Asmer 5 роки тому
батько
коміт
1db4ff4463
2 змінених файлів з 69 додано та 88 видалено
  1. 53 47
      index.js
  2. 16 41
      models.js

+ 53 - 47
index.js

@@ -39,8 +39,7 @@ const upload  = require('multer')({ dest: uploadPath })
             url: String,
             originalFileName: String,
             userAvatar: User,
-            good: Good
-            category: Category
+            order: Order
             owner: User
         }
 
@@ -48,68 +47,75 @@ const upload  = require('multer')({ dest: uploadPath })
             _id: ID,
             text: String,
             userAvatar: UserInput,
-            good: GoodInput
-            category: CategoryInput
-        }
-
-        type Category {
-            _id: ID,
-            name: String!,
-            goods: [Good]
-            image: Image
+            order: OrderInput
         }
 
-        input CategoryInput {
-            _id: ID,
-            name: String!,
-            goods: [ID]
-            image: ImageInput
-        }
 
-        type Good {
-            _id: ID,
-            name: String!,
-            description: String
-            price: Float
-            orderGoods: [OrderGood]
-            categories: [Category]
+        type Order {
+            _id: ID
+            executor: User
+            messages: [Message]
+            comments: [Comment]
             images: [Image]
+            link: String,
+            name: String,
+            price: Float,
+            details: String,
+            quantity: Int,
+            delivery_counter: Int,
+            address_from: String
+            address_to: String
+            time_of_delivery: String
+            owner: User
         }
 
-        input GoodInput {
-            _id: ID,
-            name: String!,
-            description: String
-            price: Float
-            categories: [CategoryInput]
-            images: [ImageInput]
+        input OrderInput {
+            _id: ID
+            executor: User
+            messages: [MessageInput]
+            comments: [CommentInput]
+            images: [Image]
+            link: String,
+            name: String,
+            price: Float,
+            details: String,
+            quantity: Int,
+            delivery_counter: Int,
+            address_from: String
+            address_to: String
+            time_of_delivery: String
         }
 
-        type OrderGood {
-            _id: ID,
-            price: Float,
-            count: Float,
-            good: Good,
+        type Message {
+            _id: ID
+            owner: User
             order: Order
+            title: String,
+            text: String,
         }
 
-        input OrderGoodInput {
-            _id: ID,
-            count: Int!,
-            good: [GoodInput],
-            order: [OrderInput]
+        input MessageInput {
+            _id: ID
+            order: OrderInput
+            title: String,
+            text: String,
         }
 
-        type Order {
+        type Comment {
             _id: ID
-            total: Float
-            orderGoods: [OrderGood]
+            owner: User
+            order: Order
+            title: String,
+            text: String,
         }
 
-        input OrderInput {
+        input CommentInput {
             _id: ID
-            orderGoods: [OrderGoodInput]
+            order: OrderInput
+            title: String,
+            text: String,
         }
+
     `);
 
     schema = expand(schema)
@@ -138,7 +144,7 @@ const upload  = require('multer')({ dest: uploadPath })
     app.use(express.static('public'));
 
 
-    let socketPath = "/home/asmer/node_hosts/shop"
+    let socketPath = "/home/asmer/node_hosts/amazon2home"
     app.listen(socketPath, () => {
         console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
         fs.chmodSync(socketPath, '777');

+ 16 - 41
models.js

@@ -1,7 +1,7 @@
 const ObjectID    = require("mongodb").ObjectID;
 const {connect}   = require('mm')
 
-module.exports = async (dbName='shop') => {
+module.exports = async (dbName='amazon2home') => {
     const {Savable, slice} = await connect(dbName)
 
     async function getModels({id}){
@@ -21,6 +21,7 @@ module.exports = async (dbName='shop') => {
             static get relations(){ //don't needed due to ___owner in most cases
                 return {
                     avatar : "userAvatar",
+                    executedOrders: "executor"
                 }
             }
         }
@@ -63,82 +64,56 @@ module.exports = async (dbName='shop') => {
             static get relations(){
                 return {
                     userAvatar: "avatar", //if it is ava
-                    good: ["images"], //if it is ava
-                    category: "image", //if it is ava
+                    order: ["images"] 
                 }
             }
 
         }
         SlicedSavable.addClass(Image)
 
-        class Good extends SlicedSavable {
+        class Order extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
             static get relations(){
                 return {
-                    categories: ["goods"],
-                    orderGoods: "good",
-                    images: "good"
+                    executor: ["executedOrders"]
+                    messages: "order"
+                    comments: "order"
+                    images: "order"
                 }
             }
-
-            static get guestRelations(){
-                return ["categories", "orderGoods"]
-            }
         }
-        SlicedSavable.addClass(Good)
+        SlicedSavable.addClass(Order)
 
-        class Category extends SlicedSavable {
+        class Message extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
             static get relations(){
                 return {
-                    goods: ["categories"],
-                    image: "category",
+                    order: ["messages"]
                 }
             }
         }
-        SlicedSavable.addClass(Category)
-
-        class Order extends SlicedSavable {
-            constructor(...params){
-                super(...params)
-            }
-
-            get total(){
-                return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))()
-            }
+        SlicedSavable.addClass(Message)
 
-            static get relations(){
-                return {
-                    user: ["orders"],
-                    orderGoods: "order"
-                }
-            }
-        }
-        SlicedSavable.addClass(Order)
 
-        class OrderGood extends SlicedSavable {
+        class Comment extends OwnerSlicedSavable {
             constructor(...params){
                 super(...params)
             }
 
-            get total(){
-                return this.price*this.count
-            }
-
             static get relations(){
                 return {
-                    good: ["orderGoods"],
-                    order: ["orderGoods"]
+                    order: ["comments"]
                 }
             }
         }
-        SlicedSavable.addClass(OrderGood)
+        SlicedSavable.addClass(Comment)
+