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

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