Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react changes #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"cmath": "cpp",
"complex": "cpp"
}
}
35 changes: 35 additions & 0 deletions week-2/Week-2-Assignments/02-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions week-2/Week-2-Assignments/02-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"testTimeout": 10000
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"uuid": "^9.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ app.use((req, res, next) => {
res.status(404).send();
});

app.listen(3000);

module.exports = app;
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
*/
const express = require('express');
const bodyParser = require('body-parser');

const cors = require("cors");
const app = express();

app.use(cors());
app.use(bodyParser.json());

let todos = [];
Expand Down Expand Up @@ -97,4 +97,8 @@ app.use((req, res, next) => {
res.status(404).send();
});

app.listen(3000, () => {
console.log("listening on 3000");
});

module.exports = app;
2 changes: 1 addition & 1 deletion week-3/01-reconciler/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<body>
<div id="mainArea"></div>
<script src="script-ugly.js"></script>
<script src="script-less-ugly.js"></script>
</body>

</html>
185 changes: 172 additions & 13 deletions week-3/02-course-app-easy/index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,213 @@
const express = require('express');
const app = express();

app.use(express.json());


let ADMINS = [];
let USERS = [];
let COURSES = [];
let PURCHASED_COURSES = [];

function checkIfAdmin(req) {
const adminCreds = {
username : req.headers.username,
password : req.headers.password
};
const checkUsername = ADMINS.find((currVal) => {
return (currVal.username === adminCreds.username && currVal.password === adminCreds.password);
});
return checkUsername;
}

//- POST /admin/signup
// Description: Creates a new admin account.
// Input: { username: 'admin', password: 'pass' }
// Output: { message: 'Admin created successfully' }

// Admin routes
app.post('/admin/signup', (req, res) => {
// logic to sign up admin
const newAdmin = {
username: req.headers.username,
password: req.headers.password
};
ADMINS.push(newAdmin);
res.send(201);
});

app.post('/admin/login', (req, res) => {
// logic to log in admin
// - POST /admin/login
// Description: Authenticates an admin. It requires the admin to send username and password in the headers.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Output: { message: 'Logged in successfully' }

app.post('/admin/login', (req, res) => {
const checkCreds = checkIfAdmin(req);
console.log(checkCreds);
if(!checkCreds) {
res.send(404);
}
else {
res.json( { message: 'Logged in successfully' } );
}
});

// - POST /admin/courses
// Description: Creates a new course.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Input: Body: { title: 'course title', description: 'course description', price: 100, imageLink: 'https://linktoimage.com', published: true }
// Output: { message: 'Course created successfully', courseId: 1 }

app.post('/admin/courses', (req, res) => {
// logic to create a course
const checkCreds = checkIfAdmin(req);
if(!checkCreds) {
res.send(401);
}
const newCourse = {
id : Math.floor(Math.random() * 1000000),
title: req.body.title,
description: req.body.description,
price: req.body.price,
imageLink: req.body.imageLink,
published: req.body.published
};
COURSES.push(newCourse);
res.json( { message: 'Course created successfully', courseId: newCourse.id } );
});

// - PUT /admin/courses/:courseId
// Description: Edits an existing course. courseId in the URL path should be replaced with the ID of the course to be edited.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Input: Body { title: 'updated course title', description: 'updated course description', price: 100, imageLink: 'https://updatedlinktoimage.com', published: false }
// Output: { message: 'Course updated successfully' }

app.put('/admin/courses/:courseId', (req, res) => {
// logic to edit a course
// const checkCreds = checkIfAdmin(req);
// if(!checkCreds) {
// res.send(401);
// }
var Id = parseInt(req.params.courseId);
const checkIfCourseExist = COURSES.find((currVal) => {
return (currVal.id === Id);
});
console.log(checkIfCourseExist);
if(checkIfCourseExist === undefined) {
res.send(404);
}
else {
for (let course of COURSES) {
if (course.id === Id) {
course.title = req.body.title,
course.description = req.body.description,
course.price = req.body.price,
course.imageLink = req.body.imageLink,
course.published = req.body.published
}
}
res.json({ message: 'Course updated successfully' });
}
});

// - GET /admin/courses
// Description: Returns all the courses.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Output: { courses: [ { id: 1, title: 'course title', description: 'course description', price: 100, imageLink: 'https://linktoimage.com', published: true }, ... ] }

app.get('/admin/courses', (req, res) => {
// logic to get all courses
const checkCreds = checkIfAdmin(req);
if(!checkCreds) {
res.send(401);
}
else {
res.json(COURSES);
}
});

app.get('/admin/alladmin', (req, res) => {
res.json(ADMINS);
});

// User routes

// - POST /users/signup
// Description: Creates a new user account.
// Input: { username: 'user', password: 'pass' }
// Output: { message: 'User created successfully' }

app.post('/users/signup', (req, res) => {
// logic to sign up user
const userCreds = {
username: req.headers.username,
password: req.headers.password
};
const alreadyExist = USERS.find((currVal) => {
return (currVal.username === userCreds.username);
});

if(alreadyExist) {
res.json({ message : 'User already exist' });
}
else {
USERS.push(userCreds);
res.json({ message: 'User created successfully'});
}
});

// - POST /users/login
// Description: Authenticates a user. It requires the user to send username and password in the headers.
// Input: Headers: { 'username': 'user', 'password': 'pass' }
// Output: { message: 'Logged in successfully' }

app.post('/users/login', (req, res) => {
// logic to log in user
const userCreds = {
username: req.headers.username,
password: req.headers.password
};
const isExist = USERS.find((currVal) => {
return (currVal.username === userCreds.username && currVal.password === userCreds.password);
});

if(!isExist) {
res.json({ message : 'Authorization failed.' });
}
else {
res.json({message : 'Logged in successfully'});
}
});

// GET /users/courses
// Description: Lists all the courses.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Output: { courses: [ { id: 1, title: 'course title', description: 'course description', price: 100, imageLink: 'https://linktoimage.com', published: true }, ... ] }

app.get('/users/courses', (req, res) => {
// logic to list all courses
res.json( COURSES );
});

// - POST /users/courses/:courseId
// Description: Purchases a course. courseId in the URL path should be replaced with the ID of the course to be purchased.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Output: { message: 'Course purchased successfully' }

app.post('/users/courses/:courseId', (req, res) => {
// logic to purchase a course
const courseToPurchase = COURSES.findIndex( (currValue) => {
return (currValue.id === req.params.courseId);
});
if(courseToPurchase === -1) {
res.send(404);
}
else {
PURCHASED_COURSES.push(COURSES[index]);
res.json( { message: 'Course purchased successfully' } );
}
});

// - GET /users/purchasedCourses
// Description: Lists all the courses purchased by the user.
// Input: Headers: { 'username': 'admin', 'password': 'pass' }
// Output: { purchasedCourses: [ { id: 1, title: 'course title', description: 'course description', price: 100, imageLink: 'https://linktoimage.com', published: true }, ... ] }

app.get('/users/purchasedCourses', (req, res) => {
// logic to view purchased courses
res.json(PURCHASED_COURSES);
});

app.listen(3000, () => {
console.log('Server is listening on port 3000');
});
});
12 changes: 12 additions & 0 deletions week-3/02-course-app-easy/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions week-3/02-course-app-easy/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading