feat: new jumbotron page for dm hall screen
This commit is contained in:
@@ -65,6 +65,9 @@ export function createDatabaseWrapper(db: DatabaseInternal): DatabaseConnection
|
||||
getProblem(id: string): Promise<Problem | null> {
|
||||
return getProblem(db, id)
|
||||
},
|
||||
getJumbotronProblem(): Promise<Problem | null> {
|
||||
return getJumbotronProblem(db)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +148,18 @@ export const getProblem = (db: DatabaseInternal, id: string): Promise<Problem |
|
||||
return problem && !problem.deleted ? problem : null
|
||||
})
|
||||
|
||||
export const getJumbotronProblem = (db: DatabaseInternal): Promise<Problem | null> =>
|
||||
withDatabase(db, state => {
|
||||
const mostRecentProblems = Object.values(state.problems)
|
||||
.filter(p => !p.deleted)
|
||||
.sort((p1, p2) => new Date(p2.createdAt).getTime() - new Date(p1.createdAt).getTime())
|
||||
.slice(0, 3)
|
||||
|
||||
if (mostRecentProblems.length === 0) return null
|
||||
|
||||
return mostRecentProblems[Math.floor(Math.random() * mostRecentProblems.length)]
|
||||
})
|
||||
|
||||
export const getProblems = (db: DatabaseInternal): Promise<Problem[]> =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.problems).filter(p => !p.deleted)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { UserId } from '../../shared/model'
|
||||
import { Database } from './database'
|
||||
import { Database } from '../../shared/database'
|
||||
|
||||
export const initialDatabaseValue: Database = {
|
||||
users: {},
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
createSolution,
|
||||
deleteProblem,
|
||||
deleteSolution,
|
||||
getJumbotronProblem,
|
||||
getProblem,
|
||||
getProblems,
|
||||
getSolution,
|
||||
@@ -140,6 +141,11 @@ export async function createApiRouter(): Promise<[Router, DatabaseConnection]> {
|
||||
res.json(users)
|
||||
})
|
||||
|
||||
r.get('/api/jumbotron', async (req, res) => {
|
||||
const problem = await getJumbotronProblem(db)
|
||||
res.json(problem)
|
||||
})
|
||||
|
||||
r.get('/api/problems', async (req, res) => {
|
||||
type ProblemWithSolutionsCount = ProblemModel & { solutionsCount?: number }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user