-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lesson] Count number of views per lesson
- Loading branch information
Showing
5 changed files
with
136 additions
and
2 deletions.
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
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,17 @@ | ||
import HttpService from '../Http.service'; | ||
|
||
class LessonService { | ||
static get(lessonId: string) { | ||
return HttpService | ||
.get(`${process.env.ENDPOINT}/lessons/${lessonId}`) | ||
.then((resp) => resp.json()); | ||
} | ||
|
||
static increaseViews(lessonId: string) { | ||
return HttpService | ||
.post(`${process.env.ENDPOINT}/lessons/${lessonId}/views`) | ||
.then((resp) => resp.json()); | ||
} | ||
} | ||
|
||
export default LessonService; |
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,59 @@ | ||
const mongoose = require('mongoose'); | ||
const { ServerError } = require('../ServerUtils'); | ||
|
||
const LessonsSchema = new mongoose.Schema({ | ||
lessonId: { type: String, required: true, unique: true }, | ||
views: { type: Number, required: true }, | ||
}, { | ||
timestamps: { | ||
updatedAt: 'updatedAt', | ||
}, | ||
}); | ||
|
||
const Lesson = mongoose.models.Lesson || mongoose.model('Lesson', LessonsSchema); | ||
|
||
class LessonModel { | ||
static get(lessonId) { | ||
return Lesson.findOne({ lessonId }); | ||
} | ||
|
||
static async updateViews(lessonId, views) { | ||
let lesson = null; | ||
|
||
try { | ||
lesson = await LessonModel.get(lessonId); | ||
} catch (err) { | ||
console.error(` | ||
[LessonModel.updateViews] got while fetching for lessonId=${lessonId}`, | ||
err | ||
); | ||
|
||
throw err; | ||
} | ||
|
||
if (!lesson) { | ||
lesson = new Lesson({ lessonId }); | ||
console.log(`[LessonModel.updateViews] created lesson on demand, with id=${lessonId}`); | ||
} | ||
|
||
lesson.views = views; | ||
|
||
return new Promise((resolve, reject) => { | ||
lesson.save((err) => { | ||
if (err) { | ||
console.error(` | ||
[LessonModel.updateViews] got while saving lessonId=${lessonId}`, | ||
err | ||
); | ||
|
||
reject(err); | ||
return; | ||
} | ||
|
||
resolve(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = LessonModel; |
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,42 @@ | ||
const express = require('express'); | ||
const LessonModel = require('./lesson.model'); | ||
const { ServerError } = require('../ServerUtils'); | ||
|
||
const lessonRouter = express.Router(); | ||
|
||
lessonRouter.get('/:lessonId', async function getLesson(req, res) { | ||
const { lessonId } = req.params; | ||
const lesson = await LessonModel.get(lessonId); | ||
|
||
if (!lesson) { | ||
new ServerError(404, `Lesson with id=${lessonId} doesn't exist!`).send(res); | ||
return; | ||
} | ||
|
||
res.json(lesson); | ||
}); | ||
|
||
lessonRouter.post('/:lessonId/views', async function increaseViews(req, res) { | ||
const { lessonId } = req.params; | ||
|
||
const lesson = await LessonModel.get(lessonId); | ||
|
||
try { | ||
if (!lesson) { | ||
await LessonModel.updateViews(lessonId, 1); | ||
} else { | ||
await LessonModel.updateViews(lessonId, lesson.views + 1); | ||
} | ||
|
||
const updatedLesson = await LessonModel.get(lessonId); | ||
res.json(updatedLesson); | ||
} catch (err) { | ||
new ServerError( | ||
err.code || 500, | ||
err.message || 'Oops! A apărut o problemă. Încearcă din nou!' | ||
); | ||
} | ||
}); | ||
|
||
|
||
module.exports = lessonRouter; |
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