Skip to content

Instantly share code, notes, and snippets.

@Skyross
Last active August 29, 2015 14:22
Show Gist options
  • Save Skyross/23393fddf5a7de6cb258 to your computer and use it in GitHub Desktop.
Save Skyross/23393fddf5a7de6cb258 to your computer and use it in GitHub Desktop.
Sample coding of ImporterApp
'use strict';
var _ = require('lodash');
var Attribute = require('./attribute.model.js');
// Get list of mappings
exports.index = function (req, res) {
Attribute.find(req.query, function (err, mappings) {
if (err) {
return handleError(res, err);
}
return res.json(200, mappings);
});
};
// Get a single mapping
exports.show = function (req, res) {
Attribute.findById(req.params.id, function (err, mapping) {
if (err) {
return handleError(res, err);
}
if (!mapping) {
return res.send(404);
}
return res.json(mapping);
});
};
// Creates a new mapping in the DB.
exports.create = function (req, res) {
Attribute.create(req.body, function (err, mapping) {
if (err) {
return handleError(res, err);
}
return res.json(201, mapping);
});
};
// Updates an existing mapping in the DB.
exports.update = function (req, res) {
if (req.body._id) {
delete req.body._id;
}
if (req.body.__v) {
delete req.body.__v;
}
Attribute.findById(req.params.id, function (err, mapping) {
if (err) {
return handleError(res, err);
}
if (!mapping) {
return res.send(404);
}
var updated = _.merge(mapping, req.body);
updated.markModified('groups');
updated.groups = req.body.groups;
updated.save(function (err) {
if (err) {
return handleError(res, err);
}
return res.json(200, mapping);
});
});
};
// Deletes a mapping from the DB.
exports.destroy = function (req, res) {
Attribute.findById(req.params.id, function (err, mapping) {
if (err) {
return handleError(res, err);
}
if (!mapping) {
return res.send(404);
}
mapping.remove(function (err) {
if (err) {
return handleError(res, err);
}
return res.send(204);
});
});
};
function handleError(res, err) {
return res.send(500, err);
}
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Attribute = new Schema({
userId: {type: String, required: true},
category: {type: String, required: true},
label: {type: String, default: 'UNLABELED'},
factorPrices: {type: Number, min: 0.0001, default: 1.1},
groups: {
_id: false,
type: [{_id: false, items: Array, name: String}],
default: []
},
weight: {
_id: false,
attribute: {type: String, default: 'Package Weight'},
extractor: {type: String, default: '([\\d\\.]+)kg'}
},
urlKey: {
_id: false,
extractor: {type: String, default: 'item\/(.*?).html'}
},
categories: {type: [Number], default: [2]},
websites: {type: [Number], default: [1]},
visibility: {type: Number, default: 4},
taxClassId: {type: Number, default: 0},
status: {type: Number, default: 2}
});
// Creating indexes
Attribute.index({userId: 1});
Attribute.index({category: 1});
// Loading plugins
var modifyAt = require('../../../plugins/last_modification_at');
Attribute.plugin(modifyAt, {index: true});
module.exports = mongoose.model('Attribute', Attribute);
/**
* Broadcast updates to client when the model changes
*/
'use strict';
var Mapping = require('./attribute.model.js');
exports.register = function(socket) {
Mapping.schema.post('save', function (doc) {
onSave(socket, doc);
});
Mapping.schema.post('remove', function (doc) {
onRemove(socket, doc);
});
};
function onSave(socket, doc, cb) {
socket.emit('mapping:save', doc);
}
function onRemove(socket, doc, cb) {
socket.emit('mapping:remove', doc);
}
'use strict';
angular.module('importerApp')
.controller('MappingAttributesCtrl', function ($scope, $state, $stateParams, Category, Auth, MappingAttributes) {
$scope.isLoading = true;
$scope.alerts = [];
$scope.valuesLimit = 4;
$scope.mapping = {};
$scope.mappings = [];
$scope.productsCount = 0;
$scope.attributesCount = 0;
$scope.mappingHasChanges = false;
$scope.category = $stateParams.category;
if (!$stateParams.category) {
$state.transitionTo('product');
}
$scope.itemGroupingOriginal = [];
$scope.itemGrouping = [
{
heading: 'Must-have attributes',
className: 'danger',
rateMin: 3 / 4,
rateMax: Infinity,
items: []
},
{
heading: 'Well known',
className: 'warning',
rateMin: 2 / 4,
rateMax: 3 / 4,
items: []
},
{
heading: 'Have some',
className: 'success',
rateMin: 1 / 4,
rateMax: 2 / 4,
items: []
},
{
heading: 'Almost unique',
className: 'default',
rateMin: -Infinity,
rateMax: 1 / 4,
items: []
}
];
Category.getAttributes({
category: $scope.category
}).$promise.then(function (data) {
$scope.productsCount = data.productsCount;
$scope.attributesCount = data.attributesCount;
if ($scope.productsCount == 0 || $scope.attributesCount == 0) {
$scope.alerts.push({
type: 'danger',
msg: 'There is no products and/or attributes for this category!'
});
}
var rateMax = data.rateMax;
data.attributes.forEach(function (attribute) {
attribute.rate = Math.round(attribute.rate / rateMax * 100) / 100;
attribute.values = attribute.values.slice(0, $scope.valuesLimit);
$scope.addItemToGrouping(attribute);
});
MappingAttributes.query({
userId: Auth.getCurrentUser()._id,
category: $scope.category
}).$promise.then(function (data) {
var finishLoading = function () {
$scope.changeMapping(0);
$scope.isLoading = false;
$scope.$watch('mapping', function () {
$scope.mappingHasChanges = true;
});
$scope.mappingHasChanges = false;
};
$scope.mappings = data;
if ($scope.mappings.length === 0) {
$scope.createMapping(finishLoading);
}
else {
finishLoading();
}
}, function (err) {
$scope.alerts.push({type: 'danger', msg: err});
$scope.isLoading = false;
});
}, function (err) {
$scope.alerts.push({type: 'danger', msg: err});
$scope.isLoading = false;
});
$scope.addNewGroup = function (name, item) {
$scope.mapping.selected.groups.push({
name: name || 'UNNAMED_' + $scope.mapping.selected.groups.length,
items: item ? [item] : []
});
};
$scope.moveAttributeToNewGroup = function (groupingIndex, itemIndex) {
var item = $scope.itemGrouping[groupingIndex].items[itemIndex];
$scope.addNewGroup(item._id, item);
$scope.itemGrouping[groupingIndex].items.splice(itemIndex, 1);
};
$scope.renameGroup = function (idx, name) {
$scope.mapping.selected.groups[idx].name = name;
};
$scope.removeGroup = function (idx) {
$scope.mapping.selected.groups[idx].items.forEach(function (item) {
$scope.addItemToGrouping(item);
});
$scope.mapping.selected.groups.splice(idx, 1);
};
$scope.addItemToGrouping = function (item) {
$scope.itemGrouping.forEach(function (grouping, index, array) {
if (grouping.rateMin < item.rate && item.rate <= grouping.rateMax) {
array[index].items.push(item);
}
});
};
$scope.createMapping = function (fn) {
var newMapping = new MappingAttributes({
userId: Auth.getCurrentUser()._id,
category: $scope.category
});
newMapping.$save(function (data) {
$scope.mappings.push(data);
if (fn) {
fn();
}
$scope.mappingHasChanges = false;
});
};
$scope.changeMapping = function (idx) {
if ($scope.mapping.selected) {
$scope.mapping.selected.groups.forEach(function (group) {
group.items.map(function (item, index, array) {
$scope.addItemToGrouping(item);
array[index] = item._id;
});
});
}
$scope.mapping.selected = $scope.mappings[idx];
$scope.mapping.selected.groups.forEach(function (group) {
group.items.forEach(function (item, index, array) {
var founded = false;
$scope.itemGrouping.forEach(function (itemGroup) {
for (var i = itemGroup.items.length - 1; i >= 0; i--) {
if (itemGroup.items[i]._id === item) {
array[index] = itemGroup.items[i];
itemGroup.items.splice(i, 1);
founded = true;
break;
}
}
});
for (var k = 0; k < $scope.itemGrouping.length; k++) {
if ($scope.itemGrouping[k].items.length > 0) {
$scope.itemGrouping[k].isOpened = true;
break;
}
}
if (!founded) {
array[index] = null;
}
});
group.items = group.items.filter(function (n) {
return n != undefined
});
});
$scope.mappingHasChanges = false;
};
$scope.resetMapping = function () {
$scope.mapping.selected.groups.forEach(function (group, index) {
$scope.removeGroup(index);
});
};
$scope.saveMapping = function (nextStep) {
if (!$scope.mappingHasChanges) return;
var copy = new MappingAttributes();
angular.copy($scope.mapping.selected, copy);
copy.groups.forEach(function (group) {
group.items.map(function (item, index, array) {
array[index] = item._id;
});
});
copy.$update(function () {
$scope.mappingHasChanges = false;
if (nextStep) {
$state.go('mappingValues', {
attributesMappingId: $scope.mapping.selected._id
});
}
else {
$scope.alerts.push({type: 'success', msg: 'This mapping successfully saved!'});
}
}, function (err) {
$scope.alerts.push({type: 'danger', msg: 'Error while saving mapping\'s changes!', jsonError: err});
});
};
$scope.closeAlert = function (index) {
$scope.alerts.splice(index, 1);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment