Guest User

Untitled

a guest
Oct 23rd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. export class User {
  2.  
  3. user: any;
  4.  
  5. constructor() {
  6.  
  7. }
  8.  
  9. getAge() {
  10.  
  11. return "Age"
  12.  
  13. }
  14. }
  15.  
  16. import { UserService } from '../../services/user.service';
  17. import { AuthService } from '../../services/auth.service';
  18. import { Component, OnInit } from '@angular/core';
  19. import { Router, ActivatedRoute, ParamMap } from '@angular/router';
  20. import 'rxjs/add/operator/switchMap';
  21. import {User} from '../../models/user.model'
  22.  
  23. @Component({
  24. selector: 'app-profile',
  25. templateUrl: './profile.component.html',
  26. styleUrls: ['./profile.component.scss']
  27. })
  28. export class ProfileComponent implements OnInit {
  29.  
  30. user: User;
  31.  
  32. constructor(private authService: AuthService,
  33. private route: ActivatedRoute,
  34. private router: Router,
  35. private userService: UserService) { }
  36.  
  37. ngOnInit() {
  38.  
  39. this.route.params.switchMap((params) => {
  40. let user_id = params['id'];
  41. return this.userService.get(user_id);
  42. }).subscribe((res) => {
  43. this.user = res;
  44. let age = this.user.getAge();
  45. console.log(age);
  46.  
  47. });
  48. }
  49.  
  50. }
  51.  
  52. import { Injectable } from '@angular/core';
  53. import { ApiService } from './api.service';
  54.  
  55. @Injectable()
  56. export class UserService {
  57.  
  58.  
  59. path = 'users/';
  60.  
  61. constructor(
  62. private apiService: ApiService
  63. ) {
  64.  
  65. }
  66.  
  67. all() {
  68. return this.apiService.get(this.path);
  69. }
  70.  
  71. create(user) {
  72.  
  73. return this.apiService.post(this.path, user);
  74.  
  75. }
  76.  
  77. get(user_id) {
  78.  
  79. let endpoint = this.path + user_id;
  80.  
  81. return this.apiService.get(endpoint);
  82. }
  83.  
  84. }
  85.  
  86. let UserSchema = new Schema({
  87.  
  88. email: {
  89. address: {
  90. type: String,
  91. lowercase: true,
  92. //unique: true,
  93.  
  94. },
  95. token: String,
  96. verified: {
  97. type: Boolean,
  98. default: false,
  99. },
  100. },
  101. password: {
  102. type: String,
  103. },
  104.  
  105. socketId: String,
  106. isOnline: Boolean,
  107.  
  108. phone: {
  109. countryCode: {
  110. type: String,
  111. },
  112. number: {
  113. type: String,
  114. },
  115. code: String,
  116. verified: {
  117. type: Boolean,
  118. default: false
  119. },
  120. },
  121.  
  122. jwt: String,
  123.  
  124. profile: {
  125. username: String,
  126. firstname: String,
  127. lastname: String,
  128. dob: String,
  129. gender: String,
  130. level: Number,
  131. location: {
  132. longitude: String,
  133. latitude: String
  134. },
  135. image: String,
  136. introduction: String,
  137.  
  138. following: [],
  139. followers: [],
  140.  
  141. },
  142.  
  143. },
  144. {
  145. timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
  146. });
  147.  
  148. public get(user_id): Observable<User> {
  149. let endpoint = this.path + user_id;
  150. return this.apiService.get(endpoint).map(res => {
  151. let userObj = new User();
  152. userObj.user = res;
  153. return userObj;
  154. });
  155. }
  156.  
  157. this.route.params.switchMap((params) => {
  158. let user_id = params['id'];
  159. return this.userService.get(user_id);
  160. }).subscribe((res) => {
  161.  
  162. let userRef: User = new User();
  163. Object.assign(userRef, res);
  164.  
  165. this.user = userRef;
  166. let age = this.user.getAge();
  167. console.log(age);
  168.  
  169. });
  170.  
  171. export class User {
  172. user: any;
  173. constructor() {}
  174. getAge(){ return "Age"}
  175. public static fromObject(obj):User {
  176. let userRef: User = new User();
  177. Object.assign(userRef, obj);
  178. return userRef;}}
  179.  
  180. this.route.params.switchMap((params) => {
  181. let user_id = params['id'];
  182. return this.userService.get(user_id);
  183. }).subscribe((res) => {
  184.  
  185. this.user = User.fromObject(res);
  186. let age = this.user.getAge();
  187. console.log(age);
  188.  
  189. });
  190.  
  191. get(user_id) {
  192.  
  193. let endpoint = this.path + user_id;
  194.  
  195. return this.apiService.get(endpoint).map(res => new User(){//set your user properties here});
  196. }
Add Comment
Please, Sign In to add comment