Skip to content

Commit

Permalink
Create tutorials.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent ccd0322 commit 1cf1715
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/education/tutorials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// education/tutorials.js
class Tutorial {
constructor(title, content, author) {
this.id = Tutorial.incrementId();
this.title = title;
this.content = content;
this.author = author;
this.createdAt = new Date();
}

static incrementId() {
if (!this.currentId) {
this.currentId = 1;
} else {
this.currentId++;
}
return this.currentId;
}
}

class Tutorials {
constructor() {
this.tutorials = []; // Store tutorials
}

createTutorial(title, content, author) {
const tutorial = new Tutorial(title, content, author);
this.tutorials.push(tutorial);
console.log(`Tutorial created: ${title}`);
return tutorial;
}

getTutorials() {
return this.tutorials;
}

getTutorialById(tutorialId) {
const tutorial = this.tutorials.find(t => t.id === tutorialId);
if (!tutorial) {
throw new Error('Tutorial not found.');
}
return tutorial;
}

updateTutorial(tutorialId, updatedContent) {
const tutorial = this.getTutorialById(tutorialId);
tutorial.content = updatedContent;
console.log(`Tutorial ${tutorialId} updated.`);
return tutorial;
}

deleteTutorial(tutorialId) {
const index = this.tutorials.findIndex(t => t.id === tutorialId);
if (index === -1) {
throw new Error('Tutorial not found.');
}
this.tutorials.splice(index, 1);
console.log(`Tutorial ${tutorialId} deleted.`);
}
}

module.exports = Tutorials;

0 comments on commit 1cf1715

Please sign in to comment.