/**
* Manages a collection of categories.
*/
class CategoryService {
/**
* Creates a new CategoryService instance.
*/
constructor() {
this.categories = [];
}
/**
* Adds a new category to the collection.
* @param {Category} category - The category to add.
*/
addCategory(category) {
this.categories.push(category);
}
/**
* Retrieves a category by its ID.
* @param {string} categoryId - The ID of the category to find.
* @returns {Category|null} The found category or null if not found.
*/
getCategoryById(categoryId) {
for(let category of this.categories){
if(category.categoryId === categoryId){
return category;
}
}
return null;
}
/**
* Updates an existing category's information.
* @param {string} categoryId - The ID of the category to update.
* @param {Object} updates - An object containing updated properties.
* @returns {boolean} True if the category was updated, false otherwise.
*/
updateCategory(categoryId, updates) {
const category = this.getCategoryById(categoryId);
if (category) {
Object.assign(category, updates);
category.updatedAt = new Date();
return true;
}
return false;
}
/**
* Deletes a category from the collection.
* @param {string} categoryId - The ID of the category to delete.
*/
deleteCategory(categoryId) {
const index = this.categories.findIndex(category => category.categoryId === categoryId);
if(index !== -1){
this.categories.splice(index, 1);
}
}
/**
* Lists all categories in the collection.
* @returns {Category[]} Array of all categories.
*/
listCategories() {
return this.categories;
}
/**
* Retrieves all available categories.
* @returns {Category[]} Array of available categories.
*/
getAvailableCategories() {
const availableCategories =[];
function isAvailable(category){
return category.availabilityStatus;
}
this.categories.forEach(category => {
if(isAvailable(category)){
availableCategories.push(category);
}
});
return availableCategories;
}
/**
* Retrieves all recommended categories.
* @returns {Category[]} Array of recommended categories.
*/
getRecommendedCategories() {
const recommendedCategories = [];
function isRecommended(category){
return category.isRecommended;
}
this.categories.forEach(category => {
if(isRecommended(category)){
recommendedCategories.push(category);
}
});
return recommendedCategories;
}
}
export default CategoryService ;