Skip to content

Commit

Permalink
Added posts complete
Browse files Browse the repository at this point in the history
  • Loading branch information
tanish35 committed Aug 4, 2024
1 parent 2b5ac1e commit 72b7366
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 159 deletions.
57 changes: 57 additions & 0 deletions backend/dist/controllers/postController.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
"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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPost = exports.getCommunities = void 0;
const express_async_handler_1 = __importDefault(require("express-async-handler"));
const prisma_1 = __importDefault(require("../lib/prisma"));
// @ts-ignore
const getCommunities = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
// @ts-ignore
const user_id = req.user.user_id;
// return res.status(200).json({ message: "Communities fetched" });
const communities = yield prisma_1.default.userCourse.findMany({
where: {
user_id: user_id,
},
select: {
college_id: true,
},
distinct: ["college_id"],
});
return res.status(200).json({ communities });
}));
exports.getCommunities = getCommunities;
// @ts-ignore
const createPost = (0, express_async_handler_1.default)((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
const { title, content, collegeId } = req.body;
// @ts-ignore
const user_id = req.user.user_id;
if (!title || !content || !collegeId) {
return res.status(400).json({ message: "Required fields are missing" });
}
const user = yield prisma_1.default.user.findUnique({
where: { user_id },
});
if (!user) {
return res.status(404).json({ message: "User not recognized" });
}
const post = yield prisma_1.default.post.create({
// @ts-ignore
data: {
title,
content,
user_id,
},
});
return res.status(201).json({ post });
}));
exports.createPost = createPost;
26 changes: 14 additions & 12 deletions backend/dist/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const bcrypt_1 = __importDefault(require("bcrypt"));
const sendMail_1 = __importDefault(require("../mail/sendMail"));
const academic_email_verifier_1 = require("academic-email-verifier");
const checkAcademic_1 = __importDefault(require("../mail/checkAcademic"));
// @ts-ignore
const registerUser = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { email, name, password, collegeName, courseName, isOnline, location } = req.body;
const hashedPassword = yield bcrypt_1.default.hash(password, 8);
Expand All @@ -44,9 +45,8 @@ const registerUser = (0, express_async_handler_1.default)((req, res) => __awaite
isCollegeEmail = yield academic_email_verifier_1.Verifier.isAcademic(email);
}
if (isCollegeEmail == true) {
if (!collegeName || !courseName || !isOnline || !location) {
res.status(400).json({ message: "Please provide all fields" });
return;
if (!courseName || !collegeName || !location || isOnline === undefined) {
return res.status(400).json({ message: "Please provide all fields" });
}
let college = yield prisma_1.default.college.findFirst({
where: {
Expand Down Expand Up @@ -93,6 +93,7 @@ const registerUser = (0, express_async_handler_1.default)((req, res) => __awaite
data: {
user_id: user.user_id,
course_id,
college_id,
},
});
const exp = Date.now() + 1000 * 60 * 5;
Expand Down Expand Up @@ -212,13 +213,13 @@ const getCurrentUserDetails = (0, express_async_handler_1.default)((req, res) =>
name: true,
},
},
}
}
},
},
},
},
reviews: true,
chatRooms: true,
}
},
});
if (!user) {
res.status(404).json({ message: "User not found" });
Expand Down Expand Up @@ -246,13 +247,13 @@ const getUserDetailsById = (0, express_async_handler_1.default)((req, res) => __
name: true,
},
},
}
}
},
},
},
},
reviews: true,
chatRooms: true,
}
},
});
if (!user) {
res.status(404).json({ message: "User not found" });
Expand All @@ -261,11 +262,11 @@ const getUserDetailsById = (0, express_async_handler_1.default)((req, res) => __
res.status(200).json(user);
}));
exports.getUserDetailsById = getUserDetailsById;
// @ts-ignore
const addCourseToUser = (0, express_async_handler_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { courseName, collegeName, isOnline, location } = req.body;
if (!courseName || !collegeName || !isOnline || !location) {
res.status(400).json(req.body);
return;
if (!courseName || !collegeName || !location || isOnline === undefined) {
return res.status(400).json({ message: "Please provide all fields" });
}
// @ts-ignore
const userId = req.user.user_id;
Expand Down Expand Up @@ -306,6 +307,7 @@ const addCourseToUser = (0, express_async_handler_1.default)((req, res) => __awa
data: {
user_id: userId,
course_id,
college_id,
},
});
res.status(201).json({ message: "Course added to user" });
Expand Down
6 changes: 5 additions & 1 deletion backend/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const mRuote_1 = __importDefault(require("./routes/mRuote"));
const chatRoutes_1 = __importDefault(require("./routes/chatRoutes"));
const reviewRoutes_1 = __importDefault(require("./routes/reviewRoutes"));
const ratingRoute_1 = __importDefault(require("./routes/ratingRoute"));
const postsRoutes_1 = __importDefault(require("./routes/postsRoutes"));
// import { getCommunities } from "./controllers/postController";
const app = (0, express_1.default)();
app.use(express_1.default.json());
const corsOptions = {
Expand All @@ -29,7 +31,9 @@ app.use("/api/user", userRoutes_1.default);
app.use("/api/admin", mRuote_1.default);
app.use("/api/review", reviewRoutes_1.default);
app.use("/api/rating", ratingRoute_1.default);
app.use('/api/chat', chatRoutes_1.default); // Use the chat routes
app.use("/api/chat", chatRoutes_1.default); // Use the chat routes
app.use("/api/post", postsRoutes_1.default);
// app.get("/api/post/communities", getCommunities);
app.get("/", (req, res) => {
res.send("Backend is running");
});
Expand Down
5 changes: 2 additions & 3 deletions backend/dist/middleware/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkModeration = exports.checkModerationForString = void 0;
exports.checkModerationForString = checkModerationForString;
exports.checkModeration = checkModeration;
const promises_1 = __importDefault(require("fs/promises"));
function binarySearch(items, value) {
var startIndex = 0, stopIndex = items.length - 1, middle = Math.floor((stopIndex + startIndex) / 2);
Expand Down Expand Up @@ -51,7 +52,6 @@ function checkModerationForString(content) {
}
});
}
exports.checkModerationForString = checkModerationForString;
function checkModeration(req, res, next) {
const fs = require("fs");
//for some reason it is taking backend folder as the root folder
Expand Down Expand Up @@ -79,4 +79,3 @@ function checkModeration(req, res, next) {
}
});
}
exports.checkModeration = checkModeration;
12 changes: 12 additions & 0 deletions backend/dist/routes/postsRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const postController_1 = require("../controllers/postController");
const checkAuth_1 = __importDefault(require("../middleware/checkAuth"));
const postsRoutes = express_1.default.Router();
postsRoutes.get("/communities", checkAuth_1.default, postController_1.getCommunities);
postsRoutes.post("/create", checkAuth_1.default, postController_1.createPost);
exports.default = postsRoutes;
5 changes: 0 additions & 5 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ model College {
courses Course[]
reviews Review[]
Post Post[]
Comment Comment[]
UserCourse UserCourse[]
}

