all: Aggiunte varie feature e riscritte svariate cose
This commit is contained in:
+49
-40
@@ -2,15 +2,7 @@ import crypto from 'crypto'
|
||||
|
||||
import { readFile, writeFile, access, constants } from 'fs/promises'
|
||||
|
||||
import {
|
||||
MetadataProps as MetaProps,
|
||||
Problem,
|
||||
ProblemId,
|
||||
Solution,
|
||||
SolutionId,
|
||||
User,
|
||||
UserId,
|
||||
} from '../../shared/model'
|
||||
import { MetadataProps as MetaProps, Problem, ProblemId, Solution, SolutionId, User, UserId } from '../../shared/model'
|
||||
|
||||
function once<T extends (...args: any) => any>(fn: T, message: string): T {
|
||||
let flag = false
|
||||
@@ -36,23 +28,23 @@ function createMutex(): Mutex {
|
||||
|
||||
const unlock = () => {
|
||||
if (waiters.length > 0) {
|
||||
console.log(`[Mutex] Passing lock to next in queue (of size ${waiters.length})`)
|
||||
console.log(`[Database/Mutex] Passing lock to next in queue (of size ${waiters.length})`)
|
||||
const resolve = waiters.shift()!!
|
||||
resolve(once(unlock, `lock already released`))
|
||||
} else {
|
||||
locked = false
|
||||
console.log(`[Mutex] Releasing the lock`)
|
||||
console.log(`[Database/Mutex] Releasing the lock`)
|
||||
}
|
||||
}
|
||||
|
||||
const lock = (): Promise<Lock> => {
|
||||
if (locked) {
|
||||
console.log(`[Mutex] Putting into queue`)
|
||||
console.log(`[Database/Mutex] Putting into queue`)
|
||||
return new Promise(resolve => {
|
||||
waiters.push(resolve)
|
||||
})
|
||||
} else {
|
||||
console.log(`[Mutex] Acquiring the lock`)
|
||||
console.log(`[Database/Mutex] Acquiring the lock`)
|
||||
locked = true
|
||||
return Promise.resolve(once(unlock, `lock already released`))
|
||||
}
|
||||
@@ -81,10 +73,7 @@ export function createDatabase(path: string, initialValue: Database) {
|
||||
}
|
||||
}
|
||||
|
||||
async function withDatabase<R>(
|
||||
{ path, initialValue, mu }: DatabaseConnection,
|
||||
fn: (db: Database) => R | Promise<R>
|
||||
): Promise<R> {
|
||||
async function withDatabase<R>({ path, initialValue, mu }: DatabaseConnection, fn: (db: Database) => R | Promise<R>): Promise<R> {
|
||||
const unlock: Lock = await mu.lock()
|
||||
|
||||
try {
|
||||
@@ -125,18 +114,19 @@ export const getUser: (db: DatabaseConnection, id: string) => Promise<User | nul
|
||||
// Problems
|
||||
//
|
||||
|
||||
export const createProblem = (
|
||||
db: DatabaseConnection,
|
||||
{ content, createdBy }: Omit<Problem, 'id' | 'createdAt'>
|
||||
): Promise<ProblemId> =>
|
||||
export const createProblem = (db: DatabaseConnection, { title, content, createdBy }: Omit<Problem, MetaProps>): Promise<ProblemId> =>
|
||||
withDatabase(db, state => {
|
||||
const nextId = (Object.keys(state.problems).length + 1).toString() as ProblemId
|
||||
const problemIds = Object.keys(state.problems)
|
||||
const nextId = (problemIds.length > 0 ? String(parseInt(problemIds.at(-1)!) + 1) : '1') as ProblemId
|
||||
|
||||
state.problems[nextId] = {
|
||||
id: nextId,
|
||||
createdAt: new Date().toJSON(),
|
||||
deleted: false,
|
||||
|
||||
title,
|
||||
content,
|
||||
createdBy,
|
||||
createdAt: new Date().toJSON(),
|
||||
}
|
||||
|
||||
return nextId
|
||||
@@ -144,12 +134,28 @@ export const createProblem = (
|
||||
|
||||
export const getProblem = (db: DatabaseConnection, id: string): Promise<Problem | null> =>
|
||||
withDatabase(db, state => {
|
||||
return state.problems[id] ?? null
|
||||
const problem = (state.problems[id] ?? null) as Problem | null
|
||||
return problem && !problem.deleted ? problem : null
|
||||
})
|
||||
|
||||
export const getProblems = (db: DatabaseConnection): Promise<Problem[]> =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.problems)
|
||||
return Object.values(state.problems).filter(p => !p.deleted)
|
||||
})
|
||||
|
||||
export const updateProblem = (db: DatabaseConnection, id: ProblemId, problem: Partial<Omit<Problem, MetaProps>>): Promise<Problem> =>
|
||||
withDatabase(db, state => {
|
||||
state.problems[id] = {
|
||||
...state.problems[id],
|
||||
...problem,
|
||||
}
|
||||
|
||||
return state.problems[id]
|
||||
})
|
||||
|
||||
export const deleteProblem = (db: DatabaseConnection, id: ProblemId): Promise<void> =>
|
||||
withDatabase(db, state => {
|
||||
state.problems[id].deleted = true
|
||||
})
|
||||
|
||||
//
|
||||
@@ -166,6 +172,7 @@ export const createSolution = (
|
||||
state.solutions[id] = {
|
||||
id,
|
||||
createdAt: new Date().toISOString(),
|
||||
deleted: false,
|
||||
|
||||
sentBy,
|
||||
forProblem,
|
||||
@@ -179,14 +186,23 @@ export const createSolution = (
|
||||
|
||||
export const getSolution = (db: DatabaseConnection, id: SolutionId): Promise<Solution | null> =>
|
||||
withDatabase(db, state => {
|
||||
return state.solutions[id] ?? null
|
||||
const solution = (state.solutions[id] ?? null) as Solution | null
|
||||
return solution && !solution.deleted ? solution : null
|
||||
})
|
||||
|
||||
export const updateSolution = (
|
||||
db: DatabaseConnection,
|
||||
id: SolutionId,
|
||||
solution: Partial<Omit<Solution, MetaProps>>
|
||||
): Promise<Solution> =>
|
||||
export const getSolutions = (db: DatabaseConnection) =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.solutions).filter(s => !s.deleted)
|
||||
})
|
||||
|
||||
export const getVisibleSolutions = (db: DatabaseConnection): Promise<Solution[]> =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.solutions)
|
||||
.filter(s => !s.deleted)
|
||||
.filter(s => s.visible)
|
||||
})
|
||||
|
||||
export const updateSolution = (db: DatabaseConnection, id: SolutionId, solution: Partial<Omit<Solution, MetaProps>>): Promise<Solution> =>
|
||||
withDatabase(db, state => {
|
||||
state.solutions[id] = {
|
||||
...state.solutions[id],
|
||||
@@ -196,14 +212,7 @@ export const updateSolution = (
|
||||
return state.solutions[id]
|
||||
})
|
||||
|
||||
export const getSolutions = (db: DatabaseConnection) =>
|
||||
export const deleteSolution = (db: DatabaseConnection, id: SolutionId): Promise<void> =>
|
||||
withDatabase(db, state => {
|
||||
let solutions = Object.values(state.solutions)
|
||||
|
||||
return solutions
|
||||
})
|
||||
|
||||
export const getVisibleSolutions = (db: DatabaseConnection) =>
|
||||
withDatabase(db, state => {
|
||||
return Object.values(state.solutions).filter(s => s.visible)
|
||||
state.solutions[id].deleted = true
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user