wmiys / gui.alpha

Front end for the site
https://wmiys.com
0 stars 0 forks source link

[JS] Create api response view data model classes #218

Open rrickgauer opened 2 years ago

rrickgauer commented 2 years ago

Have them all inherit from a base class that takes in an api response and automatically set's the object's properties to the ones given in the constructor.

These classes should be just properties, no logic or methods to them.

rrickgauer commented 2 years ago

Products

Field Type
id int UN
name varchar(250)
description text
product_categories_sub_id int UN
product_categories_sub_name char(50)
product_categories_minor_id int UN
product_categories_minor_name char(50)
product_categories_major_id int UN
product_categories_major_name char(50)
location_id int UN
location_city varchar(100)
location_state_id char(2)
location_state_name varchar(45)
dropoff_distance smallint UN
price_full decimal(10,2) UN
image char(41)
minimum_age tinyint UN
created_on timestamp
user_id int UN
user_email char(254)
user_name_first char(100)
user_name_last char(150)
rrickgauer commented 2 years ago

Users

Field Type
id int UN
email char(254)
name_first char(100)
name_last char(150)
birth_date date
created_on timestamp
count_products bigint
count_agreements bigint
lender_earnings double
lender_balance double
payout_account_id char(25)
rrickgauer commented 2 years ago

Basic example of how the views should look and use a base class:


/**
 * Base view class.
 */
class ViewBase
{
    constructor(apiRecord) {
        const apiRecordKeys = Object.keys(apiRecord);
        const viewKeys = Object.keys(this);

        for (const apiKey of apiRecordKeys) {
            if (viewKeys.includes(apiKey)) {
                this[apiKey] = apiRecord[apiKey];
            }
        }
    }
}

/**
 * View Product class
 */
class ViewProduct extends ViewBase
{
    constructor(apiRecord) {
        this.id = null;
        this.name = null;
        this.description = null;
        this.product_categories_sub_id = null;
        this.product_categories_sub_name = null;
        this.product_categories_minor_id = null;
        this.product_categories_minor_name = null;
        this.product_categories_major_id = null;
        this.product_categories_major_name = null;
        this.location_id = null;
        this.location_city = null;
        this.location_state_id = null;
        this.location_state_name = null;
        this.dropoff_distance = null;
        this.price_full = null;
        this.image = null;
        this.minimum_age = null;
        this.created_on = null;
        this.user_id = null;
        this.user_email = null;
        this.user_name_first = null;
        this.user_name_last = null;

        super(apiRecord);
    }
}