Skip to content

Commit

Permalink
feat: add /api/articles/list api
Browse files Browse the repository at this point in the history
  • Loading branch information
MarleneJiang committed Jan 26, 2023
1 parent 869b173 commit 1702598
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
<<<<<<< Updated upstream
"x-generation-date": "2023-01-26T05:03:19.417Z"
=======
"x-generation-date": "2023-01-26T13:01:50.925Z"
>>>>>>> Stashed changes
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions frontend/composables/useTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function getChineseDate() {
return new Date(
new Date().getTime()
+ new Date().getTimezoneOffset() * 60 * 1000
+ 8 * 60 * 60 * 1000,
)
}

// 返回num天前的日期
export function useTime(num: number) {
const date = new Date(getChineseDate().getTime() - num * 24 * 60 * 60 * 1000)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return `${year}-${month}-${day}`
}
2 changes: 1 addition & 1 deletion frontend/server/api/articles/[id].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface IAuthor {
name: string
motto: string
Expand Down
92 changes: 92 additions & 0 deletions frontend/server/api/articles/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useGraphql } from '~~/composables/useGraphql'
import { useTime } from '~~/composables/useTime'
interface IAuthor {
name: string
}

interface ITagItem {
tag: string
}

interface IArticleItem {
id: string
title: string
viewed: number
liked: number
commented: number
summary: string
cover: string
createdAt: string
authorId: IAuthor
tagIds: { data: ITagItem[] }
}
export default defineEventHandler(async (event): Promise<IArticleItem[]> => {
const query = getQuery(event)
const sort = query.sort || ''// recommended、newest、hottest(全部)、three_days_hottest、weekly_hottest、monthly_hottest
const strategy = ['', '']
switch (sort) {
case 'recommended':
strategy[1] = 'sort: "shared:desc"'
break
case 'newest':
strategy[1] = 'sort: "createdAt:desc"'
break
case 'hottest':
strategy[1] = 'sort: "viewed:desc"'
break
case 'three_days_hottest':
strategy[0] = `{createdAt: { gte: "${useTime(3)}T00:00:00.000Z" }}`
strategy[1] = 'sort: "viewed:desc"'
break
case 'weekly_hottest':
strategy[0] = `{createdAt: { gte: "${useTime(7)}T00:00:00.000Z" }}`
strategy[1] = 'sort: "viewed:desc"'
break
case 'monthly_hottest':
strategy[0] = `{createdAt: { gte: "${useTime(30)}T00:00:00.000Z" }}`
strategy[1] = 'sort: "viewed:desc"'
break
}
const type = query.type || ''// all、others
const pageNum = query.pageNum || '1'// 1 to ∞
const reqQuery = `query{
articles(
filters: {
and:[
${strategy[0]},
{typeId: { type: { ${type ? `eq: "${type}"` : ''} } }}
]
}
${strategy[1]}
pagination: { page: ${pageNum || '1'}, pageSize: 20 }
){
data{
id
attributes{
title
viewed
liked
commented
summary
cover
createdAt
authorId{
data{
attributes{
name
}
}
}
tagIds{
data{
attributes{
tag
}
}
}
}
}
}
}`
return await useGraphql(reqQuery)
})
2 changes: 1 addition & 1 deletion frontend/server/api/articles/tags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface ITagItem {
tag: string
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/server/api/authors/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface IAuthorListItem {
name: string
motto: string
Expand Down
2 changes: 1 addition & 1 deletion frontend/server/api/global/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface IGadget {
title: string
subscribe: string
Expand Down
2 changes: 1 addition & 1 deletion frontend/server/api/global/navs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface INavItem {
name: string
url: string
Expand Down
2 changes: 1 addition & 1 deletion frontend/server/api/global/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGraphql } from '~~/composables/graphql'
import { useGraphql } from '~~/composables/useGraphql'
interface ITypeItem {
type: string
}
Expand Down

0 comments on commit 1702598

Please sign in to comment.