Almost MVP
This commit is contained in:
+25
-26
@@ -37,7 +37,7 @@ function createMutex(): Mutex {
|
||||
const unlock = () => {
|
||||
if (waiters.length > 0) {
|
||||
console.log(`[Mutex] Passing lock to next in queue (of size ${waiters.length})`)
|
||||
const resolve = waiters.shift()
|
||||
const resolve = waiters.shift()!!
|
||||
resolve(once(unlock, `lock already released`))
|
||||
} else {
|
||||
locked = false
|
||||
@@ -116,8 +116,8 @@ export const getUsers = (db: DatabaseConnection) =>
|
||||
return Object.values(state.users)
|
||||
})
|
||||
|
||||
export const getUser = (db: DatabaseConnection, id: string) =>
|
||||
withDatabase(db, (state: Database): User | null => {
|
||||
export const getUser: (db: DatabaseConnection, id: string) => Promise<User | null> = (db, id) =>
|
||||
withDatabase(db, state => {
|
||||
return state.users[id] ?? null
|
||||
})
|
||||
|
||||
@@ -142,9 +142,9 @@ export const createProblem = (
|
||||
return nextId
|
||||
})
|
||||
|
||||
export const getProblem = (db: DatabaseConnection, id: string): Promise<Problem> =>
|
||||
export const getProblem = (db: DatabaseConnection, id: string): Promise<Problem | null> =>
|
||||
withDatabase(db, state => {
|
||||
return state.problems[id]
|
||||
return state.problems[id] ?? null
|
||||
})
|
||||
|
||||
export const getProblems = (db: DatabaseConnection): Promise<Problem[]> =>
|
||||
@@ -158,53 +158,52 @@ export const getProblems = (db: DatabaseConnection): Promise<Problem[]> =>
|
||||
|
||||
export const createSolution = (
|
||||
db: DatabaseConnection,
|
||||
{ sentBy, forProblem, content }: Omit<Solution, MetaProps>
|
||||
{ sentBy, forProblem, content }: Omit<Solution, MetaProps | 'status' | 'visible'>
|
||||
): Promise<SolutionId> =>
|
||||
withDatabase(db, state => {
|
||||
const id = crypto.randomBytes(10).toString('hex')
|
||||
const id = crypto.randomBytes(10).toString('hex') as SolutionId
|
||||
|
||||
state.solutions[id] = {
|
||||
id,
|
||||
createdAt: new Date().toISOString(),
|
||||
|
||||
sentBy,
|
||||
forProblem,
|
||||
content,
|
||||
status: 'pending',
|
||||
visible: false,
|
||||
}
|
||||
|
||||
return id
|
||||
})
|
||||
|
||||
export const getSolution = (db: DatabaseConnection, id: SolutionId): Promise<Solution> =>
|
||||
export const getSolution = (db: DatabaseConnection, id: SolutionId): Promise<Solution | null> =>
|
||||
withDatabase(db, state => {
|
||||
return state.solutions[id]
|
||||
return state.solutions[id] ?? null
|
||||
})
|
||||
|
||||
export const updateSolution = (
|
||||
db: DatabaseConnection,
|
||||
id: SolutionId,
|
||||
solution: Omit<Solution, MetaProps>
|
||||
solution: Partial<Omit<Solution, MetaProps>>
|
||||
): Promise<Solution> =>
|
||||
withDatabase(db, state => {
|
||||
state.solutions[id] = { id, ...solution }
|
||||
state.solutions[id] = {
|
||||
...state.solutions[id],
|
||||
...solution,
|
||||
}
|
||||
|
||||
return state.solutions[id]
|
||||
})
|
||||
|
||||
type SolutionsQuery = Partial<{
|
||||
sentBy?: UserId
|
||||
forProblem?: ProblemId
|
||||
}>
|
||||
|
||||
export const getSolutions = (db: DatabaseConnection, { sentBy, forProblem }: SolutionsQuery = {}) =>
|
||||
export const getSolutions = (db: DatabaseConnection) =>
|
||||
withDatabase(db, state => {
|
||||
let solutions = Object.values(state.solutions)
|
||||
|
||||
console.log(solutions.length, sentBy, forProblem)
|
||||
|
||||
if (sentBy) {
|
||||
solutions = solutions.filter(s => s.sentBy === sentBy)
|
||||
}
|
||||
if (forProblem) {
|
||||
solutions = solutions.filter(s => s.forProblem === forProblem)
|
||||
}
|
||||
|
||||
return solutions
|
||||
})
|
||||
|
||||
export const getVisibleSolutions = (db: DatabaseConnection) =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.solutions).filter(s => s.visible)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user