更新批量增删标签方法

This commit is contained in:
kilo5hz 2022-01-07 20:18:17 +08:00
parent 2db02aee41
commit 928f1ce1ee
3 changed files with 17 additions and 25 deletions

View File

@ -88,7 +88,7 @@ export default (app: Router) => {
'/removelabels',
celebrate({
body: Joi.object({
ids:Joi.array().items(Joi.string().required()),
ids:Joi.array().items(Joi.number().required()),
labels:Joi.array().items(Joi.string().required()),
})
}),
@ -109,7 +109,7 @@ export default (app: Router) => {
'/addlabels',
celebrate({
body: Joi.object({
ids:Joi.array().items(Joi.string().required()),
ids:Joi.array().items(Joi.number().required()),
labels:Joi.array().items(Joi.string().required()),
})
}),
@ -190,7 +190,7 @@ export default (app: Router) => {
command: Joi.string().optional(),
schedule: Joi.string().optional(),
name: Joi.string().optional(),
id: Joi.string().required(),
id: Joi.number().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {

View File

@ -94,29 +94,21 @@ export default class CronService {
}
public async addLabels(ids: string[],labels: string[]){
return new Promise((resolve: any) => {
this.cronDb.update(
{ _id: { $in: ids } },
{ $addToSet: { labels: { $each: labels} } },
{ multi: true },
async (err) => {
resolve();
},
);
});
const docs = await CrontabModel.findAll({ where: { id:ids }});
for (const doc of docs) {
await CrontabModel.update({
labels: Array.from(new Set(doc.labels.concat(labels)))
},{ where: {id:doc.id}});
}
}
public async removeLabels(ids: string[],labels: string[]){
return new Promise((resolve: any) => {
this.cronDb.update(
{ _id: { $in: ids } },
{ $pull: { labels: { $in: labels} } },
{ multi: true },
async (err) => {
resolve();
},
);
});
const docs = await CrontabModel.findAll({ where: { id:ids }});
for (const doc of docs) {
await CrontabModel.update({
labels: doc.labels.filter( label => !labels.includes(label) )
},{ where: {id:doc.id}});
}
}
public async crontabs(searchText?: string): Promise<Crontab[]> {

View File

@ -151,10 +151,10 @@ const CronLabelModal = ({
<Button onClick={() => handleCancel(false)} key="test">
</Button>,
<Button type="primary" danger onClick={() => update('removelabels')}>
<Button type="primary" danger onClick={() => update('removeLabels')}>
</Button>,
<Button type="primary" onClick={() => update('addlabels')}>
<Button type="primary" onClick={() => update('addLabels')}>
</Button>
];