Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detailed API Examples With Shuttle + ActixWeb + Turso #199

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

iltifatibad
Copy link

Description of change

User Registration and Authentication:
    A create_user function was added to allow new user registrations with fields like name, surname, email, phone, and password. A random id and a security token are generated for the user upon successful registration.
    A login_user function was implemented to handle user login with email and password. It returns a response indicating whether the credentials match an existing user.

Content Management (News and Events Upload):
    The news_upload function enables users to upload news articles with a file (image) and description. The uploaded files are saved to a specified folder and the news entry is stored in the database.
    Similarly, the events_upload function allows the uploading of event data with a file (image) and details. The event is then stored in the database.

Data Retrieval:
    The get_data function was introduced to retrieve and return the latest news and events from the database in JSON format based on API endpoint requests.

Statistics Calculation:
    A get_statistics function was added to calculate and return the total revenue, current day's user count, and the number of news and events in the database.

Reasons for Changes

These changes were made to provide key functionalities for user management, content uploading, and real-time data retrieval in the application.
User registration and login allow for personalized interactions.
Content management for news and events ensures that the platform can handle dynamic updates.
Statistics retrieval allows administrators to monitor and manage the platform's performance.

How has this been tested? (if applicable)

User Registration (create_user):
This function accepts a JSON object of type SigninUser (with fields: name, surname, email, phone, password) and registers a new user in the database.
A random user ID (ranid) is generated, ensuring it does not already exist in the database.
The user's data is then stored in the users table, including a generated token and registration time.
Upon successful registration, a response with "User successfully registered" is returned.

User Login (login_user):
    This function accepts a JSON object of type LoginUser (with fields: email, password).
    It checks the database to verify if a user exists with the provided email and password.
    If the user is found, a successful login response ("Ok") is returned; otherwise, a bad request ("Not Ok") is returned.

News Upload (news_upload):
    This function processes multipart data to upload news items. It accepts name, description, and an image file (file).
    A unique ID (ranid) is generated for the news item, ensuring it doesn't conflict with existing news.
    The image is saved to a directory, and the news data (including image path, name, description, and registration time) is saved to the database.
    The response returned is "News successfully registered" on success or an error message on failure.

Events Upload (events_upload):
    Similar to the news_upload function, it handles multipart data to upload event items with a name, description, and an image file (file).
    A unique ID is generated for the event item, and the event data is saved to the events table, along with the image.
    The response is "Events successfully registered" on success or an error message if something goes wrong.

Fetching Data (get_data):
    This function handles querying different types of data (news, events) based on the provided path in the query parameters.
    Depending on the path value, it fetches all news, single news (landing), all events, or single event (landing) from the database and returns them as JSON.

Statistics Fetching (get_statistics):
    This function is designed to fetch statistics like total income, users, news, and events based on the specified path in the query parameters.
    If the path is "api/get/income", it calculates the total income by multiplying the number of users by 3.
    It also retrieves statistics for users, news, and events created today using SQL queries.
    The response returns these statistics as a JSON object.

Code Details:

User Structs:
    SigninUser: Represents the structure of a user during registration.
    LoginUser: Represents the structure for login requests.
    StatisticsPayload: Contains the path and optional pagination parameters (page, limit).
    Statistics: Represents the statistics data, such as total income and counts for users, news, and events created today.
    User: Represents a user’s basic information along with a generated token.
    NewsItem and NewsItemLanding: Represent news item data, with the latter being used for landing page queries.
    Info: Contains the path and optional id parameters used for querying different resources.

Multipart Handling:
    The code handles multipart form data to upload files (e.g., images for news and events).
    Files are saved to specific directories, and the path to these files is saved in the database.

Database Interactions:
    The code interacts with a database (using libsql), performing queries and inserts.
    Random IDs are generated to avoid conflicts in the users, news, and events tables.
    SQL queries are used to fetch data for statistics, news, events, and user details.

Date Handling:
    The chrono::Utc::now() is used to store the registration time for users, news, and events.

Error Handling:
    Errors during database operations (like insert failures) are captured and returned in the response as an internal server error.

Response Examples:

User Registration:
    Success: { "status": "User successfully registered" }
    Error: { "error": "Error: <details>" }
Login:
    Success: { "status": "Ok" }
    Error: { "status": "Not Ok" }
News/Event Upload:
    Success: { "status": "News/Event successfully registered" }
    Error: { "error": "Error: <details>" }
Fetching Data:
    Example for news: [ { "id": 1, "imgpath": "path/to/image", "name": "News Title", "description": "Description" } ]
Statistics:
    Success: { "total_income": 150, "users_today": 5, "news_today": 3, "events_today": 2 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant