Skip to content

Commit d5824a8

Browse files
committed
add separate getProjectForUser to controller
1 parent f2190ba commit d5824a8

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

server/controllers/project.controller.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,29 @@ export async function projectExists(projectId) {
165165
*/
166166
export async function projectForUserExists(username, projectId) {
167167
const user = await User.findByUsername(username);
168-
if (!user) return { success: false };
168+
if (!user) return false;
169+
const project = await Project.findOne({
170+
user: user._id,
171+
$or: [{ _id: projectId }, { slug: projectId }]
172+
});
173+
return project != null;
174+
}
175+
176+
/**
177+
* @param {string} username
178+
* @param {string} projectId - the database id or the slug or the project
179+
* @return {Promise<object>}
180+
*/
181+
export async function getProjectForUser(username, projectId) {
182+
const user = await User.findByUsername(username);
183+
if (!user) return { exists: false };
169184
const project = await Project.findOne({
170185
user: user._id,
171186
$or: [{ _id: projectId }, { slug: projectId }]
172187
});
173188
return project != null
174-
? { success: true, projectName: project.name }
175-
: { success: false };
189+
? { exists: true, userProject: project }
190+
: { exists: false };
176191
}
177192

178193
/**

server/routes/server.routes.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import sendHtml, { renderIndex, renderProjectIndex } from '../views/index';
33
import { userExists } from '../controllers/user.controller';
44
import {
55
projectExists,
6-
projectForUserExists
6+
projectForUserExists,
7+
getProjectForUser
78
} from '../controllers/project.controller';
89
import { collectionForUserExists } from '../controllers/collection.controller';
910

@@ -43,15 +44,15 @@ router.get(
4344
);
4445

4546
router.get('/:username/sketches/:project_id', async (req, res) => {
46-
const exists = await projectForUserExists(
47+
const userProject = await getProjectForUser(
4748
req.params.username,
4849
req.params.project_id
4950
);
5051

51-
if (exists.success && exists.projectName) {
52-
res.send(renderProjectIndex(req.params.username, exists.projectName));
52+
if (userProject.exists) {
53+
res.send(renderProjectIndex(req.params.username, userProject.projectName));
5354
} else {
54-
sendHtml(req, res, exists);
55+
sendHtml(req, res, userProject.exists);
5556
}
5657
});
5758

server/views/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export function renderProjectIndex(username, projectName) {
102102
* @param {import('express').e.Response} res
103103
* @param {boolean} [exists]
104104
*/
105-
export default function sendHtml(req, res, exists = {success:true}) {
106-
if (!exists.success) {
105+
export default function sendHtml(req, res, exists = true) {
106+
if (!exists) {
107107
res.status(404);
108108
get404Sketch((html) => res.send(html));
109109
} else {

0 commit comments

Comments
 (0)