Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/idrag0/assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Drago committed Dec 28, 2023
2 parents 6bd220a + 44221a6 commit 0978008
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 190 deletions.
39 changes: 39 additions & 0 deletions week-3/03-mongo/solution/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');

// Connect to MongoDB
mongoose.connect('mongodb+srv://kirags123:[email protected]/course_selling_app');

// Define schemas
const AdminSchema = new mongoose.Schema({
// Schema definition here
username: String,
password: String
});

const UserSchema = new mongoose.Schema({
// Schema definition here
username: String,
password: String,
purchasedCourses: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}]
});

const CourseSchema = new mongoose.Schema({
// Schema definition here
title: String,
description: String,
imageLink: String,
price: Number
});

const Admin = mongoose.model('Admin', AdminSchema);
const User = mongoose.model('User', UserSchema);
const Course = mongoose.model('Course', CourseSchema);

module.exports = {
Admin,
User,
Course
}
95 changes: 0 additions & 95 deletions week-3/03-mongo/solution/index.js

This file was deleted.

25 changes: 25 additions & 0 deletions week-3/03-mongo/solution/middleware/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { Admin } = require("../db");

// Middleware for handling auth
function adminMiddleware(req, res, next) {
// Implement admin auth logic
// You need to check the headers and validate the admin from the admin DB. Check readme for the exact headers to be expected
const username = req.headers.username; // harkirat@gmail.com
const password = req.headers.password; /// 123456

Admin.findOne({
username: username,
password: password
})
.then(function(value) {
if (value) {
next();
} else {
res.status(403).json({
msg: "Admin doesnt exist"
})
}
})
}

module.exports = adminMiddleware;
24 changes: 24 additions & 0 deletions week-3/03-mongo/solution/middleware/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { User } = require("../db");

function userMiddleware(req, res, next) {
// Implement admin auth logic
// You need to check the headers and validate the admin from the admin DB. Check readme for the exact headers to be expected
const username = req.headers.username; // harkirat@gmail.com
const password = req.headers.password; /// 123456

User.findOne({
username: username,
password: password
})
.then(function(value) {
if (value) {
next();
} else {
res.status(403).json({
msg: "User doesnt exist"
})
}
})
}

module.exports = userMiddleware;
53 changes: 53 additions & 0 deletions week-3/03-mongo/solution/routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require("express");
const adminMiddleware = require("../middleware/admin");
const { Admin, Course } = require("../db");
const router = express.Router();

router.post('/signup', async (req, res) => {
// Implement admin signup logic
const username = req.body.username;
const password = req.body.password;

// check if a user with this username already exists
await Admin.create({
username: username,
password: password
})

res.json({
message: 'Admin created successfully'
})

});

router.post('/courses', adminMiddleware, async (req, res) => {
// Implement course creation logic
const title = req.body.title;
const description = req.body.description;
const imageLink = req.body.imageLink;
const price = req.body.price;
// zod
const newCourse = await Course.create({
title,
description,
imageLink,
price
})

res.json({
message: 'Course created successfully', courseId: newCourse._id
})
});

router.get('/courses', adminMiddleware, async (req, res) => {
// Implement fetching all courses logic
const response = await Course.find({});

res.json({
courses: response
})

});


module.exports = router;
66 changes: 66 additions & 0 deletions week-3/03-mongo/solution/routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { Router } = require("express");
const router = Router();
const userMiddleware = require("../middleware/user");
const { User, Course } = require("../db");
const { default: mongoose } = require("mongoose");

// User Routes
router.post('/signup', (req, res) => {
// Implement user signup logic
const username = req.body.username;
const password = req.body.password;
User.create({
username,
password
})
res.json({
message: "User created successfully"
})
});

router.get('/courses', async (req, res) => {
// Implement listing all courses logic
// Implement fetching all courses logic
const response = await Course.find({});

res.json({
courses: response
})
});

router.post('/courses/:courseId', userMiddleware, async(req, res) => {
// Implement course purchase logic
const courseId = req.params.courseId;
const username = req.headers.username;

await User.updateOne({
username: username
}, {
"$push": {
purchasedCourses: courseId
}
})
res.json({
message: "Purchase complete!"
})
});

router.get('/purchasedCourses', userMiddleware, async (req, res) => {
// Implement fetching purchased courses logic
const user = await User.findOne({
username: req.headers.username
});

console.log(user.purchasedCourses);
const courses = await Course.find({
_id: {
"$in": user.purchasedCourses
}
});

res.json({
courses: courses
})
});

module.exports = router
39 changes: 39 additions & 0 deletions week-3/04-mongo-with-jwt-auth/solution/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');

// Connect to MongoDB
mongoose.connect('mongodb+srv://kirags123:[email protected]/course_selling_app2');

// Define schemas
const AdminSchema = new mongoose.Schema({
// Schema definition here
username: String,
password: String
});

const UserSchema = new mongoose.Schema({
// Schema definition here
username: String,
password: String,
purchasedCourses: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}]
});

const CourseSchema = new mongoose.Schema({
// Schema definition here
title: String,
description: String,
imageLink: String,
price: Number
});

const Admin = mongoose.model('Admin', AdminSchema);
const User = mongoose.model('User', UserSchema);
const Course = mongoose.model('Course', CourseSchema);

module.exports = {
Admin,
User,
Course
}
Loading

0 comments on commit 0978008

Please sign in to comment.