Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. "use strict";
  2.  
  3. var express = require('express');
  4. var Web = require("seneca-web");
  5. var bodyParser = require('body-parser')
  6.  
  7. var plugin = require('./products_actions/products_actions');
  8.  
  9. module.exports = plugin;
  10.  
  11. var entities = require('seneca-entity')
  12. var seneca = require('seneca')();
  13.  
  14. seneca.use(plugin);
  15. seneca.use(entities);
  16.  
  17. seneca.use('mysql-store', {
  18. name : 'ecrm',
  19. host : 'localhost',
  20. user : 'root',
  21. password : 'ecrm',
  22. port : 3306
  23. })
  24.  
  25. seneca.ready(function(err) {
  26.  
  27.  
  28. var Routes = [ {
  29. pin : 'area:product,action:fetch,criteria:*',
  30.  
  31. prefix : '/products/fetch',
  32.  
  33. map : {
  34. byId : {
  35. GET : true,
  36. suffix : "/:id"
  37. }
  38. }
  39.  
  40. }, {
  41. pin : 'area:product,action:*',
  42.  
  43. prefix : '/products',
  44.  
  45. map : {
  46. fetch : {
  47. GET : true
  48. },
  49.  
  50. add : {
  51. GET : false,
  52. PUT : true
  53. }
  54. }
  55.  
  56. } ];
  57.  
  58. var app = express();
  59. app.use(bodyParser.json());
  60.  
  61. var config = {
  62. routes : Routes,
  63. adapter : require('seneca-web-adapter-express'),
  64. context : app,
  65. options : {
  66. parseBody : false
  67. }
  68. }
  69.  
  70. seneca.use(Web, config);
  71.  
  72. app.listen(3000);
  73. });
  74.  
  75. module.exports = function(options) {
  76. var seneca = this;
  77.  
  78. // ADD
  79. seneca.add({
  80. area : "product",
  81. action : "add"
  82. }, function(req, done) {
  83. var products = this.make$("prodotti");
  84.  
  85. var args = req.args.body;
  86.  
  87. console.log(args);
  88.  
  89. products.nome = args.nome;
  90. products.categoria = args.categoria;
  91. products.descrizione = args.descrizione;
  92. products.prezzo = args.prezzo;
  93.  
  94. products.save$(function(err, product) {
  95. done(err, products.data$(false));
  96. });
  97. });
  98.  
  99.  
  100.  
  101. // get by Id , PROBLEM!!!
  102. seneca.add({
  103. area : "product",
  104. action : "fetch",
  105. criteria : "byId"
  106. }, function(req, done) {
  107. console.log("HERE");
  108.  
  109. var id = req.args.params.id;
  110.  
  111. var product = this.make("prodotti");
  112. product.load$(id, done);
  113. });
  114.  
  115. // LIST ALL
  116. seneca.add({
  117. area : "product",
  118. action : "fetch"
  119. }, function(args, done) {
  120. var products = this.make("prodotti");
  121. products.list$({}, done);
  122. });
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement