Просмотр исходного кода

Add model, controller and first endpoint for products

Tati 9 лет назад
Родитель
Сommit
fbda3565e6

+ 9 - 0
back/api/products/controller.js

@@ -0,0 +1,9 @@
+'use strinct';
+
+var Product = require('./model');
+
+module.exports.all = function (req, res, next) {
+    Product.find({}, res.languageProjection).then(function (ps) {
+        res.send(ps);
+    })
+}

+ 3 - 3
back/api/products/index.js

@@ -1,10 +1,10 @@
 'use strict';
 
 var express = require('express');
-var controller = require('./product.controller');
 var router = express.Router();
+var language = require('./../../components/middlewares/language');
+var controller = require('./controller');
 
-router.get('/all', function(req, res){console.log('aaaaa'); res.send('fdsafdsa')}, controller.all);
-
+router.get('/all', language, controller.all);
 
 module.exports = router;

+ 27 - 0
back/api/products/model.js

@@ -0,0 +1,27 @@
+'use strict';
+
+var mongoose = require('mongoose');
+var Schema = mongoose.Schema;
+
+var informationSchema = {
+    text: {
+        shortDesc: { type: String },
+        fullDesc: { type: String },
+        application: { type: String },
+        actives: [ String ],
+        ph: { type: String },
+    },
+    category: {
+        _id: Schema.ObjectId,
+        name: { type: String }
+    }
+};
+
+module.exports = mongoose.model('Product', mongoose.Schema({
+    name: { type: String, required: true },
+    imgUrl: { type: String },
+    thumbUrl: { type: String },
+    size: [ String ],
+    ES: informationSchema,
+    EN: informationSchema
+}))

+ 0 - 6
back/api/products/product.controller.js

@@ -1,6 +0,0 @@
-'use strinct';
-
-exports.all = function (req, res, next) {
-    console.log('aaaaaaaaaallll')
-    res.send('PRODUCTS');
-}

+ 3 - 0
back/app.js

@@ -6,6 +6,9 @@
 
 var express = require('express');
 var app = express();
+var mongoose = require('mongoose');
+
+mongoose.connect('mongodb://localhost/eba');
 
 require('./routes')(app); 
 

+ 8 - 0
back/components/middlewares/language.js

@@ -0,0 +1,8 @@
+'use strinct';
+var R = require('ramda');
+
+module.exports = function (req, res, next) {
+    var lang = req.headers.lang;
+    res.languageProjection = R.objOf(lang, 1);
+    next();
+}

+ 0 - 1
back/routes.js

@@ -5,6 +5,5 @@
 'use strict';
 
 module.exports = function (app) {
-    console.log('routes')
     app.use('/products', require('./api/products'));
 }