Skip to content

Commit

Permalink
Merge 'recent_save'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray-D-Song committed Oct 31, 2024
2 parents c8503a2 + 96feb52 commit 9dec6f5
Show file tree
Hide file tree
Showing 25 changed files with 1,099 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/preview-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- dev
- main
- hotfix
- new_card
- recent_save

jobs:
deploy:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"react-error-boundary": "^4.0.13",
"react-hook-form": "^7.53.1",
"react-hot-toast": "^2.4.1",
"recharts": "^2.13.0",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
Expand Down
22 changes: 22 additions & 0 deletions packages/server/src/api/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Hono } from 'hono'
import type { HonoTypeUserInformation } from '~/constants/binding'
import { getHomeChartData } from '~/model/data'
import result from '~/utils/result'

const app = new Hono<HonoTypeUserInformation>()

app.get('/page_chart_data', async (c) => {
const data = await getHomeChartData(c.env.DB)

return c.json(result.success(data))
})

app.get('/r2_usage', async (c) => {
const res = await c.env.BUCKET.list()
return c.json(result.success({
size: res.objects.reduce((acc, obj) => acc + obj.size, 0),
count: res.objects.length,
}))
})

export default app
7 changes: 6 additions & 1 deletion packages/server/src/api/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { validator } from 'hono/validator'
import { isNil, isNotNil, isNumberString } from '@web-archive/shared/utils'
import type { HonoTypeUserInformation } from '~/constants/binding'
import result from '~/utils/result'
import { clearDeletedPage, deletePageById, getPageById, insertPage, queryDeletedPage, queryPage, restorePage, selectPageTotalCount } from '~/model/page'
import { clearDeletedPage, deletePageById, getPageById, insertPage, queryDeletedPage, queryPage, queryRecentSavePage, restorePage, selectPageTotalCount } from '~/model/page'
import { getFolderById, restoreFolder } from '~/model/folder'
import { getFileFromBucket, saveFileToBucket } from '~/utils/file'
import type { Page } from '~/sql/types'
Expand Down Expand Up @@ -103,6 +103,11 @@ app.post(
},
)

app.get('/recent_save', async (c) => {
const pages = await queryRecentSavePage(c.env.DB)
return c.json(result.success(pages))
})

app.get(
'/detail',
validator('query', (value, c) => {
Expand Down
29 changes: 29 additions & 0 deletions packages/server/src/model/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { D1Database } from '@cloudflare/workers-types/experimental'
import { selectAllPageCount, selectPageTotalCount } from './page'
import { selectAllFolders } from './folder'

async function getHomeChartData(DB: D1Database) {
const folderList = await selectAllFolders(DB)
const folderPageCountList = await Promise.all(folderList.map(async (folder) => {
const pageCount = await selectPageTotalCount(DB, { folderId: folder.id })
return {
id: folder.id,
name: folder.name,
pageCount: pageCount as number,
}
}))

const sortedFolderPageCountList = folderPageCountList
.sort((a, b) => b.pageCount - a.pageCount)
.slice(0, 5)

const allPageCount = await selectAllPageCount(DB)
return {
folders: sortedFolderPageCountList,
all: allPageCount,
}
}

export {
getHomeChartData,
}
19 changes: 19 additions & 0 deletions packages/server/src/model/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async function selectPageTotalCount(DB: D1Database, options: { folderId: number,
return result.count
}

async function selectAllPageCount(DB: D1Database) {
const sql = `
SELECT COUNT(*) as count FROM pages
WHERE isDeleted = 0
`
const result = await DB.prepare(sql).first()
return result.count
}

async function queryPage(DB: D1Database, options: { folderId: number, pageNumber?: number, pageSize?: number, keyword?: string }) {
const { folderId, pageNumber, pageSize, keyword } = options
let sql = `
Expand Down Expand Up @@ -151,6 +160,14 @@ async function clearDeletedPage(DB: D1Database) {
return result.success
}

async function queryRecentSavePage(DB: D1Database) {
const sql = `
SELECT * FROM pages WHERE isDeleted = 0 ORDER BY createdAt DESC LIMIT 20
`
const result = await DB.prepare(sql).all<Page>()
return result.results
}

export {
selectPageTotalCount,
queryPage,
Expand All @@ -161,4 +178,6 @@ export {
getPageById,
insertPage,
clearDeletedPage,
queryRecentSavePage,
selectAllPageCount,
}
2 changes: 2 additions & 0 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from 'hono'
import type { Bindings, HonoTypeUserInformation } from './constants/binding'
import tokenMiddleware from './middleware/token'
import data from './api/data'
import showcase from '~/api/showcase'
import pages from '~/api/pages'
import auth from '~/api/auth'
Expand Down Expand Up @@ -35,6 +36,7 @@ api.use(tokenMiddleware)
api.route('/pages', pages)
api.route('/auth', auth)
api.route('/folders', folders)
api.route('/data', data)
app.route('/api', api)

export default app
Loading

0 comments on commit 9dec6f5

Please sign in to comment.