Skip to content

Commit

Permalink
Merge pull request #175 from unitaryfund/217_submission_tag_list
Browse files Browse the repository at this point in the history
unitaryfund/metriq-app#217: Submission tag lists (back-end)
  • Loading branch information
WrathfulSpatula authored Mar 9, 2022
2 parents da08e23 + e054a6e commit 31f0969
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
38 changes: 33 additions & 5 deletions metriq-api/service/submissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,22 @@ class SubmissionService extends ModelService {
return { success: true, body: submission }
}

async populateTags (submission) {
submission.tags = []
for (let i = 0; i < submission.submissionTagRefs.length; i++) {
submission.tags.push(await submission.submissionTagRefs[i].getTag())
}
delete submission.submissionTagRefs
}

async populate (submission, userId) {
const toRet = { ...submission }
toRet.isUpvoted = ((userId > 0) && toRet.likes.length) ? (toRet.likes.find(like => like.dataValues.userId === userId) !== undefined) : false
toRet.upvotesCount = toRet.likes.length
delete toRet.likes
toRet.user = await userService.getByPk(toRet.userId)

toRet.tags = []
for (let i = 0; i < toRet.submissionTagRefs.length; i++) {
toRet.tags.push(await toRet.submissionTagRefs[i].getTag())
}
delete toRet.submissionTagRefs
await this.populateTags(submission)

toRet.methods = []
toRet.results = []
Expand Down Expand Up @@ -370,16 +374,28 @@ class SubmissionService extends ModelService {

async getTrending (startIndex, count, userId) {
const result = (await sequelize.query(this.sqlTrending(userId, '"upvotesPerHour"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

async getLatest (startIndex, count, userId) {
const result = (await sequelize.query(this.sqlLike(userId, 'submissions."createdAt"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

async getPopular (startIndex, count, userId) {
const result = (await sequelize.query(this.sqlLike(userId, '"upvotesCount"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

Expand All @@ -391,6 +407,10 @@ class SubmissionService extends ModelService {
const tagId = tag.id

const result = (await sequelize.query(this.sqlTagTrending(tagId, userId, '"upvotesPerHour"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

Expand All @@ -402,6 +422,10 @@ class SubmissionService extends ModelService {
const tagId = tag.id

const result = (await sequelize.query(this.sqlTagLike(tagId, userId, 'submissions."createdAt"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

Expand All @@ -413,6 +437,10 @@ class SubmissionService extends ModelService {
const tagId = tag.id

const result = (await sequelize.query(this.sqlTagLike(tagId, userId, '"upvotesCount"', true, count, startIndex)))[0]
for (let i = 0; i < result.length; i++) {
result[i].submissionTagRefs = (await submissionTagRefService.getBySubmissionId(result[i].id))
await this.populateTags(result[i])
}
return { success: true, body: result }
}

Expand Down
4 changes: 4 additions & 0 deletions metriq-api/service/submissionTagRefService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class SubmissionTagRefService extends SubmissionRefService {
constructor () {
super('tagId', SubmissionTagRef)
}

async getBySubmissionId (submissionId) {
return await this.SequelizeServiceInstance.findAll({ submissionId: submissionId })
}
}

module.exports = SubmissionTagRefService

0 comments on commit 31f0969

Please sign in to comment.