Created
November 25, 2021 11:36
-
-
Save acrolink/562f3af7d517e571af9d1264116ec9d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _ = require('lodash'); | |
var Storage = require('./storage.model'); | |
var ProcductLifeCycle = require('../product/product.model').ProductLifeCycle; | |
var async = require('async'); | |
// Get list of storages | |
exports.index = function (req, res) { | |
Storage.find(function (err, storages) { | |
if (err) { | |
return handleError(res, err); | |
} | |
var calls = []; | |
storages.forEach(function (s) { | |
calls.push(function (callback) { | |
ProcductLifeCycle.count({'storage.location': s._id}, function (err, c) { | |
if (err) { | |
callback(err); | |
} | |
s.count = c; | |
callback(null, s); | |
}); | |
}); | |
}); | |
async.parallel(calls, function (err, result) { | |
if (err) | |
return handleError(res, err); | |
// return res.json(200, result); | |
return res.status(200).json(result); | |
}); | |
}); | |
}; | |
// Get a single storage | |
exports.show = function (req, res) { | |
Storage.findById(req.params.id, function (err, storage) { | |
if (err) { | |
return handleError(res, err); | |
} | |
if (!storage) { | |
return res.send(404); | |
} | |
return res.json(storage); | |
}); | |
}; | |
// Creates a new storage in the DB. | |
exports.create = function (req, res) { | |
Storage.create(req.body, function (err, storage) { | |
if (err) { | |
return handleError(res, err); | |
} | |
return res.json(201, storage); | |
}); | |
}; | |
// Updates an existing storage in the DB. | |
exports.update = function (req, res) { | |
if (req.body._id) { | |
delete req.body._id; | |
} | |
Storage.findById(req.params.id, function (err, storage) { | |
if (err) { | |
return handleError(res, err); | |
} | |
if (!storage) { | |
return res.send(404); | |
} | |
var updated = _.merge(storage, req.body); | |
updated.save(function (err) { | |
if (err) { | |
return handleError(res, err); | |
} | |
return res.json(200, storage); | |
}); | |
}); | |
}; | |
// Deletes a storage from the DB. | |
exports.destroy = function (req, res) { | |
Storage.findById(req.params.id, function (err, storage) { | |
if (err) { | |
return handleError(res, err); | |
} | |
if (!storage) { | |
return res.send(404); | |
} | |
storage.remove(function (err) { | |
if (err) { | |
return handleError(res, err); | |
} | |
return res.send(204); | |
}); | |
}); | |
}; | |
function handleError(res, err) { | |
return res.send(500, err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment