Skip to content

Commit

Permalink
content routes added
Browse files Browse the repository at this point in the history
  • Loading branch information
ranaanjel committed Dec 2, 2024
1 parent b7776b5 commit c06f4e1
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 44 deletions.
33 changes: 24 additions & 9 deletions brainly_backend/dist/db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,32 @@ var __importStar = (this && this.__importStar) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentModel = exports.UserModel = void 0;
exports.ShareLinkModel = exports.TagModel = exports.ContentModel = exports.UserModel = void 0;
const mongoose_1 = __importStar(require("mongoose"));
let databaseURL = process.env.DATABASE_URL;
console.log(databaseURL);
mongoose_1.default.connect(databaseURL + "brainly").then(function (err) {
console.log("connected to database");
});
const userSchema = new mongoose_1.default.Schema({
const userRoutes_1 = require("../routes/userRoutes");
const UserSchema = new mongoose_1.default.Schema({
name: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
exports.UserModel = mongoose_1.default.model("User", userSchema);
const ContentSchema = new mongoose_1.Schema();
exports.UserModel = mongoose_1.default.model("User", UserSchema);
const ContentSchema = new mongoose_1.Schema({
title: { type: String, required: true },
link: { type: String, required: true },
type: { type: String, enum: userRoutes_1.contentTypes, required: true },
tag: [{ type: mongoose_1.Types.ObjectId, ref: "Tag" }],
userId: { type: mongoose_1.Types.ObjectId, ref: "User", required: true }
});
exports.ContentModel = (0, mongoose_1.model)("Content", ContentSchema);
const TagSchema = new mongoose_1.Schema({
title: { type: String, unique: true, required: true }
});
exports.TagModel = (0, mongoose_1.model)("Tag", TagSchema);
//sharing all the contents
const ShareLinkSchema = new mongoose_1.Schema({
hash: { type: String, required: true
},
userId: {
type: mongoose_1.Types.ObjectId, required: true, ref: "User"
}
});
exports.ShareLinkModel = (0, mongoose_1.model)("ShareLink", ShareLinkSchema);
6 changes: 6 additions & 0 deletions brainly_backend/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const userRoutes_1 = require("./routes/userRoutes");
const app = (0, express_1.default)();
//mongoose connection
const mongoose_1 = __importDefault(require("mongoose"));
let databaseURL = process.env.DATABASE_URL;
mongoose_1.default.connect(databaseURL + "brainly").then(function () {
console.log("connected to database");
});
app.use((0, cors_1.default)());
//body parsing - converting the bytes to json
app.use(express_1.default.json());
Expand Down
44 changes: 44 additions & 0 deletions brainly_backend/dist/middlewares/tagMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tagMiddleware = void 0;
const db_1 = require("../db/db");
const tagMiddleware = function (req, res, next) {
let { tag } = req.body;
let tagRef = [];
tag.forEach(function (tagN, index) {
return __awaiter(this, void 0, void 0, function* () {
try {
let tagDocument = yield db_1.TagModel.find({ title: tagN });
if (tagDocument.length == 0) {
throw new Error("does not exist");
}
//@ts-ignore
tagRef.push(tagDocument[0]["_id"].toString());
if (index == tag.length - 1) {
req.body.tag = tagRef;
next();
}
}
catch (err) {
let newTag = yield db_1.TagModel.create({
title: tagN
});
tagRef.push(newTag._id.toString());
if (index == tag.length - 1) {
req.body.tag = tagRef;
next();
}
}
});
});
};
exports.tagMiddleware = tagMiddleware;
54 changes: 45 additions & 9 deletions brainly_backend/dist/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.userRouter = void 0;
exports.userRouter = exports.contentTypes = void 0;
const express_1 = __importDefault(require("express"));
const validMiddleware_1 = require("../middlewares/validMiddleware");
const userRouter = express_1.default.Router();
Expand All @@ -21,6 +21,7 @@ const db_1 = require("../db/db");
const bcrypt_1 = __importDefault(require("bcrypt"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const authMiddlewares_1 = require("../middlewares/authMiddlewares");
const tagMiddleware_1 = require("../middlewares/tagMiddleware");
const jwtSecret = String(process.env.JWT_SECRET);
userRouter.get("/", function (req, res) {
res.redirect("/user/content");
Expand Down Expand Up @@ -81,19 +82,54 @@ userRouter.post("/signin", function (req, res) {
});
});
//content related
userRouter.get("/content", authMiddlewares_1.authMiddleware, function (req, res) {
res.json({
message: "content get"
var contentTypes;
(function (contentTypes) {
contentTypes["image"] = "image";
contentTypes["video"] = "video";
contentTypes["article"] = "article";
contentTypes["audio"] = "audio";
})(contentTypes || (exports.contentTypes = contentTypes = {}));
userRouter.post("/content", authMiddlewares_1.authMiddleware, tagMiddleware_1.tagMiddleware, function (req, res) {
return __awaiter(this, void 0, void 0, function* () {
let { link, title, tag, type } = req.body;
console.log(link, title, tag, type);
// getting the reference for each tag if not present creating one.
try {
console.log("before tag check");
const content = yield db_1.ContentModel.create({
link, title, type, tag, userId: req.userId
});
console.log(content);
res.json({
content
});
}
catch (err) {
console.log(err);
res.status(500).json({
message: "server not able to store the content"
});
}
});
});
userRouter.post("/content", authMiddlewares_1.authMiddleware, function (req, res) {
res.json({
message: "content post"
userRouter.get("/content", authMiddlewares_1.authMiddleware, function (req, res) {
return __awaiter(this, void 0, void 0, function* () {
const userId = req.userId;
const contents = yield db_1.ContentModel.find({
userId
});
res.json({
contents
});
});
});
userRouter.delete("/content", authMiddlewares_1.authMiddleware, function (req, res) {
res.json({
message: "content delete"
return __awaiter(this, void 0, void 0, function* () {
let contentId = req.body.contentId;
yield db_1.ContentModel.deleteOne({ _id: contentId, userId: req.userId });
res.json({
message: "deleted the content"
});
});
});
//share the content i.e visible to others
Expand Down
42 changes: 29 additions & 13 deletions brainly_backend/src/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import mongoose,{model, Schema} from "mongoose";
import mongoose,{model, Schema, Types} from "mongoose";
import {contentTypes} from "../routes/userRoutes"

let databaseURL = process.env.DATABASE_URL

console.log(databaseURL)

mongoose.connect(databaseURL + "brainly").then(function(err) {
console.log("connected to database")
})

const userSchema = new mongoose.Schema({
const UserSchema = new mongoose.Schema({
name:{type:String, required:true, unique:true},
password:{type:String, required:true}
})

export const UserModel = mongoose.model("User", userSchema)
export const UserModel = mongoose.model("User", UserSchema)

const ContentSchema = new Schema({
title:{type:String, required:true},
link:{type:String, required:true},
type:{type:String, enum:contentTypes, required:true},
tag:[{type:Types.ObjectId, ref:"Tag"}],
userId:{type:Types.ObjectId, ref:"User", required:true}
});
export const ContentModel = model("Content", ContentSchema);

const TagSchema = new Schema({
title:{type:String, unique:true, required:true}
})
export const TagModel = model("Tag", TagSchema);


const ContentSchema = new Schema();
export const ContentModel = model("Content", ContentSchema);
//sharing all the contents
const ShareLinkSchema = new Schema({
hash:{type:String, required
:true
},
userId:{
type:Types.ObjectId, required:true, ref:"User"
}
})
export const ShareLinkModel = model("ShareLink", ShareLinkSchema)
8 changes: 7 additions & 1 deletion brainly_backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ dotenv.config()
import express, { NextFunction, Request, Response,ErrorRequestHandler } from "express";
import cors from "cors"
import { userRouter } from "./routes/userRoutes";

const app = express();

//mongoose connection
import mongoose from "mongoose";
let databaseURL = process.env.DATABASE_URL

mongoose.connect(databaseURL + "brainly").then( function () {
console.log("connected to database")
})
app.use(cors())
//body parsing - converting the bytes to json
app.use(express.json())
Expand Down
35 changes: 35 additions & 0 deletions brainly_backend/src/middlewares/tagMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Request, Response, NextFunction } from "express";
import mongoose, { Types} from "mongoose";
import { TagModel } from "../db/db";

export const tagMiddleware = function (req:Request, res:Response, next:NextFunction) {
let {tag}:{tag:string[]} = req.body;

let tagRef:string[] = []

tag.forEach(async function (tagN,index) {
try {

let tagDocument = await TagModel.find({title:tagN});
if(tagDocument.length == 0) {
throw new Error("does not exist")
}
//@ts-ignore
tagRef.push(tagDocument[0]["_id"].toString())
if(index == tag.length-1) {
req.body.tag = tagRef;
next()
}
}
catch(err) {
let newTag = await TagModel.create({
title:tagN
})
tagRef.push(newTag._id.toString())
if(index == tag.length-1) {
req.body.tag = tagRef;
next()
}
}
})
}
Loading

0 comments on commit c06f4e1

Please sign in to comment.