2017-11-15 16:59:12 +01:00
|
|
|
const _ = require('lodash');
|
2019-03-13 19:27:18 +01:00
|
|
|
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
2018-10-10 00:36:29 +02:00
|
|
|
|
2017-11-15 16:59:12 +01:00
|
|
|
module.exports = {
|
2019-03-13 19:27:18 +01:00
|
|
|
find: async function(params, populate) {
|
2019-02-02 13:07:18 +01:00
|
|
|
const model = this;
|
2018-01-23 15:38:43 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
const filters = convertRestQueryParams(params);
|
|
|
|
|
|
|
|
return this.query(buildQuery({ model, filters }))
|
|
|
|
.fetchAll({
|
|
|
|
withRelated: populate || this.associations.map(x => x.alias),
|
|
|
|
})
|
|
|
|
.then(data => data.toJSON());
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
count: async function(params = {}) {
|
2019-02-02 13:07:18 +01:00
|
|
|
const model = this;
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
const { where } = convertRestQueryParams(params);
|
|
|
|
|
|
|
|
return this.query(buildQuery({ model, filters: { where } })).count();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
findOne: async function(params, populate) {
|
2018-05-09 16:57:16 +02:00
|
|
|
const primaryKey = params[this.primaryKey] || params._id;
|
2018-01-23 18:54:17 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
params = {
|
2019-03-13 19:27:18 +01:00
|
|
|
[this.primaryKey]: primaryKey,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2017-11-28 15:32:25 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
const record = await this.forge(params).fetch({
|
|
|
|
withRelated: populate || this.associations.map(x => x.alias),
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
|
|
|
|
return record ? record.toJSON() : record;
|
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
create: async function(params) {
|
|
|
|
return this.forge()
|
|
|
|
.save(
|
|
|
|
Object.keys(params).reduce((acc, current) => {
|
|
|
|
if (
|
|
|
|
_.get(this._attributes, [current, 'type']) ||
|
|
|
|
_.get(this._attributes, [current, 'model'])
|
|
|
|
) {
|
|
|
|
acc[current] = params[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
.catch(err => {
|
2018-01-23 18:54:17 +01:00
|
|
|
if (err.detail) {
|
|
|
|
const field = _.last(_.words(err.detail.split('=')[0]));
|
|
|
|
err = { message: `This ${field} is already taken`, field };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
update: async function(search, params = {}) {
|
2018-01-23 15:38:43 +01:00
|
|
|
if (_.isEmpty(params)) {
|
|
|
|
params = search;
|
2017-11-15 16:59:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:38:43 +01:00
|
|
|
const primaryKey = search[this.primaryKey] || search.id;
|
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
search = {
|
2019-03-13 19:27:18 +01:00
|
|
|
[this.primaryKey]: primaryKey,
|
2018-01-23 18:54:17 +01:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const entry = await module.exports.findOne.call(this, search);
|
|
|
|
|
|
|
|
search = {
|
2019-03-13 19:27:18 +01:00
|
|
|
[this.primaryKey]: entry[this.primaryKey] || entry.id,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2018-01-23 15:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.forge(search)
|
2018-01-23 18:54:17 +01:00
|
|
|
.save(params, {
|
2019-03-13 19:27:18 +01:00
|
|
|
patch: true,
|
2018-01-23 18:54:17 +01:00
|
|
|
})
|
2019-03-13 19:27:18 +01:00
|
|
|
.catch(err => {
|
2018-01-23 18:54:17 +01:00
|
|
|
const field = _.last(_.words(err.detail.split('=')[0]));
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
delete: async function(params) {
|
|
|
|
return await this.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id,
|
|
|
|
}).destroy();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
deleteMany: async function(params) {
|
|
|
|
return await this.query(qb => {
|
|
|
|
qb.whereIn(this.primaryKey, params[this.primaryKey] || params.id);
|
|
|
|
}).destroy();
|
2018-05-03 16:26:57 +02:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
search: async function(params) {
|
|
|
|
return this.query(function(qb) {
|
|
|
|
qb.where('username', 'LIKE', `%${params.id}%`).orWhere('email', 'LIKE', `%${params.id}%`);
|
|
|
|
}).fetchAll();
|
2017-11-30 12:27:04 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
addPermission: async function(params) {
|
|
|
|
return this.forge(params).save();
|
2018-01-23 15:38:43 +01:00
|
|
|
},
|
2017-11-30 12:27:04 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
removePermission: async function(params) {
|
|
|
|
const value = params[this.primaryKey]
|
|
|
|
? {
|
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id,
|
|
|
|
}
|
|
|
|
: params;
|
2018-05-09 15:11:55 +02:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return this.forge()
|
2018-05-09 15:11:55 +02:00
|
|
|
.where(value)
|
2018-01-23 15:38:43 +01:00
|
|
|
.destroy();
|
2019-03-13 19:27:18 +01:00
|
|
|
},
|
2017-11-15 16:59:12 +01:00
|
|
|
};
|