Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

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

Open
rrickgauer opened this issue Apr 6, 2022 · 3 comments
Open

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

rrickgauer opened this issue Apr 6, 2022 · 3 comments
Assignees
Labels
refactor Refactor this fucking code

Comments

@rrickgauer
Copy link
Member

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 rrickgauer added the refactor Refactor this fucking code label Apr 6, 2022
@rrickgauer rrickgauer self-assigned this Apr 6, 2022
@rrickgauer
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

rrickgauer commented Apr 7, 2022

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);
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
refactor Refactor this fucking code
Projects
None yet
Development

No branches or pull requests

1 participant