From 4a821eef984c27f2330aa7e2b6258ef570a8c456 Mon Sep 17 00:00:00 2001 From: Ibitoye Rotimi Best Date: Tue, 13 Jul 2021 09:43:53 +0300 Subject: [PATCH] Creating endpoints to persist data on server memory --- src/routes/api/_db.js | 363 +++++++++++++++++++ src/routes/api/_lessons.js | 14 - src/routes/api/course.js | 15 + src/routes/api/lesson.js | 22 +- src/routes/courses/[id]/lessons/index.svelte | 5 +- src/routes/discussion/[id].svelte | 16 +- src/utils/constants/answers.js | 5 + 7 files changed, 402 insertions(+), 38 deletions(-) create mode 100644 src/routes/api/_db.js delete mode 100644 src/routes/api/_lessons.js create mode 100644 src/routes/api/course.js create mode 100644 src/utils/constants/answers.js diff --git a/src/routes/api/_db.js b/src/routes/api/_db.js new file mode 100644 index 00000000..733c3a6b --- /dev/null +++ b/src/routes/api/_db.js @@ -0,0 +1,363 @@ +import { STATUS } from '../../utils/constants/answers'; +import { QUESTION_TYPE } from '../../components/Question/constants'; + +const ROLE = { + ADMIN: 0, + TUTOR: 1, + STUDENT: 2, +}; + +const tutors = [ + { + id: 1, + text: 'Sergey Semko', + }, + { + id: 3, + text: 'Jonathan Nelson', + }, + { + id: 4, + text: 'Sasha Pokidin', + }, + { + id: 5, + text: 'Natasha Rudenko', + }, + { + id: 6, + text: 'Yulia Marushko', + }, +]; + +const people = [ + { + name: 'Alexander Pokidin', + title: 'Senior Software Engineer', + avatar: 'https://i.pravatar.cc/150?img=1', + role: ROLE.TUTOR, + telegramLink: 'https://t.me/seed_of_abraham', + mailLink: 'mailto:irb.ossystem@gmail.com', + }, + { + name: 'Andrey Filatov', + title: 'Fullstack Software Engineer', + avatar: 'https://i.pravatar.cc/150?img=2', + role: ROLE.TUTOR, + telegramLink: 'https://t.me/seed_of_abraham', + mailLink: 'mailto:irb.ossystem@gmail.com', + }, + { + name: 'Sergey Semko', + title: 'Senior Software Engineer', + avatar: 'https://i.pravatar.cc/150?img=3', + role: ROLE.TUTOR, + telegramLink: 'https://t.me/seed_of_abraham', + mailLink: 'mailto:irb.ossystem@gmail.com', + }, + { + name: 'Natalia Fedii', + title: 'Project Manager', + avatar: 'https://i.pravatar.cc/150?img=4', + role: ROLE.TUTOR, + telegramLink: 'https://t.me/seed_of_abraham', + mailLink: 'mailto:irb.ossystem@gmail.com', + }, +]; + +const courses = [ + { + id: 1, + title: 'React.js Fundamentals', + status: '', + }, +]; + +// many to many: id, tutor_id, course_id +const course_tutors = [ + { + id: 1, + tutor_id: 1, + course_id: 1, + }, +]; + +// id, course_id +const lessons = [ + { + id: 1, + course_id: 1, + title: 'Вступительное слово. Софт скилы', + to: '/courses/1/lessons/1', + tutor: { + userId: 1, + avatar: 'https://picsum.photos/32/32/?random', + name: 'Sergey Semko', + }, + resources: [ + { + label: 'lesson', + value: 1, + }, + { + label: 'quiz', + value: 1, + }, + { + label: 'home task', + value: 2, + }, + ], + date: new Date(), + isComplete: true, + }, + { + id: 2, + course_id: 1, + title: 'Введение в ReactJS', + to: '/courses/1/lessons/2', + tutor: { + userId: 1, + avatar: 'https://picsum.photos/32/32/?random', + name: 'Sergey Semko', + }, + resources: [ + { + label: 'lesson', + value: 1, + }, + { + label: 'quiz', + value: 1, + }, + { + label: 'home task', + value: 2, + }, + ], + date: new Date(), + isComplete: false, + }, + { + id: 3, + course_id: 1, + title: 'Компоненты', + to: '/courses/1/lessons/3', + tutor: { + userId: 1, + avatar: 'https://picsum.photos/32/32/?random', + name: 'Sergey Semko', + }, + resources: [ + { + label: 'lesson', + value: 1, + }, + { + label: 'quiz', + value: 1, + }, + { + label: 'home task', + value: 2, + }, + ], + date: new Date(), + isComplete: false, + }, + { + id: 4, + course_id: 1, + title: 'Состояние компонентов и пропсы', + to: '/courses/1/lessons/4', + tutor: { + userId: 1, + avatar: 'https://picsum.photos/32/32/?random', + name: 'Sergey Semko', + }, + resources: [ + { + label: 'lesson', + value: 1, + }, + { + label: 'quiz', + value: 1, + }, + { + label: 'home task', + value: 2, + }, + ], + date: new Date(), + isComplete: false, + }, + { + id: 5, + course_id: 1, + title: 'Жизненный цикл', + to: '/courses/1/lessons/5', + tutor: { + userId: 1, + avatar: 'https://picsum.photos/32/32/?random', + name: 'Sergey Semko', + }, + resources: [ + { + label: 'lesson', + value: 1, + }, + { + label: 'quiz', + value: 1, + }, + { + label: 'home task', + value: 2, + }, + ], + date: new Date(), + isComplete: false, + }, +]; + +// id, lesson_id +const exercises = [ + { + id: 1, + lesson_id: 1, + title: 'Home task 1', + description: + "You will be to answer 10 questions, it isn't timed so you can take your time to answer. You can also continue from where you left off, you don't need to worry cause everything is automatically syncronized in the cloud.", + questions: [ + { + id: 'react-founder', + type: QUESTION_TYPE.CHECKBOX, + title: 'Who is the creator of React.js', + options: [ + { + id: '1', + value: 'Dan Abrahmov', + }, + { + id: '2', + value: 'Google', + }, + { + id: '3', + value: 'Facebook', + }, + { + id: '4', + value: 'Traversy Media', + }, + ], + answers: ['3'], + }, + { + id: 'vue-founder', + type: QUESTION_TYPE.CHECKBOX, + title: 'Who is the creator of Vue.js', + code: `const name = 'Josh Perez';\nconst element =