Expand Down Expand Up @@ -88,7 +87,6 @@ model Post {
post_id String @id @default(cuid())
user_id String
college_id String
course_id String
title String
content String
likes Int @default(0)
Expand All @@ -103,12 +101,9 @@ model Comment {
comment_id String @id @default(cuid())
post_id String
user_id String
college_id String
course_id String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
User User @relation(fields: [user_id], references: [user_id])
College College @relation(fields: [college_id], references: [college_id])
Post Post @relation(fields: [post_id], references: [post_id])
}
58 changes: 58 additions & 0 deletions backend/src/controllers/postController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@ import asyncHandler from "express-async-handler";
import { Request, Response } from "express";
import prisma from "../lib/prisma";

// @ts-ignore
const getCommunities = asyncHandler(async (req: Request, res: Response) => {
// @ts-ignore
const user_id = req.user.user_id;
// const user_id = "clzfffeey0000e6hx5j16b96c";
const communities = await prisma.userCourse.findMany({
where: {
user_id: user_id,
},
select: {
college_id: true,
College: {
select: {
name: true,
},
},
},
distinct: ["college_id"],
});
const communityIds = communities.map((community) => community.college_id);
if (communityIds.length === 0) {
return res.status(200).json({ communityIds: [] });
}
let college = [];
for (let i = 0; i < communityIds.length; i++) {
college[i] = await prisma.college.findUnique({
where: {
college_id: communityIds[i],
},
select: {
college_id: true,
name: true,
},
});
}
return res.status(200).json({ college });
});

// @ts-ignore
Expand All @@ -31,8 +64,33 @@ const createPost = asyncHandler(async (req: Request, res: Response, next) => {
title,
content,
user_id,
college_id: collegeId,
},
});

return res.status(201).json({ post });
});

// @ts-ignore
const fetchPosts = asyncHandler(async (req: Request, res: Response) => {
const posts = await prisma.post.findMany({
orderBy: {
createdAt: "desc",
},
select: {
post_id: true,
title: true,
content: true,
likes: true,
College: {
select: {
name: true,
},
},
},
take: 10,
});
return res.status(200).json({ posts });
});

export { getCommunities, createPost, fetchPosts };
9 changes: 8 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import chatRoutes from "./routes/chatRoutes";

import reviewRoutes from "./routes/reviewRoutes";
import ratingRoutes from "./routes/ratingRoute";
import postsRoutes from "./routes/postsRoutes";

// import { getCommunities } from "./controllers/postController";

const app = express();
app.use(express.json());
const corsOptions = {
origin: [
"http://localhost:3001",
"https://app-statuscode1.wedevelopers.online",
"http://localhost:5173",
],
credentials: true,
};
Expand All @@ -30,7 +34,10 @@ app.use("/api/admin", moderationRouter);

app.use("/api/review", reviewRoutes);
app.use("/api/rating", ratingRoutes);
app.use('/api/chat', chatRoutes); // Use the chat routes
app.use("/api/chat", chatRoutes); // Use the chat routes
app.use("/api/post", postsRoutes);

// app.get("/api/post/communities", getCommunities);

app.get("/", (req: Request, res: Response) => {
res.send("Backend is running");
Expand Down
15 changes: 15 additions & 0 deletions backend/src/routes/postsRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from "express";
import {
getCommunities,
createPost,
fetchPosts,
} from "../controllers/postController";
import checkAuth from "../middleware/checkAuth";

const postsRoutes = express.Router();

postsRoutes.get("/communities", checkAuth, getCommunities);
postsRoutes.post("/create", checkAuth, createPost);
postsRoutes.get("/fetch", checkAuth, fetchPosts);

export default postsRoutes;
Loading

0 comments on commit 72b7366

Please sign in to comment.