123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- const express = require('express');
- const path = require('path');
- const CrudModule = require('klapi/base/CrudModule');
- const OneToOne = require('klapi/base/OneToOne');
- const RepositorySystem = require('klapi/systems/RepositorySystem');
- const RoutingSystem = require('klapi/systems/RoutingSystem');
- const Actions = require('klapi/systems/ActionSystem');
- var db = require("../../db.js");
- //const FilterService = require(;'')
- var knex = require('knex')(db);
- let CategoriesScheme = {
- fields: [
- {
- key: "name",
- type: "text"
- },
- {
- key: "price",
- type: "decimal"
- },
- {
- key: "SKU",
- type: "text"
- },
- {
- key: "SKUName",
- type: "string",
- keyLength: 50,
- unique: true
- }
- ]
- };
- class ProductsModule extends CrudModule {
- constructor(){
- super('/products', [1,0,1,1,0], 'Products', '');
- this.repository = RepositorySystem.create('Products', CategoriesScheme);
- this.setRepo(this.repository);
- this.skus = new OneToOne('SKU', 'Products');
- this.init();
-
- }
- init(){
- let routes = [
- {
- type:"custom",
- method:"get",
- endpoint:"/prods",
- customFn: (req,res) => {
- // console.log("Req came")
- let category = 'Κουρτίνες';
- this.selectSKU(category)
- .then((products) => {
- res.send(products)
- })
- .catch((e) => {
- console.log("errio form here")
- res.send(412);
- })
- }
- }
- ];
- RoutingSystem.loadRoutes(routes, this.router);
- }
-
- /*selectSKU(category){
- //select all products --> from SKUs
- // for each product==SKU i ll select all prodcuts/buyes base on SKU
- //return knex.select().from('SKU').where({category:"Κουρτίνες"}).limit(10);
- return this.repository.knex().select().from('sku')
- .then((sku) => {
- return Promise.all(sku.map((product) => {
- return knex("products").select('price').count('id as Shops').where({sku:product.sku}).groupBy('price').orderBy('Shops','desc')//
- .then((suggested_item) => {
- let price = suggested_item[0].price;
- let paravates = suggested_item.filter((item) => item.price < price )
- let number_of_paraviaseis = paravates.reduce((accumulator,item) => accumulator+item.Shops,0)
- product['suggested_price'] = price;
- product['paraviaseis'] = number_of_paraviaseis
- return product;
- })
- }))
- })
- //return knex('products').where('');
- }*/
- }
- module.exports = ProductsModule;
|