From 5087e6c57d90e701b8651744d95d087586b81734 Mon Sep 17 00:00:00 2001 From: Pete Muir Date: Fri, 10 Mar 2017 15:02:23 -0500 Subject: [PATCH] feat(users): deprecate all existing API, use rxjs --- package.json | 1 + src/app/user/user.service.ts | 127 ++++++++++++++++++++++------------- 2 files changed, 83 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index addbf50..fed5771 100644 --- a/package.json +++ b/package.json @@ -133,6 +133,7 @@ "karma-phantomjs-launcher": "1.0.2", "karma-sourcemap-loader": "0.3.7", "karma-webpack": "2.0.2", + "lodash": "4.17.4", "mocha": "3.2.0", "moment": "2.17.1", "ng-router-loader": "2.1.0", diff --git a/src/app/user/user.service.ts b/src/app/user/user.service.ts index 4735017..feee6bc 100644 --- a/src/app/user/user.service.ts +++ b/src/app/user/user.service.ts @@ -1,15 +1,16 @@ import { Injectable, Inject } from '@angular/core'; import { Headers, Http } from '@angular/http'; +import { Observable } from 'rxjs'; import 'rxjs/add/operator/toPromise'; import 'rxjs/add/operator/catch'; +import { cloneDeep } from 'lodash'; import { Broadcaster } from '../shared/broadcaster.service'; import { AuthenticationService } from '../auth/authentication.service'; import { Logger } from '../shared/logger.service'; import { AUTH_API_URL } from '../shared/auth-api'; import { User } from './user'; -import { Observable } from 'rxjs'; /** * Provides user and user list methods to retrieve current or user list details @@ -17,23 +18,28 @@ import { Observable } from 'rxjs'; * The UserService should be injected at the root of the application to ensure it is a singleton * getUser and getAllUsers return observables that can be subscribed to for information */ - @Injectable() export class UserService { - userData: User = {} as User; - allUserData: User[] = []; + /** + * @deprecated since v0.4.4. Use {@link #loggedInUser} instead. + */ + private userData: User = {} as User; + + /** + * @deprecated since v0.4.4. No replacement method is provided. + */ + private allUserData: User[] = []; - private headers = new Headers({'Content-Type': 'application/json'}); + private headers = new Headers({ 'Content-Type': 'application/json' }); private userUrl: string; // URL to web api private usersUrl: string; // URL to web api - constructor(private http: Http, - private logger: Logger, - private auth: AuthenticationService, - private broadcaster: Broadcaster, - @Inject(AUTH_API_URL) apiUrl: string + private logger: Logger, + private auth: AuthenticationService, + private broadcaster: Broadcaster, + @Inject(AUTH_API_URL) apiUrl: string ) { this.userUrl = apiUrl + 'user'; this.usersUrl = apiUrl + 'users'; @@ -41,67 +47,98 @@ export class UserService { .subscribe(message => { this.resetUser(); }); + // TODO Remove these when we remove deprecated methods + this.loggedInUser.subscribe(val => this.userData = val); + this.getAllUsers().subscribe(val => this.allUserData = val); + } + + /** + * The currently logged in user + */ + get loggedInUser(): Observable { + return this.http + .get(this.userUrl, { headers: this.headers }) + .map(response => { + let userData = cloneDeep(response.json().data as User); + userData.attributes.primaryEmail = userData.attributes.email; + return userData; + }); } + /** + * Get the User object for a given user id, or null if no user is found + * @param userId the userId to search for + */ + getUserByUserId(userId: string): Observable { + return this.getAllUsers().map(val => { + for (let u of val) { + if (userId === u.id) { + return u; + } + } + return null; + }); + } + + /** + * Get the User object for a given username, or null if no user is found + * @param username the username to search for + */ + getUserByUsername(username: string): Observable { + return this.getAllUsers().map(val => { + for (let u of val) { + if (username === u.attributes.username) { + return u; + } + } + return null; + }); + } + + + /** + * @deprecated since v0.4.4. Use {@link #loggedInUser} instead. + */ getSavedLoggedInUser(): User { return this.userData; } + /** + * @deprecated since v0.4.4. No replacement is provided. + */ getLocallySavedUsers(): User[] { return this.allUserData; } /** + * @deprecated since v0.4.4. Use {@link #loggedInUser} instead. * Get currently logged in user * * @returns Observable */ getUser(): Observable { - // Check if we have the user data if not then check if the user is logged in. - // We need the auth key to get the user data. So either we already the data or we don't have the keys - // in either case don't try to get the data. - if (Object.keys(this.userData).length || !this.auth.isLoggedIn()) { - return Observable.of(this.userData); - } else { - return this.http - .get(this.userUrl, {headers: this.headers}) - .map(response => { - let userData = response.json().data as User; - // The reference of this.userData is - // being used in Header - // So updating the value like that - this.userData.attributes = { - fullName: userData.attributes.fullName, - imageURL: userData.attributes.imageURL, - username: userData.attributes.username, - bio: userData.attributes.bio, - primaryEmail: userData.attributes.email - }; - this.userData.id = userData.id; - // this.profile.initDefaults(this.userData); - return this.userData; - }); - } + return this.loggedInUser; } /** + * @deprecated since v0.4.4. No replacement is provided. * Get all users * * @returns Observable */ getAllUsers(): Observable { - if (this.allUserData.length) { - return Observable.of(this.allUserData); - } else { - return this.http - .get(this.usersUrl, {headers: this.headers}) - .map(response => { - this.allUserData = response.json().data as User[]; - return this.allUserData; - }); - } + return this.http + .get(this.usersUrl, { headers: this.headers }) + .map(response => { + this.allUserData = response.json().data as User[]; + return this.allUserData; + }); } + /** + * @deprecated since v0.4.4. No replacement is provided. + * + */ resetUser(): void { this.userData = {} as User; }