user.service.ts 4.29 KB
import { Injectable, Inject } from '@angular/core';
//import { HttpClient, HttpParams, HttpRequest} from "@angular/common/http";
import { Http, Response, Headers, RequestOptions, HttpModule } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { User } from '../UserEntity/datamodel';
import 'rxjs/add/operator/do';
import { Observable } from 'rxjs/Observable';
import { GlobalService } from '../../Shared/global';
@Injectable()
export class UserService {

  constructor(private http: Http, private commonService: GlobalService ) { } 
  //////////Get User Detail///////////////
  GetUserById() {
    debugger;
    return this.http.get(this.commonService.resourceBaseUrl + "/GetUserProfile/"+this.commonService.UserId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));;

  } 
  //////////Update User Detail///////////////
  UpdateUserProfileById(obj: User) {    
    //let options = new RequestOptions({ headers: this.headers });
    return this.http.post(this.commonService.resourceBaseUrl + "/UpdateProfile", obj)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
   //////////// End /////////////////////

  //////////Update User Password///////////
  ChangeUserPassword(obj: any) {
    //let options = new RequestOptions({ headers: this.headers });
    var jsonData = { 'id': obj.userId, 'newPassword': obj.newPassword };
    console.log(obj);
    var headers = new Headers({
      'Content-Type': 'application/json'
    });
    return this.http.post(this.commonService.resourceBaseUrl + "/ChangeUserPassword",
      JSON.stringify(jsonData), { headers: headers })
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  //////////// End /////////////////////
    //////////Update User Userid///////////////
  UpdateUserId(obj: User) {
    //let options = new RequestOptions({ headers: this.headers });
    return this.http.post(this.commonService.resourceBaseUrl + "/UpdateUserId", obj)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  /// End ////

  /// Users List Form///////

  GetUserType() {
    return this.http.get(this.commonService.resourceBaseUrl + "/GetUserType/"+this.commonService.UserType)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  GetAccountType() {
    return this.http.get(this.commonService.resourceBaseUrl + "/GetAccountType/"+this.commonService.AccountType)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  GetUserList(obj: any) {
    return this.http.get(this.commonService.resourceBaseUrl + "/Users?firstname=" + obj.FirstName +
      "&lastname=" + obj.LastName +
      "&emailid=" + obj.EmailId +
      "&accountnumber=" + obj.AccountNumber +
      "&usertypeid=" + obj.UserTypeId +
      "&accounttypeid=" + obj.AccountTypeId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }
  /// End Users /////

  /// Add User Entity///
  GetUserTypeByLicenseType(obj: any) {
    debugger;
    return this.http.get(this.commonService.resourceBaseUrl + "/GetUserTypebyLicenseId?UserTypeId=" + this.commonService.UserType + "&LicenseId=" + obj.AccountNumberId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  GetAccountNumber() {
    return this.http.get(this.commonService.resourceBaseUrl + "/GetAccountNumber").
      map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  GetProductEdition(obj: any) {
    return this.http.get(this.commonService.resourceBaseUrl + "/GetProductEdition?LicenseId=" + obj.AccountNumberId)
      .map(this.extractData)
      .catch((res: Response) => this.handleError(res));
  }

  /// End //////

  extractData(res: Response) {
    debugger;
    
    let body = res.json();
    return body;
  }
  handleError(error: any) {
    debugger;
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }
  
  
}