feat: add ?public parameter to /api/solutions

pull/1/head
Francesco Minnocci 2 years ago
parent f9d264c6c1
commit 3088faaf3f
Signed by: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -18,7 +18,7 @@ export const UserPage = ({ uid }: RouteProps) => {
if(stats) { if(stats) {
const userStats = stats[uid] const userStats = stats[uid]
const [solutions, refreshSolutions] = useResource<SolutionModel[]>(`/api/solutions?user=${uid}`, []) const [solutions, refreshSolutions] = useResource<SolutionModel[]>(`/api/solutions?user=${uid}&public`, [])
const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, false) const sortedSolutions = sortByStringKey(solutions, s => s.createdAt, false)
return ( return (

@ -242,11 +242,12 @@ export async function createApiRouter() {
r.get('/api/solutions', async (req, res) => { r.get('/api/solutions', async (req, res) => {
let queryUser = (req.query.user ?? null) as UserId | null let queryUser = (req.query.user ?? null) as UserId | null
let queryProblem = (req.query.problem ?? null) as ProblemId | null let queryProblem = (req.query.problem ?? null) as ProblemId | null
let isPublic = (req.query.public === '')
const requestUser = await getRequestUser(req) const requestUser = await getRequestUser(req)
let solutions = await getSolutions(db) let solutions = await getSolutions(db)
// se l'utente non è loggato o se non è un amministratore allora mostra solo le soluzioni "visibili" // se l'utente non è loggato o se non è un amministratore, mostra solo le soluzioni "visibili"
if (!requestUser || !isAdministrator(requestUser.role)) { if (!requestUser || !isAdministrator(requestUser.role)) {
solutions = solutions.filter(s => s.visible || (requestUser && s.sentBy === requestUser.id)) solutions = solutions.filter(s => s.visible || (requestUser && s.sentBy === requestUser.id))
} }
@ -254,6 +255,11 @@ export async function createApiRouter() {
if (queryUser !== null) { if (queryUser !== null) {
solutions = solutions.filter(s => s.sentBy === queryUser) solutions = solutions.filter(s => s.sentBy === queryUser)
} }
if(isPublic) {
solutions = solutions.filter(s => s.visible)
}
// filtra rispetto ai problemi // filtra rispetto ai problemi
if (queryProblem !== null) { if (queryProblem !== null) {
solutions = solutions.filter(s => s.forProblem === queryProblem) solutions = solutions.filter(s => s.forProblem === queryProblem)

Loading…
Cancel
Save