Overview
EducaStream provides a comprehensive set of utility functions for managing users, courses, payments, and data validation. All API calls use Axios and are configured to work with the backend endpoints.
Data Fetching Functions
getAllCourses
Fetches all courses from the database and stores them in local storage, sorted by creation date (newest first).
This function takes no parameters
Array of course objects sorted by createdAt in descending orderISO timestamp of course creation
Source: src/utils/getAllCourses.js:3
import { getAllCourses } from './utils/getAllCourses';
// Fetch and store all courses
const courses = await getAllCourses();
console.log(courses); // Sorted by newest first
getAllCategories
Retrieves all course categories from the database, sorted alphabetically by name.
This function takes no parameters
Array of category objects sorted alphabetically
Source: src/utils/getAllCategories.js:3
import { getAllCategories } from './utils/getAllCategories';
// Used in App.jsx on initial load
useEffect(() => {
getAllCategories();
}, []);
getAllUser
Fetches all users from the database.
This function takes no parameters
Source: src/utils/getAllUser.js:3
import { getAllUser } from './utils/getAllUser';
// Example from App.jsx
useEffect(() => {
getAllUser();
getAllCourses();
getAllCategories();
}, []);
getUserById
Retrieves a specific user by their unique ID.
The unique identifier of the user to fetch
User object with all user detailsArray of user’s payment records
Source: src/utils/getUserById.js:3
import { getUserById } from './utils/getUserById';
const user = await getUserById('ab518b48-1a30-4d10-b525-479167e4fdd4');
getUser
Fetches a user by their email address.
The email address of the user to retrieve
User object matching the email
Source: src/utils/getUser.js:3
import { getUser } from './utils/getUser';
const user = await getUser('[email protected]');
getOnSaleCourses
Retrieves all courses currently on sale.
This function takes no parameters
Array of course objects with active sales/discounts
Source: src/utils/getOnSaleCourses.js:3
import { getOnSaleCourses } from './utils/getOnSaleCourses';
const saleCourses = await getOnSaleCourses();
getAllPayments
Fetches all payment records from the database.
This function takes no parameters
Array of payment objectsCourses included in the payment
Source: src/utils/getAllPayments.js:3
import { getAllPayments } from './utils/getAllPayments';
const payments = await getAllPayments();
Data Mutation Functions
User Operations
Course Operations
Lesson & Payment
postUser
Creates a new user in the database.User object containing registration dataUser’s birthdate in ISO format
Whether user has instructor role
Whether user has student role
Source: src/utils/postUser.js:3import { postUser } from './utils/postUser';
const newUser = await postUser({
email: "[email protected]",
password: "securePassword123",
user_name: "johndoe",
first_name: "John",
last_name: "Doe",
role_student: true,
role_instructor: false
});
updateUser
Updates an existing user’s information.User object containing fields to update (must include user ID)
Source: src/utils/updateUser.js:3import { updateUser } from './utils/updateUser';
const updated = await updateUser({
id: "ab518b48-1a30-4d10-b525-479167e4fdd4",
first_name: "UpdatedName"
});
sendData (Create Course)
Creates a new course in the database.Course object containing course detailsCourse title (minimum 3 characters)
Course description (20-90 characters)
UUID of the instructor creating the course
URL to course thumbnail image
Course price (minimum $0.50)
Number of sections (minimum 1)
Source: src/utils/postCreateCourse.js:3import { sendData } from './utils/postCreateCourse';
const newCourse = await sendData({
title: "Advanced JavaScript",
description: "Learn modern JavaScript techniques and best practices",
category: "Programación",
instructor_id: "b05d6c8d-0eaa-4e99-8b75-548f5466714e",
price: 49.99,
sections: 5,
image: "https://example.com/course-image.jpg"
});
updateCourse
Updates an existing course with new data.Course update objectWhether course is enabled
Whether course is on sale
Discount percentage if on sale
Source: src/utils/updateCourse.js:3import updateCourse from './utils/updateCourse';
const updated = await updateCourse({
id: "course-uuid",
onSale: true,
percentageDiscount: 20,
enabled: true
});
updateLesson
Updates lesson information for a course.Lesson update object containing fields to modify Source: src/utils/updateLesson.js:3import updateLesson from './utils/updateLesson';
const updatedLesson = await updateLesson({
id: "lesson-uuid",
title: "Updated Lesson Title",
video_url: "https://youtube.com/watch?v=new-video"
});
postRating
Submits a rating and comment for a course.UUID of the course being rated
UUID of the user submitting the rating
Rating value (typically 1-5)
Source: src/utils/postRating.js:3import { postRating } from './utils/postRating';
const rating = await postRating(
"course-uuid",
"user-uuid",
5,
"Excellent course! Highly recommended."
);
postPaymentCart
Processes payment for courses in the cart and enrolls the user.Array of course objects to purchase
User ID making the purchase
Payment transaction ID from payment gateway
Payment confirmation object
Source: src/utils/postPaymentCart.js:3import { postPaymentCart } from './utils/postPaymentCart';
const payment = await postPaymentCart(
[{ id: "course-1", title: "JavaScript" }],
"user-uuid",
"[email protected]",
"payment-transaction-id"
);
Validation Functions
validation
Validates course creation/update form data.
Object containing course form fieldsCourse title (min 3 characters)
Course description (20-90 characters)
Number of sections (min 1)
Object containing validation error messages keyed by field nameTitle validation error message
Description validation error message
Category validation error message
Sections validation error message
Price validation error message
Source: src/utils/validation.js:1
import { validation } from './utils/validation';
const formData = {
title: "JS", // Too short
description: "Short desc", // Too short
category: "Programming",
sections: 3,
price: 29.99
};
const errors = validation(formData);
// Returns: {
// title: "Debe tener al menos 3 caracteres.",
// description: "La descripción debe tener al menos 20 caracteres."
// }
validationLesson
Validates lesson creation/update form data.
Object containing lesson form fieldsLesson title (min 3 characters)
Lesson description (min 20 characters)
Section number (must be >= 1)
Object containing validation error messages keyed by field name
Source: src/utils/validation.js:39
import { validationLesson } from './utils/validation';
const lessonData = {
title: "Introduction",
description: "This lesson covers the basics of JavaScript",
section: 1,
video_url: "https://youtube.com/watch?v=abc123"
};
const errors = validationLesson(lessonData);
if (Object.keys(errors).length === 0) {
// Validation passed
}
Utility Functions
clear
Clears all user session data from localStorage and reloads the page. Used for logout functionality.
This function takes no parameters
Source: src/utils/clear.js:1
import { clear } from './utils/clear';
// Logout user and clear session
clear();
// Removes: userOnSession, logged, payment
// Then reloads the page
filterBack
Filters courses by a specific parameter on the backend.
Filter parameters objectFilter type (e.g., “category”, “instructor”)
Source: src/utils/FiltersBc.js:3
import { filterBack } from './utils/FiltersBc';
// Filter courses by category
await filterBack({
type: "category",
value: "Programación"
});
Error Handling
All API functions implement try-catch error handling:
try {
const response = await axios.get('/endpoint');
if (response.status !== 200) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.data;
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
Payment functions include user-friendly alerts:
try {
const { data } = await axios.post("payment/enrollment", payload);
return data;
} catch (error) {
window.alert("Error en la pasarela de pago");
}