Skip to content

Commit

Permalink
fix course api execution bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaog committed May 31, 2024
1 parent fb208ea commit 8c8beaa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/controllers/course.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import Flow from "../models/flow.model";

export async function createCourse(req: Request, res: Response) {
const userId = req.user?._id;
const { title, description, flows } = req.body;
const { title, description, flowsId } = req.body;

try {
if (!userId) {
return res.status(400).send("userId is required");
}

if(!title) res.status(400).send("title is required");


if(!description) res.status(400).send("description is required");

const user = await User.findById(userId);
if (!user) {
return res.status(404).send("User not found");
Expand All @@ -22,9 +27,9 @@ export async function createCourse(req: Request, res: Response) {
}
/* IMPLEMENT THIS LATER
let flowsNotFound: string[] = [];
console.log(flows);
if(flows.length > 0){
for (const flow of flows) {
console.log(flowsId);
if(flowsId.length > 0){
for (const flow of flowsId) {
const dbflow = await Flow.findOne({ _id: flow });
if (!dbflow) {
flowsNotFound.push(flow);
Expand All @@ -42,12 +47,15 @@ export async function createCourse(req: Request, res: Response) {
author: userId,
});

if (flows) for (const flow of flows) course.flows.push(flow);
if (flowsId) for (const flow of flowsId) if (flow!=null) course.flows.push(flow);

console.log(course);
await course.save();
const courseRes = await Course.find({title: course.title})
.populate("author")
.populate("flows");

return res.status(201).json(course);
return res.status(201).json(courseRes);
} catch (err) {
console.error(err);
res.status(500).send;
Expand Down Expand Up @@ -93,7 +101,7 @@ export async function getCourses(req: Request, res: Response) {
const q = req.query?.q?.toString();
const me = req.query?.me?.toString();
const query: any = q ? { title: { $regex: q, $options: "i" } } : {};

if (me) {
query.author = req.user?._id;
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import flowRouter from "./flows.routes";
import courseRouter from "./course.routes";
import executionRouter from "./execution.routes";
import userRouter from "./user.routes";
import searchRouter from "./search.routes";
Expand All @@ -10,6 +11,7 @@ import conceptRouter from "./concept.routes";
const router = express.Router();

router.use("/api/flows", flowRouter);
router.use("/api/course", courseRouter);
router.use("/api/execution", executionRouter);
router.use("/api/user", userRouter);
router.use("/api/search", searchRouter);
Expand Down

0 comments on commit 8c8beaa

Please sign in to comment.