diff options
-rwxr-xr-x | index.js | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/index.js b/index.js index ef79d57..4818521 100755 --- a/index.js +++ b/index.js @@ -70,6 +70,11 @@ const getProject = function(projectID, token) { .then(res => res.json()) } +const getStudio = function(projectID) { + return fetch(`https://api.scratch.mit.edu/studios/${projectID}`) + .then(res => res.json()) +} + const getUser = function(username) { return fetch(`https://api.scratch.mit.edu/users/${username}`) .then(res => res.json()) @@ -390,7 +395,7 @@ const handleRequest = async (request, response) => { return page(request, response, fixWS` <h1>${filterHTML(project.title)}</h1> <p>Created by ${templates.user(project.author.username)}.${parentProjectText}</p> - <p><img src="${project.image}" alt="The thumbnail for this project"></p> + <p><img src="${project.image}" alt="This project's thumbnail"></p> ${project.instructions ? fixWS` <h2>Instructions</h2> ${templates.longField(project.instructions)} @@ -421,6 +426,42 @@ const handleRequest = async (request, response) => { } } + if (compareArr(urlParts.slice(0, 2), ['studios', id => /^[0-9]*$/.test(id)])) { + const studioID = urlParts[1] + + const studio = await getStudio(studioID) + if (studio.code === 'NotFound') { + response.statusCode = 404 + return page(request, response, fixWS` + 404. Sorry, that studo doesn't exist. + `, 'Studio Not Found') + } + + if (urlParts.length === 2) { + const projects = await fetch(`https://api.scratch.mit.edu/studios/${studioID}/projects?limit=5`).then(res => res.json()) + const projectsText = projects.map(templates.projectThumbnail).join('\n') + + return page(request, response, fixWS` + <h1>${filterHTML(studio.title)}</h1> + <p><img src="${studio.image}" alt="This studio's thumbnail"></p> + <h2>Description</h2> + ${templates.longField(studio.description)} + <h2>Projects</h2> + ${projects.length ? fixWS` + <ul> + ${projectsText} + </ul> + <p><a href="/studios/${studioID}/projects">See all!</a></p> + ` : `<p>This studio doesn't have any projects yet!</p>`} + `) + } else if (compareArr(urlParts.slice(2), ['projects'])) { + return page(request, response, fixWS` + <h1>Projects in ${filterHTML(studio.title)}</h1> + ${await templates.projectList(`https://api.scratch.mit.edu/studios/${studioID}/projects`, pathname, pageNumber)} + `, `Projects in ${studio.title}`) + } + } + if (compareArr(urlParts.slice(0, 2), ['users', name => /^[a-zA-Z0-9\-_]*$/.test(name)])) { const username = urlParts[1] const user = await getUser(username) |