Skip to content

Commit 270bf47

Browse files
authored
Merge pull request #6 from oslabs-beta/liam/marketplacecontroller
Added and tested marketplaceController endpoints
2 parents db6e5af + 0deaa03 commit 270bf47

File tree

6 files changed

+154
-27
lines changed

6 files changed

+154
-27
lines changed

package-lock.json

Lines changed: 9 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163
"socket.io": "^4.6.1",
164164
"socket.io-client": "^4.6.1",
165165
"source-map-support": "^0.5.21",
166-
"typescript-coverage-report": "^0.7.0",
167166
"ts-node": "^10.9.1",
167+
"typescript-coverage-report": "^0.7.0",
168168
"uniqid": "^5.4.0",
169169
"use-debounce": "^8.0.3",
170170
"uuid": "^8.3.2"
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { MarketplaceController } from '../interfaces';
2+
import { Projects } from '../models/reactypeModels';
3+
4+
// array of objects, objects inside
5+
type Projects = { project: {} }[];
6+
7+
const marketplaceController: MarketplaceController = {
8+
/**
9+
* Middleware function that finds and returns all published projects from the database
10+
* @return sends the entire project document to frontend
11+
*/
12+
getPublishedProjects: (req, res, next) => {
13+
Projects.find({ published: true }, (err, projects) => {//removed the typing for now for projects: since its the mongodb doc
14+
if (err) {
15+
return next({
16+
log: `Error in marketplaceController.getPublishedProjects: ${err}`,
17+
message: {
18+
err: 'Error in marketplaceController.getPublishedProjects, check server logs for details'
19+
}
20+
});
21+
}
22+
// returns the entire project document, including the id
23+
res.locals.publishedProjects = projects;
24+
return next();
25+
});
26+
},
27+
28+
/**
29+
*
30+
* Middleware function that publishes (and saves) a project to the database
31+
* @return sends the updated project to the frontend
32+
*/
33+
publishProject: (req, res, next) => {
34+
const { _id, project, comments, userId, username, name } = req.body;
35+
const createdAt = Date.now();
36+
if (userId === req.cookies.ssid) {
37+
Projects.findOneAndUpdate(
38+
// looks in projects collection for project by Mongo id
39+
{ _id },
40+
// update or insert the project
41+
{ project, createdAt, published: true, comments, name, userId, username },
42+
// Options:
43+
// upsert: true - if none found, inserts new project, otherwise updates it
44+
// new: true - returns updated document not the original one
45+
{ upsert: true, new: true },
46+
(err, result) => {
47+
if (err) {
48+
return next({
49+
log: `Error in marketplaceController.publishProject: ${err}`,
50+
message: {
51+
err: 'Error in marketplaceController.publishProject, check server logs for details'
52+
}
53+
});
54+
}
55+
res.locals.publishedProject = result;
56+
return next();
57+
}
58+
);
59+
}
60+
else {
61+
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
62+
return next({
63+
log: 'Error in marketplaceController.publishProject',
64+
message: {
65+
err: 'Error in marketplaceController.publishProject, check server logs for details'
66+
}
67+
})
68+
}
69+
},
70+
71+
/**
72+
*
73+
* Middleware function that marks a project as unpublished in the database
74+
* @return sends the updated project to the frontend
75+
*/
76+
unpublishProject: (req, res, next) => {
77+
// pull project name and userId from req.body
78+
const { _id, userId } = req.body;
79+
//check if req.cookies.ssid matches userId
80+
81+
if (userId === req.cookies.ssid) {
82+
Projects.findOneAndUpdate({ _id }, {published: false}, { new: true }, (err, result) => {
83+
if (err) {
84+
return next({
85+
log: `Error in marketplaceController.unpublishProject: ${err}`,
86+
message: {
87+
err: 'Error in marketplaceController.unpublishProject, check server logs for details'
88+
}
89+
});
90+
}
91+
res.locals.unpublishedProject = result;
92+
return next();
93+
});
94+
}
95+
else {
96+
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
97+
return next({
98+
log: `Error in marketplaceController.unpublishProject`,
99+
message: {
100+
err: 'Error in marketplaceController.unpublishProject, userId of project does not match cookies.ssid'
101+
}
102+
})
103+
104+
}
105+
}
106+
};
107+
export default marketplaceController;

server/controllers/sessionController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import dotenv from 'dotenv';
44
import { SessionController, SessionCookie } from '../interfaces';
55

66
dotenv.config();
7-
7+
// here we are cheching that the user making the request is login in and has a valid cookieId
88
const sessionController: SessionController = {
99
isLoggedIn: async (req, res, next) => {
1010
try {
1111
let cookieId;
1212
if (req.cookies) {
13+
// if the request cookies exist then it assigns it to cookieId
1314
cookieId = req.cookies.ssid;
1415
} else {
16+
// else it creates a new cookieId for the user based on the userId
1517
cookieId = req.body.userId;
1618
}
1719

server/interfaces.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export interface ProjectController {
1111
deleteProject: RequestHandler;
1212
}
1313

14+
export interface MarketplaceController {
15+
publishProject: RequestHandler;
16+
getPublishedProjects: RequestHandler;
17+
unpublishProject: RequestHandler;
18+
}
19+
1420
export interface RequestId extends Request {
1521
user: {
1622
id: string;

server/server.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { json, urlencoded } = bodyParser;
1010
const { makeExecutableSchema } = require('@graphql-tools/schema');
1111

1212
import express from 'express';
13-
// import cookieParser from 'cookie-parser';
13+
import cookieParser from 'cookie-parser';
1414

1515
import config from '../config.js';
1616
const { API_BASE_URL, DEV_PORT } = config;
@@ -22,6 +22,7 @@ import userController from './controllers/userController';
2222
import cookieController from './controllers/cookieController';
2323
import sessionController from './controllers/sessionController';
2424
import projectController from './controllers/projectController';
25+
import marketplaceController from './controllers/marketplaceController';
2526

2627
// // docker stuff
2728
import { fileURLToPath } from 'url';
@@ -40,7 +41,7 @@ const isTest = process.env.NODE_ENV === 'test';
4041

4142
app.use(express.json({ limit: '100mb' }));
4243
app.use(express.urlencoded({ limit: '100mb', extended: true }));
43-
44+
app.use(cookieParser());//added cookie parser
4445
// Routes
4546
// const stylesRouter = require('./routers/stylesRouter');
4647
import stylesRouter from './routers/stylesRouter';
@@ -191,6 +192,31 @@ app.delete(
191192
projectController.deleteProject,
192193
(req, res) => res.status(200).json(res.locals.deleted)
193194
);
195+
196+
//Publish to Marketplace
197+
app.post(
198+
'/publishProject',
199+
sessionController.isLoggedIn,
200+
marketplaceController.publishProject,
201+
(req, res) => res.status(200).json(res.locals.publishedProject)
202+
);
203+
204+
//Unpublish from Marketplace
205+
app.patch(
206+
'/unpublishProject',
207+
sessionController.isLoggedIn,
208+
marketplaceController.unpublishProject,
209+
(req, res) => res.status(200).json(res.locals.unpublishedProject)
210+
);
211+
212+
//Get from Marketplace
213+
app.get(
214+
'/getMarketplaceProjects',
215+
sessionController.isLoggedIn,
216+
marketplaceController.getPublishedProjects,
217+
(req, res) => res.status(200).json(res.locals.publishedProjects)
218+
);
219+
194220
// serve index.html on the route '/'
195221
const isDocker = process.env.IS_DOCKER === 'true';
196222
console.log('this is running on docker: ', isDocker);

0 commit comments

Comments
 (0)