Skip to content

Commit 88ecadb

Browse files
authored
Merge pull request #361 from topcoder-platform/support-multiple-attachments
Support creating multiple attachments
2 parents 8a00869 + 54a13ca commit 88ecadb

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

docs/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ paths:
20952095
tags:
20962096
- Attachments
20972097
description: >
2098-
Create a new attachment in the system.
2098+
Create a new attachment in the system. If you want to create multiple attachment, you can pass an array of objects instead of a single object.
20992099
security:
21002100
- bearer: []
21012101
produces:

src/controllers/AttachmentController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Controller for attachment endpoints
33
*/
44
const HttpStatus = require('http-status-codes')
5+
const _ = require('lodash')
56
const service = require('../services/AttachmentService')
67

78
/**
@@ -10,7 +11,8 @@ const service = require('../services/AttachmentService')
1011
* @param {Object} res the response
1112
*/
1213
async function createAttachment (req, res) {
13-
const result = await service.createAttachment(req.authUser, req.params.challengeId, req.body)
14+
const body = _.isArray(req.body) ? req.body : [req.body]
15+
const result = await service.createAttachment(req.authUser, req.params.challengeId, body)
1416
res.status(HttpStatus.CREATED).send(result)
1517
}
1618

src/services/AttachmentService.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,40 @@ async function _getChallengeAttachment (challengeId, attachmentId) {
5252
/**
5353
* Create attachment.
5454
* @param {String} challengeId the challenge id
55-
* @param {Object} attachment the attachment to created
55+
* @param {Array} attachments the attachments to be created
5656
* @returns {Object} the created attachment
5757
*/
58-
async function createAttachment (currentUser, challengeId, attachment) {
58+
async function createAttachment (currentUser, challengeId, attachments) {
5959
const challenge = await helper.getById('Challenge', challengeId)
6060
await helper.ensureUserCanModifyChallenge(currentUser, challenge)
61-
validateUrl(attachment.url)
62-
const attachmentObject = { id: uuid(), challengeId, ...attachment }
63-
const ret = await helper.create('Attachment', attachmentObject)
61+
const newAttachments = []
62+
for (const attachment of attachments) {
63+
validateUrl(attachment.url)
64+
const attachmentObject = { id: uuid(), challengeId, ...attachment }
65+
const newAttachment = await helper.create('Attachment', attachmentObject)
66+
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
67+
newAttachments.push(newAttachment)
68+
}
6469
// update challenge object
6570
await challengeService.partiallyUpdateChallenge(currentUser, challengeId, {
6671
attachments: [
6772
..._.get(challenge, 'attachments', []),
68-
ret
73+
...newAttachments
6974
]
7075
})
7176
// post bus event
72-
await helper.postBusEvent(constants.Topics.ChallengeAttachmentCreated, ret)
73-
return ret
77+
return newAttachments
7478
}
7579

7680
createAttachment.schema = {
7781
currentUser: Joi.any(),
7882
challengeId: Joi.id(),
79-
attachment: Joi.object().keys({
83+
attachments: Joi.array().items(Joi.object().keys({
8084
name: Joi.string().required(),
8185
url: Joi.string().uri().required(),
8286
fileSize: Joi.fileSize(),
8387
description: Joi.string()
84-
}).required()
88+
})).required().min(1)
8589
}
8690

8791
/**

0 commit comments

Comments
 (0)