Hello, {name}

;\n\nReactDOM.render(\n + element,\, + document.getElementById('root') + ) + `, + options: [ + { + id: '1', + value: 'Evan Vue', + }, + { + id: '2', + value: 'Mark Zukerberg', + }, + { + id: '3', + value: 'Prince Charles', + }, + { + id: '4', + value: 'Bill Gates', + }, + ], + answers: ['3'], + }, + { + id: 'svelte-founder', + type: QUESTION_TYPE.RADIO, + title: 'Who is the creator of Svelte.js', + options: [ + { + id: '1', + value: 'Hillary Svelte', + }, + { + id: '2', + value: 'Mircosoft', + }, + { + id: '3', + value: 'Elevate', + }, + { + id: '4', + value: 'Coding Train', + }, + ], + answers: ['2'], + }, + { + id: 'angular-founder', + type: QUESTION_TYPE.TEXTAREA, + title: 'Who is the creator of Angular.js', + value: '', + }, + ], + }, +]; + +const answers = [ + { + lesson_id: 1, + exercise_id: 1, + answers: {}, + scores: {}, + currentQuestionIndex: 0, + isFinished: false, + progressValue: 100, + status: STATUS.PENDING, + }, +]; + +const lessonLookup = new Map(); +lessons.forEach((lesson) => { + lessonLookup.set(`${lesson.id}`, JSON.stringify(lesson)); +}); + +export function getCourse(id) { + const people = []; + return { + people, + lessons: getLessons(id), + }; +} + +export function getLessons(courseId) { + return lessons.filter((lesson) => lesson.course_id === courseId); +} + +export function getLesson(id) { + return lookup.get(id); +} + +export function getExercises(lessonId) { + return exercises.filter((exercise) => exercise.lesson_id === lessonId); +} + +export function getExercise(exerciseId) { + return { + exercise: exercises.find((exercise) => exercise.id === exerciseId), + answers: answers.find((answer) => answer.exercise_id === exerciseId), + }; +} diff --git a/src/routes/api/_lessons.js b/src/routes/api/_lessons.js deleted file mode 100644 index 4ea10134..00000000 --- a/src/routes/api/_lessons.js +++ /dev/null @@ -1,14 +0,0 @@ -const lessons = [{}]; - -const lookup = new Map(); -lessons.forEach((lesson) => { - lookup.set(`${lesson.id}`, JSON.stringify(lesson)); -}); - -export function getLessons() { - return lessons; -} - -export function getLecture(id) { - return lookup.get(id); -} diff --git a/src/routes/api/course.js b/src/routes/api/course.js new file mode 100644 index 00000000..3a88e52f --- /dev/null +++ b/src/routes/api/course.js @@ -0,0 +1,15 @@ +import { getCourse } from './_db'; + +function writeJsonHead(res, statusCode) { + res.writeHead(statusCode, { + 'Content-Type': 'application/json', + }); +} + +export function get(req, res, next) { + const { id } = req.query; + + writeJsonHead(res, 200); + + return res.end(JSON.stringify(getCourse(id))); +} diff --git a/src/routes/api/lesson.js b/src/routes/api/lesson.js index e5c61014..e5282556 100644 --- a/src/routes/api/lesson.js +++ b/src/routes/api/lesson.js @@ -1,4 +1,4 @@ -import { getLecture } from './_lessons'; +import { getLessons } from './_db'; function writeJsonHead(res, statusCode) { res.writeHead(statusCode, { @@ -7,21 +7,13 @@ function writeJsonHead(res, statusCode) { } export function get(req, res, next) { - // the `id` parameter is available because - // this file is called [id].json.js - const { id } = req.params; + const { id } = req.query; - if (lookup.has(id)) { - writeJsonHead(res, 200); + writeJsonHead(res, 200); - res.end(lookup.get(id)); - } else { - writeJsonHead(res, 404); - - res.end( - JSON.stringify({ - message: `Not found`, - }) - ); + if (id) { + return res.end(JSON.stringify(getLesson(id))); } + + return res.end(JSON.stringify(getLessons())); } diff --git a/src/routes/courses/[id]/lessons/index.svelte b/src/routes/courses/[id]/lessons/index.svelte index 5a6ada74..9d7bb383 100644 --- a/src/routes/courses/[id]/lessons/index.svelte +++ b/src/routes/courses/[id]/lessons/index.svelte @@ -1,7 +1,6 @@ @@ -68,6 +67,10 @@
(lesson.isComplete = !lesson.isComplete)} + toolTipProps={{ + title: `Click to ${lesson.isComplete ? 'lock' : 'unlock'}`, + direction: 'right', + }} > {#if lesson.isComplete} diff --git a/src/routes/discussion/[id].svelte b/src/routes/discussion/[id].svelte index 505d4b26..1ed297ec 100644 --- a/src/routes/discussion/[id].svelte +++ b/src/routes/discussion/[id].svelte @@ -1,12 +1,4 @@ diff --git a/src/utils/constants/answers.js b/src/utils/constants/answers.js new file mode 100644 index 00000000..f4624632 --- /dev/null +++ b/src/utils/constants/answers.js @@ -0,0 +1,5 @@ +export const STATUS = { + PENDING: 0, + IN_PROGRESS: 1, + REVIEWED: 2, +};