-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
264 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.