# SdkForAngular

### Simple SDK to build awesome apps with Angular Interface and iFrameAI Backend

#### Make sure you initialized module in your app.module.ts

```
import { SdkForAngularModule } from 'iframe-ai-ng'
```

```ts
 imports: [

  SdkForAngularModule.forRoot({
    apiURL: "http://IFRAME-AI-BACKEND-SDK-URL:8080/connect",
    webURL: "iframe.ai",
    socketURL: "ws.iforels.app",
    fileURL: "api.iframe.ai",
    lang: "en_US"
  })
]
```

## Usage example

```ts

import {Component} from '@angular/core';
import { SdkForAngularService } from 'sdk-for-angular';

interface RefObject {
  id: any; // or the specific type
  _d: 'in' | 'out' | 'both';  // Use union type
}

// Define the record-like object structure.
interface RecordRelations {
  [anyPropName: string]: RefObject;
}

// Define the RECORD interface
interface RECORD {
  id: number;
  value: number;
  gps_lat: number;
  gps_long: number;
  "__": RecordRelations;
}

@Component({
  selector: 'app-routers-test',
  templateUrl: './routers-test.component.html',
  styleUrls: ['./routers-test.component.css']
})
export class RoutersTestComponent {

  public demoUserQuery = {
    select: {
      "operator": "and",
      "conditions": [
        {
          "type": "equals",
          "property": "email",
          "value": "john.doe@example.com"
        }
      ]
    }
  };

  public demoAccountQuery = {
    select: {
      "operator": "and",
      "conditions": [
        {
          "type": "equals",
          "property": "email",
          "value": "garry@example.com"
        }
      ]
    }
  };
  demoAppQuery = {};
  demoUnitQuery = {};
  demoInterfaceQuery = {};
  demoFeatureQuery = {};
  demoDatasetQuery = {};
  demoRecordQuery = {};

  public demoUser = {
    id: 51544,
    "first_name": "Jeff",
    "last_name": "Smith",
    "email": "john@example.com",
    "phone": "+1 (555) 123-4567",
    "password": "hashed_password_here",
    "lang": "en_US",
    "verified": false,
    "notifications": true,
    "address": "123 Main Street, New York, NY 10001",
    "address_GPS": {"lat": 40.7128, "long": -74.0060},
    "timezone": "America/New_York",
    "avatar": "url",
    "theme": "dark"
  }

  public demoAccount = {
    id: 53327,
    "name": "BASE, INC.",
    "spending_limits": 1000,
    "email": "garry@example.com",
    "billing_email": "billing@example.com",
    "address": {
      "country": "United States",
      "line1": "123 Main Street",
      "line2": "Office 4B",
      "street": "Main Street",
      "city": "New York",
      "state": "NY",
      "postal_code": "10001"
    },
    "total_paid": 500,
    "payment_due": new Date().getTime(),
    "stripeCustomerID": "cus_abc123",
    "stripeSubscriptionID": "sub_xyz789",
    "stripePlanID": "plan_free",
    "active": true,
    "plan": "free",
    "price": 0,
    "__": {
      "appHolder": [{
        "id": 53251, // APP ID
        "_d": "out"
      }],
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "out"
      },
      "accountHolder": {
        "id": 51544, // USER ID
        "_d": "out"
      }
    }
  }

  public demoApp = {
    id: 59305,
    name: 'My Awesome App',
    __: {
      'createdBy': {
        id: 51544, // USER ID
        _d: 'out'
      },
      'team': [{
        id: 51544, // USER ID
        _d: 'both'
      }]
    }
  }

  public demoUnit = {
    "id": 53255,
    "name": "My Awesome Database",
    "__": {
      "database": {
        "id": 53251, // APP ID
        "_d": "both"
      },
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "in"
      }
    }
  }

  public demoInterface = {
    id: 53666,
    name: 'My Awesome Interface',
    type: 'table',
    config:{},
    "__": {
      "interfaceOfApp": {
        "id": 53251, // App ID
        "_d": "out"
      },
      "interfaceOfUnit": {
        "id": 53255, // UNIT ID
        "_d": "both"
      },
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "in"
      }
    }
  }

  public demoFeature = {
    id: 53641,
    type: 'number',
    default: 0,
    config:{},
    "__": {
      "unitFeature": {
        "id": 53255, // UNIT ID
        "_d": "out"
      },
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "in"
      }
    }
  }

  public demoDataset = {
    id:1856807,
    archive: false,
    "__": {
      "datasetOfUnit": {
        "id": 53255, // UNIT ID
        "_d": "out"
      },
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "out"
      }
    }
  }

  demoDatasets(){

    let datasets:any[] = [];

    let datasetsNumber = 0;

    while (datasetsNumber < 100) {

      let records:any[] = [];

      let recordsNumber = 0;

      while (recordsNumber < 2) {
        let record:any = { ...this.demoRecord, _d:'in' };
        delete record?.__?.recordOfDataset;
        delete record?.id;
        records.push(record);
        recordsNumber++;
      }

      let dataset:any = {...this.demoDataset, imported: true };
      delete dataset?.id;

      datasets.push({...dataset, "__": { ...dataset.__, records } });

      datasetsNumber++;
    }
    return datasets;
  }

  recordInstance = (record: RECORD, customRelations: RecordRelations = {})=>{
    return {...record, __: {...record.__, customRelations} };
  }

  public demoRecord = this.recordInstance({
    id: 60177,
    value: Math.floor(Math.random() * 10),
    gps_lat: 37.7749 + Math.random() * 0.1,
    gps_long: -122.4194 + Math.random() * 0.1,
    "__": {
      "recordOfDataset": {
        "id": 53663, // DATASET ID
        "_d": "out"
      },
      "recordOfFeature": {
        "id": 53641, // FEATURE ID
        "_d": "out"
      },
      "createdBy": {
        "id": 51544, // USER ID
        "_d": "in"
      }
    }
  });

  public demo_ = {}

  constructor( private sdkService: SdkForAngularService) {

  }

  ngOnInit() {

  }

  // For demoUser
  createUser(data: object = {}) {
    console.log(data);
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createUser(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateUser(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateUser(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getUser(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getUser(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteUser(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteUser(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchUser(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchUser(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoAccount
  createAccount(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createAccount(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateAccount(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateAccount(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getAccount(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getAccount(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteAccount(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteAccount(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchAccount(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchAccount(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoApp
  createApp(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createApp(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateApp(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateApp(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getApp(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getApp(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteApp(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteApp(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchApp(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchApp(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoUnit
  createUnit(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createUnit(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateUnit(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateUnit(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getUnit(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getUnit(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteUnit(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteUnit(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchUnit(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchUnit(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoInterface
  createInterface(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createInterface(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateInterface(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateInterface(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getInterface(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getInterface(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteInterface(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteInterface(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchInterface(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchInterface(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoFeature
  createFeature(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createFeature(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateFeature(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateFeature(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getFeature(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getFeature(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteFeature(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteFeature(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchFeature(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchFeature(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoDataset
  createDataset(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createDataset(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  createManyDatasets() {
    const datasets = this.demoDatasets();
    try {
      const res = this.sdkService.createDatasets(datasets);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateDataset(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateDataset(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getDataset(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getDataset(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteDataset(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteDataset(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchDataset(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchDataset(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

// For demoRecord
  createRecord(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.createRecord(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  updateRecord(data: object = {}) {
    if (!data) throw new Error("Data is required");
    try {
      const res = this.sdkService.updateRecord(data);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  getRecord(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.getRecord(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  deleteRecord(id: number) {
    if (!id) throw new Error("ID is required");
    try {
      const res = this.sdkService.deleteRecord(id);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }

  searchRecord(query: object) {
    if (!query) throw new Error("Query is required");
    try {
      const res = this.sdkService.searchRecord(query);
      return res;
    } catch (error) {
      throw error;
      console.error(error);
    }
  }


}


```

## Code scaffolding

Run `ng generate component component-name --project sdk-for-angular` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project sdk-for-angular`.
> Note: Don't forget to add `--project sdk-for-angular` or else it will be added to the default project in your `angular.json` file.

## Build

Run `ng build sdk-for-angular` to build the project. The build artifacts will be stored in the `dist/` directory.

## Publishing

After building your library with `ng build sdk-for-angular`, go to the dist folder `cd dist/sdk-for-angular` and run `npm publish`.

## Running unit tests

Run `ng test sdk-for-angular` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
