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
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
module.exports = ({ model }) => ({
|
|
|
|
find(params, populate) {
|
2019-03-13 19:27:18 +01:00
|
|
|
const filters = convertRestQueryParams(params);
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
return model
|
|
|
|
.query(buildQuery({ model, filters }))
|
2019-03-13 19:27:18 +01:00
|
|
|
.fetchAll({
|
2019-04-11 16:19:15 +02:00
|
|
|
withRelated: populate || model.associations.map(x => x.alias),
|
2019-03-13 19:27:18 +01:00
|
|
|
})
|
|
|
|
.then(data => data.toJSON());
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
count(params = {}) {
|
2019-03-13 19:27:18 +01:00
|
|
|
const { where } = convertRestQueryParams(params);
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
return model.query(buildQuery({ model, filters: { where } })).count();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
async findOne(params, populate) {
|
|
|
|
const primaryKey = params[model.primaryKey] || params.id;
|
2018-01-23 18:54:17 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
params = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: primaryKey,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2017-11-28 15:32:25 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
const record = await model.forge(params).fetch({
|
|
|
|
withRelated: populate || model.associations.map(x => x.alias),
|
2019-03-13 19:27:18 +01:00
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
|
|
|
|
return record ? record.toJSON() : record;
|
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
async create(params) {
|
|
|
|
return model
|
|
|
|
.forge()
|
2019-03-13 19:27:18 +01:00
|
|
|
.save(
|
|
|
|
Object.keys(params).reduce((acc, current) => {
|
|
|
|
if (
|
2019-04-11 16:19:15 +02:00
|
|
|
_.get(model._attributes, [current, 'type']) ||
|
|
|
|
_.get(model._attributes, [current, 'model'])
|
2019-03-13 19:27:18 +01:00
|
|
|
) {
|
|
|
|
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-04-11 16:19:15 +02:00
|
|
|
async update(search, params = {}) {
|
2018-01-23 15:38:43 +01:00
|
|
|
if (_.isEmpty(params)) {
|
|
|
|
params = search;
|
2017-11-15 16:59:12 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
const primaryKey = search[model.primaryKey] || search.id;
|
2018-01-23 15:38:43 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
search = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: primaryKey,
|
2018-01-23 18:54:17 +01:00
|
|
|
};
|
|
|
|
} else {
|
2019-04-11 16:19:15 +02:00
|
|
|
const entry = await this.findOne(search);
|
2018-01-23 18:54:17 +01:00
|
|
|
|
|
|
|
search = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: entry[model.primaryKey] || entry.id,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2018-01-23 15:38:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
return model
|
|
|
|
.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-04-11 16:19:15 +02:00
|
|
|
delete(params) {
|
|
|
|
return model
|
|
|
|
.forge({
|
|
|
|
[model.primaryKey]: params[model.primaryKey] || params.id,
|
|
|
|
})
|
|
|
|
.destroy();
|
2018-05-03 16:26:57 +02:00
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
search(params) {
|
|
|
|
return model
|
|
|
|
.query(function(qb) {
|
|
|
|
qb.where('username', 'LIKE', `%${params.id}%`).orWhere(
|
|
|
|
'email',
|
|
|
|
'LIKE',
|
|
|
|
`%${params.id}%`
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.fetchAll();
|
2017-11-30 12:27:04 +01:00
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
addPermission(params) {
|
|
|
|
return model.forge(params).save();
|
2018-01-23 15:38:43 +01:00
|
|
|
},
|
2017-11-30 12:27:04 +01:00
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
removePermission(params) {
|
|
|
|
const value = params[model.primaryKey]
|
|
|
|
? { [model.primaryKey]: params[model.primaryKey] || params.id }
|
2019-03-13 19:27:18 +01:00
|
|
|
: params;
|
2018-05-09 15:11:55 +02:00
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
return model
|
|
|
|
.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
|
|
|
},
|
2019-04-11 16:19:15 +02:00
|
|
|
});
|