-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from polyglot-edu/20-new-execution-api-concept
20 new execution api concept
- Loading branch information
Showing
6 changed files
with
202 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Request, Response } from "express"; | ||
import User from "../models/user.model"; | ||
import Course from "../models/course.model"; | ||
import Flow from "../models/flow.model"; | ||
|
||
export async function createCourse(req: Request, res: Response) { | ||
const userId = req.user?._id; | ||
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"); | ||
} | ||
|
||
if (await Course.findOne({ title: title })) { | ||
return res.status(400).send("Course already exists"); | ||
} | ||
/* IMPLEMENT THIS LATER | ||
let flowsNotFound: string[] = []; | ||
console.log(flowsId); | ||
if(flowsId.length > 0){ | ||
for (const flow of flowsId) { | ||
const dbflow = await Flow.findOne({ _id: flow }); | ||
if (!dbflow) { | ||
flowsNotFound.push(flow); | ||
} | ||
} | ||
} | ||
if (flowsNotFound.length > 0) { | ||
return res.status(404).send("Flows " + flowsNotFound.join(", ") + " not found"); | ||
} | ||
*/ | ||
const course = new Course({ | ||
title: title, | ||
description: description, | ||
author: userId, | ||
}); | ||
|
||
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(courseRes); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).send; | ||
} | ||
} | ||
|
||
export async function deleteCourse(req: Request, res: Response) { | ||
const courseId = req.params.id; | ||
const userId = req.user?._id; | ||
try { | ||
if (!userId) { | ||
return res.status(400).send("userId is required"); | ||
} | ||
|
||
const user = await User.findById(userId); | ||
if (!user) { | ||
return res.status(404).send("User not found"); | ||
} | ||
|
||
if (!courseId) { | ||
return res.status(404).send("courseId is required"); | ||
} | ||
|
||
const dbcourse = await Course.findById(courseId); | ||
if (!dbcourse) { | ||
return res.status(404).send("Course not found"); | ||
} | ||
|
||
if (dbcourse.author !== userId && userId != "admin") { | ||
return res.status(403).send("You are not the author of this course"); | ||
} | ||
|
||
await Course.deleteOne({ _id: courseId }); | ||
|
||
return res.status(204).send(); | ||
} catch (err) { | ||
res.status(500).send; | ||
} | ||
} | ||
|
||
export async function getCourses(req: Request, res: Response) { | ||
try { | ||
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; | ||
} | ||
|
||
const courses = await Course.find(query) | ||
.populate("author") | ||
.populate("flows"); | ||
return res.json(courses); | ||
} catch (err) { | ||
console.error(err); | ||
return res.status(500).send; | ||
} | ||
} |
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,30 @@ | ||
import mongoose, { Document } from "mongoose"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import validator from "validator"; | ||
|
||
export type CourseDocument = Document & { | ||
title: string; | ||
description: string; | ||
author: string; | ||
flows: string[]; | ||
}; | ||
|
||
const courseSchema = new mongoose.Schema<CourseDocument>({ | ||
_id: { | ||
type: String, | ||
required: true, | ||
default: () => uuidv4(), | ||
validate: { | ||
validator: (id: string) => validator.isUUID(id), | ||
message: "Invalid UUID-v4", | ||
}, | ||
}, | ||
title: { type: String, required: true }, | ||
description: { type: String, required: true }, | ||
author: { type: String, required: true, ref: "User" }, | ||
flows: [{ type: String, ref: "Flow" }], | ||
}); | ||
|
||
const Course = mongoose.model<CourseDocument>("Course", courseSchema); | ||
|
||
export default Course; |
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,15 @@ | ||
import express from "express"; | ||
import { checkAuth } from "../middlewares/auth.middleware"; | ||
import * as CourseController from "../controllers/course.controllers"; | ||
|
||
const router = express.Router(); | ||
// cambiare tutto con flow | ||
|
||
router.route("/:id").delete(checkAuth, CourseController.deleteCourse); | ||
router | ||
.route("/") | ||
.post(checkAuth, CourseController.createCourse) | ||
.get(checkAuth, CourseController.getCourses); | ||
|
||
// get enrolled courses | ||
export default router; |
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