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

     1  import { Component, OnInit, Input } from '@angular/core';
     2  import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
     3  import { BankService } from './Bank.service';   //from Bank.service.ts
     4  import 'rxjs/add/operator/toPromise';
     5  
     6  //provide associated components
     7  @Component({
     8  	selector: 'app-Bank',
     9  	templateUrl: './Bank.component.html',
    10  	styleUrls: ['./Bank.component.css'],
    11    	providers: [BankService]
    12  })
    13  
    14  //BankComponent class
    15  export class BankComponent {
    16  
    17    //define variables
    18    myForm: FormGroup;
    19    
    20    private allBanks;
    21    private bank;
    22    private currentId;
    23    private errorMessage;
    24  
    25    private coins;
    26    private energy;
    27    private cash;
    28    
    29    //initialize form variables
    30    bankID = new FormControl("", Validators.required);
    31    name = new FormControl("", Validators.required);      
    32    coinsValue = new FormControl("", Validators.required);      
    33    cashValue = new FormControl("", Validators.required);
    34    cashCurrency = new FormControl("", Validators.required);
    35        
    36    constructor(private serviceBank:BankService, fb: FormBuilder) {
    37      //intialize form
    38      this.myForm = fb.group({         
    39            bankID:this.bankID,
    40            name:this.name,      
    41            coinsValue:this.coinsValue,
    42            cashValue:this.cashValue,
    43            cashCurrency:this.cashCurrency          
    44      });
    45    };
    46  
    47    //on page initialize, load all banks
    48    ngOnInit(): void {
    49      this.loadAll();
    50    }  
    51  
    52    //load all banks and the coins and cash assets associated to it 
    53    loadAll(): Promise<any>  {
    54      
    55      //retrieve all banks in the bankList array
    56      let bankList = [];
    57  
    58      //call serviceBank to get all bank objects
    59      return this.serviceBank.getAllBanks()
    60      .toPromise()
    61      .then((result) => {
    62        this.errorMessage = null;
    63        
    64        //append bankList with the bank objects returned
    65        result.forEach(bank => {
    66          bankList.push(bank);
    67        });     
    68      })
    69      .then(() => {
    70  
    71        //for each bank, get the associated coins and cash asset
    72        for (let bank of bankList) {
    73  
    74          //get coinsID from the string
    75          var splitted_coinsID = bank.coins.split("#", 2); 
    76          var coinsID = String(splitted_coinsID[1]);
    77  
    78          //call serviceBank to get coins asset
    79          this.serviceBank.getCoins(coinsID)
    80          .toPromise()
    81          .then((result) => {
    82            this.errorMessage = null;
    83            //update bank
    84            if(result.value){
    85              bank.coinsValue = result.value;
    86            }
    87          });
    88          
    89          //get cashID from the string
    90          var splitted_cashID = bank.cash.split("#", 2); 
    91          var cashID = String(splitted_cashID[1]);
    92          
    93          //call serviceBank to get cash asset
    94          this.serviceBank.getCash(cashID)
    95          .toPromise()
    96          .then((result) => {
    97            this.errorMessage = null;
    98            //update bank
    99            if(result.value){
   100              bank.cashValue = result.value;
   101            }
   102            if(result.currency){
   103              bank.cashCurrency = result.currency;
   104            }
   105          });
   106        }
   107  
   108        //assign bankList to allBanks
   109        this.allBanks = bankList;
   110      });
   111    }
   112  
   113    //add bank participant
   114    addBank(form: any): Promise<any> {
   115  
   116      //create assets for bank and the bank on the blockchain network
   117      return this.createAssetsBank()
   118        .then(() => {           
   119          this.errorMessage = null;
   120  
   121          //set form values to null
   122          this.myForm.setValue({
   123              "bankID":null,
   124              "name":null,
   125              "coinsValue":null,
   126              "cashValue":null,
   127              "cashCurrency":null
   128          });
   129        })
   130      .catch((error) => {
   131          if(error == 'Server error'){
   132              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   133          }
   134          else if (error == '500 - Internal Server Error') {
   135            this.errorMessage = "Input error";
   136          }
   137          else{
   138              this.errorMessage = error;
   139          }
   140      });
   141    }
   142  
   143    //create coins and cash assets associated with the Bank, followed by the Bank
   144    createAssetsBank(): Promise<any> {
   145  
   146      //create coins asset json
   147      this.coins = {
   148        $class: "org.decentralized.energy.network.Coins",
   149            "coinsID":"CO_" + this.bankID.value,
   150            "value":this.coinsValue.value,
   151            "ownerID":this.bankID.value,
   152            "ownerEntity":'Bank'
   153      };    
   154  
   155      //create cash asset json
   156      this.cash = {
   157        $class: "org.decentralized.energy.network.Cash",
   158            "cashID":"CA_" + this.bankID.value,
   159            "currency":this.cashCurrency.value,
   160            "value":this.cashValue.value,
   161            "ownerID":this.bankID.value,
   162            "ownerEntity":'Bank'        
   163      };
   164      
   165      //create bank participant json
   166      this.bank = {
   167        $class: "org.decentralized.energy.network.Bank",
   168            "bankID":this.bankID.value,
   169            "name":this.name.value,
   170            "coins":"CO_" + this.bankID.value,
   171            "cash":"CA_" + this.bankID.value
   172      };
   173      
   174      //call serviceBank to add coins asset, pass created coins asset json as parameter
   175      return this.serviceBank.addCoins(this.coins)
   176      .toPromise()
   177  		.then(() => {    
   178        
   179        //call serviceBank to add cash asset, pass created cash asset json as parameter
   180        this.serviceBank.addCash(this.cash)
   181        .toPromise()
   182        .then(() => {
   183  
   184          //call serviceBank to add bank participant, pass created bank participant json as parameter
   185          this.serviceBank.addBank(this.bank)
   186          .toPromise()
   187          .then(() => {
   188              //reload page to display the created bank
   189              location.reload();
   190          });
   191  		  });
   192  		});
   193    }
   194   
   195    //allow update name of Bank
   196    updateBank(form: any): Promise<any> {
   197      
   198      //create json of bank participant to update name
   199      this.bank = {
   200        $class: "org.decentralized.energy.network.Bank",          
   201                "name":this.name.value,                      
   202                "coins": "resource:org.decentralized.energy.network.Coins#CO_" + form.get("bankID").value,
   203                "cash": "resource:org.decentralized.energy.network.Cash#CA_" + form.get("bankID").value,
   204      };
   205      //call serviceBank to update bank, pass bankID of which bank to update as parameter
   206      return this.serviceBank.updateBank(form.get("bankID").value,this.bank)
   207  		.toPromise()
   208  		.then(() => {
   209  			this.errorMessage = null;
   210  		})
   211  		.catch((error) => {
   212              if(error == 'Server error'){
   213  				this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   214  			}
   215              else if(error == '404 - Not Found'){
   216  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   217  			}
   218  			else{
   219  				this.errorMessage = error;
   220  			}
   221      });
   222    }
   223  
   224    //delete Bank and the coins and cash assets associated to it
   225    deleteBank(): Promise<any> {
   226  
   227      //call serviceBank to delete bank, pass bankID as parameter
   228      return this.serviceBank.deleteBank(this.currentId)
   229  		.toPromise()
   230  		.then(() => {
   231        this.errorMessage = null;
   232        
   233        //call serviceBank to delete coins asset, pass coinsID as parameter
   234        this.serviceBank.deleteCoins("CO_"+this.currentId)
   235        .toPromise()
   236        .then(() => {
   237              
   238              //call serviceBank to delete cash asset, pass cashID as parameter
   239              this.serviceBank.deleteCash("CA_"+this.currentId)
   240              .toPromise()
   241              .then(() => {
   242                  console.log("Deleted")
   243            });
   244        });            
   245  		})
   246  		.catch((error) => {
   247              if(error == 'Server error'){
   248  				this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   249  			}
   250  			else if(error == '404 - Not Found'){
   251  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   252  			}
   253  			else{
   254  				this.errorMessage = error;
   255  			}
   256      });
   257    }
   258  
   259    //set id
   260    setId(id: any): void{
   261      this.currentId = id;
   262    }
   263  
   264    //get form based on bankID
   265    getForm(id: any): Promise<any>{
   266  
   267      //call serviceBank to get bank participant object
   268      return this.serviceBank.getBank(id)
   269      .toPromise()
   270      .then((result) => {
   271  			this.errorMessage = null;
   272        let formObject = {        
   273              "bankID":null,                  
   274              "name":null,
   275              "coinsValue":null,                      
   276              "cashValue":null,          
   277              "cashCurrency":null 
   278        };
   279  
   280        //update formObject
   281        if(result.bankID){
   282          formObject.bankID = result.bankID;
   283        }else{
   284          formObject.bankID = null;
   285        }
   286        
   287        if(result.name){
   288          formObject.name = result.name;
   289        }else{
   290          formObject.name = null;
   291        }              
   292  
   293        this.myForm.setValue(formObject);
   294  
   295      })
   296      .catch((error) => {
   297          if(error == 'Server error'){
   298              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   299          }
   300          else if(error == '404 - Not Found'){
   301  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   302          }
   303          else{
   304              this.errorMessage = error;
   305          }
   306      });
   307  
   308    }
   309  
   310    //reset form
   311    resetForm(): void{
   312      this.myForm.setValue({           
   313            "bankID":null, 
   314            "name":null,       
   315            "coinsValue":null,
   316            "cashValue":null,
   317            "cashCurrency":null
   318        });
   319    }  
   320  
   321  }
   322  
   323  
   324  
   325  
   326