github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/angular-app/src/app/Cash/Cash.component.ts (about)

     1  import { Component, OnInit, Input } from '@angular/core';
     2  import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
     3  import { CashService } from './Cash.service';
     4  import 'rxjs/add/operator/toPromise';
     5  
     6  //provide associated components
     7  @Component({
     8  	selector: 'app-Cash',
     9  	templateUrl: './Cash.component.html',
    10  	styleUrls: ['./Cash.component.css'],
    11    providers: [CashService]
    12  })
    13  
    14  //CashComponent class
    15  export class CashComponent implements OnInit {
    16  
    17    //define variables
    18    myForm: FormGroup;
    19  
    20    private allAssets;
    21    private asset;
    22    private currentId;
    23  	private errorMessage;
    24  
    25    //initialize form variables
    26    cashID = new FormControl("", Validators.required);
    27    currency = new FormControl("", Validators.required);
    28    value = new FormControl("", Validators.required);
    29    ownerID = new FormControl("", Validators.required);
    30    ownerEntity = new FormControl("", Validators.required);
    31    
    32    constructor(private serviceCash:CashService, fb: FormBuilder) {
    33      //intialize form
    34      this.myForm = fb.group({
    35            cashID:this.cashID,
    36            currency:this.currency,
    37            value:this.value,
    38            ownerID:this.ownerID,
    39            ownerEntity:this.ownerEntity
    40          
    41      });
    42    };
    43  
    44    //on page initialize, load all cash assets
    45    ngOnInit(): void {
    46      this.loadAll();
    47    }
    48  
    49    //load all cash assets on the blockchain network
    50    loadAll(): Promise<any> {
    51  
    52      //retrieve all cash assets in the tempList array
    53      let tempList = [];
    54  
    55      //call serviceCash to get all cash asset objects
    56      return this.serviceCash.getAll()
    57      .toPromise()
    58      .then((result) => {
    59        this.errorMessage = null;
    60        
    61        //append tempList with the cash asset objects returned
    62        result.forEach(asset => {
    63          tempList.push(asset);
    64        });
    65  
    66        //assign tempList to allAssets
    67        this.allAssets = tempList;
    68      })
    69      .catch((error) => {
    70          if(error == 'Server error'){
    71              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
    72          }
    73          else if(error == '404 - Not Found'){
    74  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
    75          }
    76          else{
    77              this.errorMessage = error;
    78          }
    79      });
    80    }
    81  
    82    //add cash asset
    83    addAsset(form: any): Promise<any> {
    84  
    85      //define cash asset object
    86      this.asset = {
    87        $class: "org.decentralized.energy.network.Cash",
    88            "cashID":this.cashID.value,
    89            "currency":this.currency.value,
    90            "value":this.value.value,
    91            "ownerID":this.ownerID.value,
    92            "ownerEntity":this.ownerEntity.value
    93      };
    94  
    95      //update form
    96      this.myForm.setValue({
    97            "cashID":null,
    98            "currency":null,
    99            "value":null,
   100            "ownerID":null,
   101            "ownerEntity":null
   102      });
   103  
   104      //call serviceCash to add cash asset
   105      return this.serviceCash.addAsset(this.asset)
   106      .toPromise()
   107      .then(() => {
   108        this.errorMessage = null;
   109        
   110        //update form
   111        this.myForm.setValue({
   112            "cashID":null,
   113            "currency":null,
   114            "value":null,
   115            "ownerID":null,
   116            "ownerEntity":null 
   117        });
   118      })
   119      .catch((error) => {
   120          if(error == 'Server error'){
   121              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   122          }
   123          else{
   124              this.errorMessage = error;
   125          }
   126      });
   127    }
   128  
   129    //set id
   130    setId(id: any): void{
   131      this.currentId = id;
   132    }
   133  
   134    //get form based on coinsID
   135    getForm(id: any): Promise<any>{
   136  
   137      //call serviceCash to get cash asset object
   138      return this.serviceCash.getAsset(id)
   139      .toPromise()
   140      .then((result) => {
   141  			this.errorMessage = null;
   142        let formObject = {
   143              "cashID":null,
   144              "currency":null,
   145              "value":null,
   146              "ownerID":null,
   147              "ownerEntity":null 
   148        };
   149        
   150        //update formObject
   151        if(result.cashID){
   152          formObject.cashID = result.cashID;
   153        }else{
   154          formObject.cashID = null;
   155        }
   156      
   157        if(result.currency){
   158          formObject.currency = result.currency;
   159        }else{
   160          formObject.currency = null;
   161        }
   162      
   163        if(result.value){
   164          formObject.value = result.value;
   165        }else{
   166          formObject.value = null;
   167        }
   168      
   169        if(result.ownerID){
   170          formObject.ownerID = result.ownerID;
   171        }else{
   172          formObject.ownerID = null;
   173        }
   174      
   175        if(result.ownerEntity){
   176          formObject.ownerEntity = result.ownerEntity;
   177        }else{
   178          formObject.ownerEntity = null;
   179        }
   180        
   181        //set formObject
   182        this.myForm.setValue(formObject);
   183  
   184      })
   185      .catch((error) => {
   186          if(error == 'Server error'){
   187              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   188          }
   189          else if(error == '404 - Not Found'){
   190  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   191          }
   192          else{
   193              this.errorMessage = error;
   194          }
   195      });
   196  
   197    }
   198  
   199    //reset form
   200    resetForm(): void{
   201      this.myForm.setValue({
   202            "cashID":null,
   203            "currency":null,
   204            "value":null,
   205            "ownerID":null,
   206            "ownerEntity":null 
   207          
   208        });
   209    }
   210  
   211  }