Skip to content

Commit

Permalink
Merge pull request #9 from sensebox/update/tutorial-model
Browse files Browse the repository at this point in the history
Update/tutorial model
  • Loading branch information
mariopesch authored Mar 1, 2022
2 parents 47e6dd2 + 346293d commit ae3cfc8
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 26 deletions.
10 changes: 10 additions & 0 deletions models/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const TutorialSchema = new mongoose.Schema(
type: String,
required: true,
},
public: {
type: Boolean,
required: true,
default: false,
},
review: {
type: Boolean,
required: false,
default: false,
},
difficulty: {
type: Number,
required: true,
Expand Down
2 changes: 1 addition & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const UserSchema = new mongoose.Schema(
type: String,
enum: ["admin", "creator", "user"],
required: true,
default: "user",
default: "creator",
},
},
{
Expand Down
69 changes: 69 additions & 0 deletions routes/tutorial/getAllTutorials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// jshint esversion: 8
// jshint node: true
"use strict";

const express = require("express");
const mongoose = require("mongoose");

const Tutorial = require("../../models/tutorial");

/**
* @api {get} /tutorial Get tutorials
* @apiName getTutorials
* @apiDescription Get all tutorials as admin.
* @apiGroup Tutorial
*
* @apiSuccess (Success 200) {String} message `Tutorials found successfully.`
* @apiSuccess (Success 200) {Object} tutorials `[
{
"_id": "5fcf7caabd63e209146d3e85",
"creator": "[email protected]",
"title": "Test",
"steps": [
{
"_id": "5fcf7caabd63e209146d3e87",
"type": "instruction",
"headline": "Einführung",
"text": "In diesem Tutorial lernst du wie man die senseBox mit dem Internet verbindest.",
"hardware": [
"senseboxmcu",
"wifi-bee"
]
},
{
"_id": "5fcf7caabd63e209146d3e88",
"type": "task",
"headline": "Aufgabe 1",
"text": "Stelle eine WLAN-Verbindung mit einem beliebigen Netzwerk her.",
"xml": "<xml xmlns='https://developers.google.com/blockly/xml'><block type='arduino_functions' id='QWW|$jB8+*EL;}|#uA' deletable='false' x='27' y='16'><statement name='SETUP_FUNC'><block type='sensebox_wifi' id='W}P2Y^g,muH@]|@anou}'><field name='SSID'>SSID</field><field name='Password'>Password</field></block></statement></block></xml>"
}
],
"createdAt": "2020-12-08T13:16:26.722Z",
"updatedAt": "2020-12-13T19:25:40.529Z",
"__v": 0
}
]`
*
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const getAllTutorials = async function (req, res) {
try {
if (req.user.role === "admin") {
var result = await Tutorial.find({});
return res.status(200).send({
message: "Tutorials found successfully.",
tutorials: result,
});
} else {
return res.status(403).send({
message: "No permission getting all the tutorial.",
});
}
} catch (err) {
return res.status(500).send(err);
}
};

module.exports = {
getAllTutorials,
};
23 changes: 11 additions & 12 deletions routes/tutorial/getTutorials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// jshint node: true
"use strict";

const express = require('express');
const mongoose = require('mongoose');
const express = require("express");
const mongoose = require("mongoose");

const Tutorial = require('../../models/tutorial');
const Tutorial = require("../../models/tutorial");

/**
* @api {get} /tutorial Get tutorials
* @apiName getTutorials
* @apiDescription Get all tutorials.
* @apiDescription Get all tutorials which are public.
* @apiGroup Tutorial
*
* @apiSuccess (Success 200) {String} message `Tutorials found successfully.`
Expand Down Expand Up @@ -46,19 +46,18 @@ const Tutorial = require('../../models/tutorial');
*
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const getTutorials = async function(req, res){
try{
var result = await Tutorial.find({});
const getTutorials = async function (req, res) {
try {
var result = await Tutorial.find({ public: true });
return res.status(200).send({
message: 'Tutorials found successfully.',
tutorials: result
message: "All public Tutorials found successfully.",
tutorials: result,
});
}
catch(err){
} catch (err) {
return res.status(500).send(err);
}
};

module.exports = {
getTutorials
getTutorials,
};
65 changes: 65 additions & 0 deletions routes/tutorial/getUserTutorials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// jshint esversion: 8
// jshint node: true
"use strict";

const express = require("express");
const mongoose = require("mongoose");

const Tutorial = require("../../models/tutorial");

/**
* @api {get} /tutorial Get tutorials
* @apiName getTutorials
* @apiDescription Get all tutorials by one user.
* @apiGroup Tutorial
*
* @apiSuccess (Success 200) {String} message `Tutorials found successfully.`
* @apiSuccess (Success 200) {Object} tutorials `[
{
"_id": "5fcf7caabd63e209146d3e85",
"creator": "[email protected]",
"title": "Test",
"steps": [
{
"_id": "5fcf7caabd63e209146d3e87",
"type": "instruction",
"headline": "Einführung",
"text": "In diesem Tutorial lernst du wie man die senseBox mit dem Internet verbindest.",
"hardware": [
"senseboxmcu",
"wifi-bee"
]
},
{
"_id": "5fcf7caabd63e209146d3e88",
"type": "task",
"headline": "Aufgabe 1",
"text": "Stelle eine WLAN-Verbindung mit einem beliebigen Netzwerk her.",
"xml": "<xml xmlns='https://developers.google.com/blockly/xml'><block type='arduino_functions' id='QWW|$jB8+*EL;}|#uA' deletable='false' x='27' y='16'><statement name='SETUP_FUNC'><block type='sensebox_wifi' id='W}P2Y^g,muH@]|@anou}'><field name='SSID'>SSID</field><field name='Password'>Password</field></block></statement></block></xml>"
}
],
"createdAt": "2020-12-08T13:16:26.722Z",
"updatedAt": "2020-12-13T19:25:40.529Z",
"__v": 0
}
]`
*
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const getUserTutorials = async function (req, res) {
try {
var userTutorials = await Tutorial.find({ creator: req.user.email });
var publicTutorials = await Tutorial.find({ public: true });
var result = userTutorials.concat(publicTutorials);
return res.status(200).send({
message: "All User Tutorials found successfully.",
tutorials: result,
});
} catch (err) {
return res.status(500).send(err);
}
};

module.exports = {
getUserTutorials,
};
42 changes: 29 additions & 13 deletions routes/tutorial/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,41 @@
// jshint node: true
"use strict";

var express = require('express');
var express = require("express");
var TutorialRouter = express.Router();

const { userAuthorization } = require('../../helper/userAuthorization');
const { upload } = require('../../helper/imageUpload');
const { userAuthorization } = require("../../helper/userAuthorization");
const { upload } = require("../../helper/imageUpload");

TutorialRouter.route('/')
.post(userAuthorization, upload.any(), require('./postTutorial').postTutorial);
TutorialRouter.route("/").post(
userAuthorization,
upload.any(),
require("./postTutorial").postTutorial
);

TutorialRouter.route('/:tutorialId')
.put(userAuthorization, upload.any(), require('./putTutorial').putTutorial);
TutorialRouter.route("/:tutorialId").put(
userAuthorization,
upload.any(),
require("./putTutorial").putTutorial
);

TutorialRouter.route('/:tutorialId')
.delete(userAuthorization, require('./deleteTutorial').deleteTutorial);
TutorialRouter.route("/:tutorialId").delete(
userAuthorization,
require("./deleteTutorial").deleteTutorial
);

TutorialRouter.route('/')
.get(require('./getTutorials').getTutorials);
TutorialRouter.route("/").get(require("./getTutorials").getTutorials);

TutorialRouter.route('/:tutorialId')
.get(require('./getTutorial').getTutorial);
TutorialRouter.route("/getAllTutorials").get(
userAuthorization,
require("./getAllTutorials").getAllTutorials
);

TutorialRouter.route("/getUserTutorials").get(
userAuthorization,
require("./getUserTutorials").getUserTutorials
);

TutorialRouter.route("/:tutorialId").get(require("./getTutorial").getTutorial);

module.exports = TutorialRouter;
2 changes: 2 additions & 0 deletions routes/tutorial/postTutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const postTutorial = async function (req, res) {
creator: req.user.email,
title: req.body.title,
difficulty: req.body.difficulty,
public: req.body.public,
review: req.body.review,
steps: req.body.steps,
};
// storing existing images in mongoDB
Expand Down
2 changes: 2 additions & 0 deletions routes/tutorial/putTutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const putTutorial = async function (req, res) {
updatedTutorial.title = req.body.title || oldTutorial.title;
updatedTutorial.difficulty =
req.body.difficulty || oldTutorial.difficulty;
updatedTutorial.public = req.body.public || oldTutorial.public;
updatedTutorial.review = req.body.review || oldTutorial.review;
updatedTutorial.steps = req.body.steps || oldTutorial.steps;
// ensure that the requirement is not related to the tutorial itself
if (updatedTutorial.steps[0].requirements) {
Expand Down

0 comments on commit ae3cfc8

Please sign in to comment.