更新批量增删标签方法

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

View File

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

View File

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