/**
* @fileOverview A service that handles CRUD operations for user's descriptions
*
* @author Ralfs Lagzda
*
* @requires NPM:lodash
* @requires ../models/description
*/
const _ = require('lodash');
const Description = require('../models/description');
/**
* List all descriptions
*
* @memberOf DescriptionService
* @function list
* @param {Object} params the parameters to limit the query to desired results
* @returns {Promise<Description[]>} Promise that eventually resolves an array
* of descriptions or throws error
*/
const list = params => new Promise(async (resolve, reject) => {
try {
if (typeof (params) !== 'object' || _.isEmpty(params)) throw (Error('Invalid parameters'));
resolve(await Description.find(params));
} catch (err) {
reject(err);
}
});
/**
* Create a description
*
* @memberOf DescriptionService
* @function create
* @param {String} userId the owner of the description
* @param {Object} description the description object to create
* @returns {Promise<Description>} Promise that eventually creates and resolves
* a description or throws error
*/
const create = (userId, description) => new Promise(async (resolve, reject) => {
try {
const creatableDescription = await Object.assign(description, { createdBy: userId });
resolve(await new Description(creatableDescription).save());
} catch (err) {
reject(err);
}
});
/**
* Retrieves a single description by ID
*
* @memberOf DescriptionService
* @function read
* @param {String} descriptionId the ID of the description
* @returns {Promise<Description>} Promise that eventually resolves a description or throws error
*/
const read = descriptionId => new Promise(async (resolve, reject) => {
try {
resolve(await Description.findById(descriptionId));
} catch (err) {
reject(err);
}
});
/**
* Updates a single description
*
* @memberOf DescriptionService
* @function update
* @param {String} descriptionId the ID of the description
* @param {Object} body key/value pairs of the fields that should be changed
* @returns {Promise<Description>} Promise that eventually updates and
* resolves a description or throws error
*/
const update = (descriptionId, body) => new Promise(async (resolve, reject) => {
try {
const updatedDescription = await Object.assign(await Description.findById(descriptionId), body);
resolve(await updatedDescription.save());
} catch (err) {
reject(err);
}
});
/**
* Deletes a single description
*
* @memberOf DescriptionService
* @function del
* @param {String} descriptionId the ID of the description
* @returns {Promise} Promise that eventually deletes
* a description or throws error
*/
const del = descriptionId => new Promise(async (resolve, reject) => {
try {
await Description.remove({ _id: descriptionId });
resolve();
} catch (err) {
reject(err);
}
});
/**
* Find or Create a description
*
* @memberOf DescriptionService
* @function findOrCreate
* @param {String} userId the owner of the description
* @param {Object} description the description object to create
* @param {String} description.description the description string in the object
* @returns {Promise<Description>} eventually creates and returns a
* description or throws error
*/
const findOrCreate = (userId, description) => new Promise(async (resolve, reject) => {
try {
if (!description) return resolve('');
const existingDescription = await Description.findOne({ description });
if (!existingDescription) {
const creatableDescription = await Object.assign({ description }, { createdBy: userId });
return resolve(await new Description(creatableDescription).save());
}
return resolve(existingDescription);
} catch (err) {
return reject(err);
}
});
/**
* A service that handles CRUD operations for user's descriptions
* @namespace DescriptionService
*/
module.exports = {
list,
create,
read,
update,
del,
findOrCreate,
};