Created
July 30, 2021 10:05
-
-
Save jcmpes/992e3c39aa10b425d18608be67b41c74 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
const mongoose = require('mongoose'); | |
// Import the slug package | |
const slug = require('mongoose-slug-updater'); | |
// Initialize | |
mongoose.plugin(slug); | |
const courseSchema = mongoose.Schema( | |
{ | |
title: { type: String, unique: true, index: true }, | |
slug: { type: String, slug: 'title', unique: true }, | |
user: { | |
type: mongoose.Schema.Types.ObjectId, | |
ref: 'User', | |
}, | |
category: { | |
type: mongoose.Schema.Types.ObjectId, | |
ref: 'Category', | |
}, | |
price: { type: Number, required: true }, | |
video: String, | |
description: String, | |
content: String, | |
image: String, | |
lessons: [ | |
{ | |
type: mongoose.Schema.Types.ObjectId, | |
ref: 'Lesson', | |
}, | |
], | |
}, | |
{ timestamps: true }, | |
); | |
courseSchema.statics.list = async function (filter, skip, limit, sort) { | |
const query = Course.find(filter).populate('user').populate('category'); | |
query.sort(sort); | |
query.skip(skip); | |
query.limit(limit); | |
return await query.exec(); | |
}; | |
const Course = mongoose.model('Course', courseSchema); | |
module.exports = Course; |
This file contains hidden or 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
'use strict'; | |
const express = require('express'); | |
const router = express.Router(); | |
const mongoose = require('mongoose'); | |
const Category = mongoose.model('Category'); | |
const Lesson = mongoose.model('Lesson'); | |
const Course = mongoose.model('Course'); | |
const User = mongoose.model('User'); | |
const jwtAuth = require('../../lib/jwAuth'); | |
const multer = require('multer'); | |
const { uploadFile } = require('../../lib/s3'); | |
const path = require('path'); | |
const UPLOAD_FOLDER = process.env.UPLOAD_FOLDER || "public/images/"; | |
/** | |
* Configurar multer | |
*/ | |
const fileExtensionRemover = originalName => { | |
return originalName.split('.')[0]; | |
}; | |
const storage = multer.diskStorage({ | |
destination: function(req, file, cb) { | |
cb(null, UPLOAD_FOLDER); | |
}, | |
filename: function(req,file, cb) { | |
cb(null, fileExtensionRemover(file.originalname) + '-' + Date.now() + path.extname(file.originalname)) | |
} | |
}); | |
const upload = multer({ storage }); | |
/** | |
* POST /api/v1/courses | |
* Create a new course | |
*/ | |
router.post( | |
'/', | |
jwtAuth, | |
upload.single('image'), | |
async function (req, res, next) { | |
try { | |
// Server side validation | |
const formData = { ...req.body }; | |
const validation = formData.title && formData.category; | |
if (!validation) { | |
res | |
.status(400) | |
.json({ message: 'Title and category are both required' }); | |
return; | |
} | |
// Inject userId in new course before saving it | |
formData.user = req.apiAuthUserId; | |
const course = new Course(formData); | |
if (req.file) { | |
// Uplaod file to S3 and add image location to course object | |
const file = req.file; | |
const { Location } = await uploadFile(file); | |
course.image = Location; | |
} | |
// Save new lessons | |
if (formData.lessons) { | |
const lessonsToSave = JSON.parse(formData.lessons) | |
course.lessons = [] | |
for (const key in lessonsToSave) { | |
async function saveLesson() { | |
const oneLessonToSave = new Lesson(lessonsToSave[key]); | |
const saved = await oneLessonToSave.save() | |
return saved | |
} | |
saveLesson().then(async saved => { | |
console.log('LESSON salvada: ', saved) | |
course.lessons.push(saved._id) | |
console.log(course.lessons) | |
}) | |
} | |
} | |
// Save new course in database | |
const newCourse = await course.save(); | |
res.status(201).json(newCourse); | |
} catch (err) { | |
next(err); | |
} | |
}, | |
); |
This file contains hidden or 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
'use strict'; | |
const mongoose = require('mongoose'); | |
// Import the slug package | |
const slug = require('mongoose-slug-updater'); | |
// Initialize | |
mongoose.plugin(slug); | |
const lessonSchema = mongoose.Schema( | |
{ | |
title: { type: String }, | |
slug: { type: String, slug: 'title', unique: 'true' }, | |
image: String, | |
video: String, | |
description: String, | |
content: String, | |
}, | |
{ timestamps: true }, | |
); | |
const Lesson = mongoose.model('Lesson', lessonSchema); | |
module.exports = Lesson; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment