<repo_name>maildebjyoti/jazzytrip<file_sep>/References.md
#Angular2
https://angular.io/docs/ts/latest/tutorial
https://angular.io/docs/ts/latest/guide/cheatsheet.html
https://angular.io/docs/ts/latest/guide/style-guide.html

#ng2 + Express
https://universal.angular.io/quickstart/

#Others
Debugging:
https://juristr.com/blog/2016/02/debugging-angular2-console/
https://www.pluralsight.com/guides/front-end-javascript/debugging-angular-2-applications
<file_sep>/client/app/Shared/search.service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs';

import { User } from './user';

@Injectable()
export class SearchService {
	constructor(private http: Http) { }

	search(term: string): Observable<User[]> {
		return this.http
			.get(`api/search?name=${term}`)
			.map((data: Response): User[] => {
				console.log(data);
				return data.json();
			});
	}
}
<file_sep>/server/routes.js
var router = require('express').Router();
var four0four = require('./utils/404')();
var data = require('./data');

router.get('/users', getUsers);
router.get('/user/:id', getUser);
router.get('/search', search);
router.get('/searchuser/:user', searchUser);
router.post('/setuser', setUserData);
router.get('/*', four0four.notFoundMiddleware);

module.exports = router;

//////////////
function getUsers(req, res) {
    var token = req.body.token || req.query.token || req.headers['x-access-token'];
    //res.setHeader('x-access-token', token);
    res.status(200).send(data.users);
}

function getUser(req, res, next) {
    var id = req.params.id;
    var person = data.users.filter(function (p) {
        return p.id.toString() === id;
    })[0];

    if (person) {
        res.status(200).send(person);
    } else {
        four0four.send404(req, res, 'person ' + id + ' not found');
    }
}

function search(req, res, next) {
    var name = req.query.name;

    var persons = [];
    data.users.filter(function (p) {
        var firstname = p.firstName.toString().toLowerCase();
        var searchUser = name.toString().toLowerCase();
        if (firstname.indexOf(searchUser) !== -1) {
            persons.push(p);
        }
    });

    res.status(200).send(persons);
}

function searchUser(req, res, next) {
    var user = req.params.user;
    var name = req.query.token;

    var persons = [];
    data.users.filter(function (p) {
        var firstname = p.firstName.toString().toLowerCase();
        var searchUser = user.toString().toLowerCase();
        //console.log(firstname.indexOf(searchUser) !== -1 ? p : []);
        if (firstname.indexOf(searchUser) !== -1) {
            persons.push(p);
        }
    });

    if (persons.length > 0) {
        res.status(200).send(persons);
    } else {
        four0four.send404(req, res, 'Error: ' + user + ' not found');
    }
}

function setUserData(req, res) {
    var data = req.body;
    var token = req.headers['x-access-token'];
    var respObj = {
        status: 'Success',
        token: token,
        data: data,
        query: req.query
    }
    res.status(200).send(respObj);
}<file_sep>/client/app/Dashboard/dashboard.component.ts
import { Component, OnInit } from '@angular/core';

import { Hero } from '../Shared/hero';
import { User } from '../Shared/user';
import { HeroService } from '../Shared/hero.service';
//import { SearchComponent } from './../Shared/search.service';

@Component({
	moduleId: module.id,
	selector: 'my-dashboard',
	templateUrl: 'dashboard.component.html',
	styleUrls: ['dashboard.component.css']
})
export class DashboardComponent implements OnInit {

	heroes: Hero[] = [];
	users: User[] = [];

	test = 'TEST';

	constructor(private heroService: HeroService) { }

	ngOnInit(): void {
		console.log('Dashboard - ngOnInit');
		// this.heroService.getHeroes()
		// 	.then(heroes => this.heroes = heroes.slice(1, 5));
		this.heroService.getHeroes().then(users => this.users = users);
	}
}
<file_sep>/client/app/Shared/hero.service.ts
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Hero } from './hero';
import { User } from './user';
import { Injectable } from '@angular/core';

@Injectable()
export class HeroService {
	private usersUrl = 'api/users';  // URL to web api
	private userUrl = 'api/user';  // URL to web api

	constructor(private http: Http) { }

	getHeroes(): Promise<User[]> {
		return this.http.get(this.usersUrl)
			.toPromise()
			.then(response => response.json() as User[])
			.catch(this.handleError);
	}

	// getHeroes(): Promise<Hero[]> {
	// 	return Promise.resolve(HEROES);
	// }

	// getHeroesSlowly(): Promise<Hero[]> {
	// 	return new Promise(resolve => {
	// 		// Simulate server latency with 2 second delay
	// 		setTimeout(() => resolve(this.getHeroes()), 2000);
	// 	});
	// }

	getHero(id: number): Promise<User> {
		return this.http.get(this.userUrl + '/' + id)
			.toPromise()
			// .then(response => response.json() as User)
			.then((response): User => {
				// console.log(response);
				return response.json();
			})
			.catch(this.handleError);
	}

	private handleError(error: any): Promise<any> {
		console.error('An error occurred', error); // for demo purposes only
		return Promise.reject(error.message || error);
	}
}
