2018-02-19 15:41:26 +01:00
|
|
|
const _ = require('lodash');
|
2019-03-13 19:27:18 +01:00
|
|
|
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
2018-02-19 15:41:26 +01: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());
|
2018-02-19 15:41:26 +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();
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
async findOne(params, populate) {
|
|
|
|
const primaryKey = params[model.primaryKey] || params._id;
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
params = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: primaryKey,
|
2018-04-30 18:00:01 +02:00
|
|
|
};
|
2018-02-19 15:41:26 +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),
|
|
|
|
});
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
return record ? record.toJSON() : record;
|
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
create(params) {
|
|
|
|
return model
|
2018-02-19 15:41:26 +01:00
|
|
|
.forge()
|
2019-04-11 16:19:15 +02:00
|
|
|
.save(
|
|
|
|
Object.keys(params).reduce((acc, current) => {
|
|
|
|
if (
|
|
|
|
_.get(model._attributes, [current, 'type']) ||
|
|
|
|
_.get(model._attributes, [current, 'model'])
|
|
|
|
) {
|
|
|
|
acc[current] = params[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
.catch(err => {
|
2018-02-19 15:41:26 +01:00
|
|
|
if (err.detail) {
|
|
|
|
const field = _.last(_.words(err.detail.split('=')[0]));
|
|
|
|
err = { message: `This ${field} is already taken`, field };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
async update(search, params = {}) {
|
2018-02-19 15:41:26 +01:00
|
|
|
if (_.isEmpty(params)) {
|
|
|
|
params = search;
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
const primaryKey = search[model.primaryKey] || search.id;
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
search = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: primaryKey,
|
2018-02-19 15:41:26 +01:00
|
|
|
};
|
|
|
|
} else {
|
2019-04-11 16:19:15 +02:00
|
|
|
const entry = await this.findOne(search);
|
2018-02-19 15:41:26 +01:00
|
|
|
|
|
|
|
search = {
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: entry[model.primaryKey] || entry.id,
|
2018-04-30 18:00:01 +02:00
|
|
|
};
|
2018-02-19 15:41:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
return model
|
|
|
|
.forge(search)
|
2018-02-19 15:41:26 +01:00
|
|
|
.save(params, {
|
2019-04-11 16:19:15 +02:00
|
|
|
patch: true,
|
2018-02-19 15:41:26 +01:00
|
|
|
})
|
2019-04-11 16:19:15 +02:00
|
|
|
.catch(err => {
|
2018-02-19 15:41:26 +01:00
|
|
|
const field = _.last(_.words(err.detail.split('=')[0]));
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
delete(params) {
|
|
|
|
return model
|
2018-02-19 15:41:26 +01:00
|
|
|
.forge({
|
2019-04-11 16:19:15 +02:00
|
|
|
[model.primaryKey]: params[model.primaryKey] || params.id,
|
2018-02-19 15:41:26 +01:00
|
|
|
})
|
|
|
|
.destroy();
|
|
|
|
},
|
|
|
|
|
2019-04-11 16:19:15 +02:00
|
|
|
search(params) {
|
|
|
|
return model
|
|
|
|
.query(qb => {
|
|
|
|
qb.whereRaw('LOWER(hash) LIKE ?', [`%${params.id}%`]).orWhereRaw(
|
|
|
|
'LOWER(name) LIKE ?',
|
|
|
|
[`%${params.id}%`]
|
|
|
|
);
|
2018-02-19 15:41:26 +01:00
|
|
|
})
|
|
|
|
.fetchAll();
|
|
|
|
},
|
2019-04-11 16:19:15 +02:00
|
|
|
});
|