Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ var galleryRouter = require('./routes/gallery/index');
var projectRouter = require('./routes/project/index');
var userRouter = require('./routes/user/index');
var uploadRouter = require('./routes/upload/index');
const ArduinoRouter = require('./routes/arduinoExamples');

api.use('/tutorial', tutorialRouter);
api.use('/share', shareRouter);
api.use('/gallery', galleryRouter);
api.use('/project', projectRouter);
api.use('/user', userRouter);
api.use('/upload', uploadRouter);
api.use('/arduino', ArduinoRouter);

// catch 404 and forward to error handler
api.use(function(req, res, next) {
Expand Down
33 changes: 33 additions & 0 deletions models/arduinoExamples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// jshint esversion: 6
// jshint node: true
"use strict";

const mongoose = require('mongoose');

const ArduinoExamplesSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
creator: {
type: String,
ref: 'User',
required: true
},
board: {
type: String,
required: true,
},
code: {
type: String
}
},{
timestamps: true
});


module.exports = mongoose.model('ArduinoExamples', ArduinoExamplesSchema);
9 changes: 8 additions & 1 deletion models/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ const GallerySchema = new mongoose.Schema({
ref: 'User',
required: true
},
board: {
type: String,
required: true,
},
xml: {
type: String
}
},
type: {
type: String,
},
},{
timestamps: true
});
Expand Down
4 changes: 4 additions & 0 deletions models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const ProjectSchema = new mongoose.Schema({
ref: 'User',
required: true
},
board: {
type: String,
required: true,
},
xml: {
type: String
}
Expand Down
4 changes: 4 additions & 0 deletions models/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ShareSchema = new mongoose.Schema({
type: String,
required: true
},
board: {
type: String,
required: true,
},
expiresAt: {
type: Date,
required: true,
Expand Down
9 changes: 9 additions & 0 deletions models/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ const TutorialSchema = new mongoose.Schema(
type: Number,
required: true,
},
board: {
type: String,
required: false,
},
language: {
type: String,
enum: ["en", "de"],
default: "en",
},
steps: [
{
type: StepSchema,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-ardublockly-backend",
"version": "0.0.0",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
Expand Down
56 changes: 56 additions & 0 deletions routes/arduinoExamples/deleteExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// jshint esversion: 8
// jshint node: true
"use strict";

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

const arduinoExamples = require('../../models/arduinoExamples');

/**
* @api {delete} /arduino/:exampleId Delete gallery
* @apiName deleteExample
* @apiDescription Delete a specific example.
* @apiGroup ArduinoExamples
*
* @apiHeader {String} Authorization allows to send a valid JSON Web Token along with this request with `Bearer` prefix.
* @apiHeaderExample {String} Authorization Header Example
* Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlMTk5OTEwY2QxMDgyMjA3Y2Y1ZGM2ZiIsImlhdCI6MTU3ODg0NDEwOSwiZXhwIjoxNTc4ODUwMTA5fQ.D4NKx6uT3J329j7JrPst6p02d311u7AsXVCUEyvoiTo
*
* @apiParam {ObjectId} exampleId the ID of the gallery you are referring to
*
* @apiSuccess (Success 200) {String} message `Example deleted successfully.`
*
* @apiError (On error) {Object} 403 `{"message": No permission deleting the gallery project."}`
* @apiError (On error) {Object} 404 `{"message": Example not found."}`
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const deleteExample = async function(req, res){
try{
var result = await arduinoExamples.findById(req.params.exampleId);
var owner = req.user.email;
if(owner === result.creator || req.user.role === 'admin'){
var example = await arduinoExamples.deleteOne({_id: req.params.exampleId});
if(example && example.deletedCount > 0){
return res.status(200).send({
message: 'Arduino Example deleted successfully.',
});
}
return res.status(404).send({
message: 'Example not found.',
});
}
else {
return res.status(403).send({
message: 'No permission deleting the example.',
});
}
}
catch(err){
return res.status(500).send(err);
}
};

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

const express = require('express');
const mongoose = require('mongoose');
const arduinoExamples = require('../../models/arduinoExamples');


/**
* @api {get} /gallery/:galleryId Get gallery
* @apiName getGallery
* @apiDescription Get a specific gallery.
* @apiGroup Gallery
*
* @apiHeader {String} Authorization allows to send a valid JSON Web Token along with this request with `Bearer` prefix.
* @apiHeaderExample {String} Authorization Header Example
* Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlMTk5OTEwY2QxMDgyMjA3Y2Y1ZGM2ZiIsImlhdCI6MTU3ODg0NDEwOSwiZXhwIjoxNTc4ODUwMTA5fQ.D4NKx6uT3J329j7JrPst6p02d311u7AsXVCUEyvoiTo
*
* @apiParam {ObjectId} galleryId the ID of the gallery you are referring to
*
* @apiSuccess (Success 200) {String} message `Gallery found successfully.`
* @apiSuccess (Success 200) {Object} gallery `{
"_id": "5fd8a66cb40982332c400bc4",
"title": "flimsy-cougar",
"description": "Beschreibung",
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" deletable=\"false\" x=\"27\" y=\"16\"></block>\n</xml>",
"creator": "em@il.de",
"createdAt": "2020-12-15T12:05:00.662Z",
"updatedAt": "2020-12-15T12:05:00.662Z",
"__v": 0
}`
*
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const getExample = async function(req, res){
try{
var id = req.params.exampleId;
var result = await arduinoExamples.findById(id);
// TODO: .populate({path: 'creator', model: 'User', select: 'name'})
return res.status(200).send({
message: 'Example found successfully.',
example: result
});
}
catch(err){
return res.status(500).send(err);
}
};

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

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

const arduinoExamples = require('../../models/arduinoExamples');

/**
* @api {get} /arduino/ Get Arduino Examples
* @apiName getGalleries
* @apiDescription Get all galleries.
* @apiGroup Gallery
*
* @apiHeader {String} Authorization allows to send a valid JSON Web Token along with this request with `Bearer` prefix.
* @apiHeaderExample {String} Authorization Header Example
* Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlMTk5OTEwY2QxMDgyMjA3Y2Y1ZGM2ZiIsImlhdCI6MTU3ODg0NDEwOSwiZXhwIjoxNTc4ODUwMTA5fQ.D4NKx6uT3J329j7JrPst6p02d311u7AsXVCUEyvoiTo
*
* @apiSuccess (Success 200) {String} message `Galleries found successfully.`
* @apiSuccess (Success 200) {Object} galleries `[
{
"_id": "5fd8a66cb40982332c400bc4",
"title": "flimsy-cougar",
"description": "Beschreibung",
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" deletable=\"false\" x=\"27\" y=\"16\"></block>\n</xml>",
"creator": "em@il.de",
"createdAt": "2020-12-15T12:05:00.662Z",
"updatedAt": "2020-12-15T12:05:00.662Z",
"__v": 0
}
]`
*
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const getArduinoExamples = async function(req, res){
try{
var result = await arduinoExamples.find({});
return res.status(200).send({
message: 'Arduino Examples found successfully.',
arduinoExamples: result
});
}
catch(err){
return res.status(500).send(err);
}
};

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

var express = require('express');
var ArduinoRouter = express.Router();

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

ArduinoRouter.route('/')
.post(userAuthorization, require('./postExample').postExample);

ArduinoRouter.route('/:exampleId')
.put(userAuthorization, require('./putExample').putExample);

ArduinoRouter.route('/:exampleId')
.delete(userAuthorization, require('./deleteExample').deleteExample);

ArduinoRouter.route('/')
.get(require('./getExamples').getArduinoExamples);

ArduinoRouter.route('/:exampleId')
.get(require('./getExample').getExample);

module.exports = ArduinoRouter;
77 changes: 77 additions & 0 deletions routes/arduinoExamples/postExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// jshint esversion: 8
// jshint node: true
"use strict";

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

const arduinoExamples = require('../../models/arduinoExamples');
const User = require('../../models/user');


/**
* @api {post} /arduino Create Arduino Example
* @apiName postExample
* @apiDescription Create a Arduino Example.
* @apiGroup ArduinoExamples
*
* @apiHeader {String} Authorization allows to send a valid JSON Web Token along with this request with `Bearer` prefix.
* @apiHeaderExample {String} Authorization Header Example
* Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlMTk5OTEwY2QxMDgyMjA3Y2Y1ZGM2ZiIsImlhdCI6MTU3ODg0NDEwOSwiZXhwIjoxNTc4ODUwMTA5fQ.D4NKx6uT3J329j7JrPst6p02d311u7AsXVCUEyvoiTo
*
* @apiParam {String} title name of the project
* @apiParam {String} description further information about the project
* @apiParam {String} xml XML-String of the blockly-content
*
* @apiSuccess (Success 201) {String} message `Gallery is successfully created.`
* @apiSuccess (Success 201) {Object} gallery `{
"_id": "5fd8a66cb40982332c400bc4",
"title": "flimsy-cougar",
"description": "Beschreibung",
"xml": "<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"arduino_functions\" id=\"QWW|$jB8+*EL;}|#uA\" deletable=\"false\" x=\"27\" y=\"16\"></block>\n</xml>",
"creator": "em@il.de",
"createdAt": "2020-12-15T12:05:00.662Z",
"updatedAt": "2020-12-15T12:05:00.662Z",
"__v": 0
}`
*
* @apiError (On error) {Object} 403 `{"message": No permission creating the gallery project."}`
* @apiError (On error) {Obejct} 500 Complications during querying the database.
*/
const postExample = async function(req, res){
// const {error} = projectValidation(req.body);
// if(error) return res.status(422).send({message: error.details[0].message});
try{
var user = await User.findOne({email: req.user.email});
if(user.role !== 'user'){
const body = {
_id: new mongoose.Types.ObjectId(),
title: req.body.title,
description: req.body.description,
code: req.body.code,
creator: req.user.email,
board: req.body.board,
type: req.body.type,
};
const example = new arduinoExamples(body);
const savedExamples = await example.save();
return res.status(201).send({
message: 'Gallery is successfully created.',
arduinoExamples: savedExamples
});
}
else {
return res.status(403).send({
message: 'No permission creating the gallery project.',
});
}
}
catch(err) {
console.log(err);
return res.status(500).send(err);
}
};

module.exports = {
postExample
};
Loading