Skip to content

Commit

Permalink
[front] Refactor getContentTagsData
Browse files Browse the repository at this point in the history
  • Loading branch information
Kogepan229 committed Feb 28, 2024
1 parent 955aee4 commit f3cab50
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions next_app/src/features/tags/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,13 @@ export type SidebarTagsData = { tag: TagData; count: number }[]

export const getContentTagsData = async (contentID: string) => {
const con = await getDBConnection()
const [rows, _] = await con.query(`SELECT tag_id, priority FROM tags_of_contents WHERE content_id=?`, [contentID])
const data1: { tag_id: string; priority: number }[] = JSON.parse(JSON.stringify(rows))

type ContentTag = TagData & {
priority: number
}
let _tags: ContentTag[] = []

for (let value of data1) {
let [rows2, _] = await con.query(`SELECT id, name FROM tags WHERE id=?`, [value.tag_id])
let data2: ContentTag[] = JSON.parse(JSON.stringify(rows2))
_tags.push({ id: data2[0].id, name: data2[0].name, priority: value.priority })
}

const [rows, _] = await con.query(
`SELECT tags.id, tags.name FROM tags JOIN tags_of_contents ON tags.id = tags_of_contents.tag_id WHERE tags_of_contents.content_id=? ORDER BY tags_of_contents.priority`,
[contentID]
)
con.end()

_tags.sort((a: ContentTag, b: ContentTag) => {
return a.priority - b.priority
})

let tags = _tags.map(value => {
return { id: value.id, name: value.name } as TagData
})
const tags: TagData[] = JSON.parse(JSON.stringify(rows))

return tags
}
Expand Down

0 comments on commit f3cab50

Please sign in to comment.