<repo_name>JustynaJPL/ProAngular-SportsStore<file_sep>/src/app/model/order.model.ts
import { Injectable } from "@angular/core";
import { Cart } from "./cart.model";

@Injectable()
export class Order {
	public id:number;
	public name:string;
	public address:string;
	public city:string;
	public state:string;
	public zip:string;
	public country:string;
	public shipped:boolean = false;

	constructor(public cart:Cart){

	}
	clear(){
		this.id = null;
		this.name = this.address = this.city = null;
		this.state = this.zip = this.country = null;
		this.shipped = false;
		this.cart.clear();
	}
}<file_sep>/src/app/model/rest.datasource.ts
import { Injectable } from "@angular/core";
import { Http, Request, RequestMethod } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { Product } from "./product.model";
import { Cart } from "./cart.model";
import { Order } from "./order.model";
import "rxjs/add/operator/map";

@Injectable()
export class RestDataSource {
	baseUrl:string;
	auth_token:string;

	constructor(private http:Http){
		this.baseUrl = 'http://localhost:3500/';

	}

	authenticate(user:string, pass:string):Observable<boolean>{
		return this.http.request(new Request({
			method:RequestMethod.Post,
			url:this.baseUrl +"login",
			body: {name:user, password:<PASSWORD> }
		})).map(response => {
			let r = response.json();
			this.auth_token = r.success ? r.token : null;
			return r.success;
		});

	}

	getProducts():Observable<Product[]>{
		return this.sendRequest(RequestMethod.Get, "products") as Observable<Product[]>;
	}

	saveProduct(product:Product):Observable<Product>{
		return this.sendRequest(RequestMethod.Post, 'products' , product, true) as Observable<Product>;
	}

	updateProduct(product):Observable<Product>{
		return this.sendRequest(RequestMethod.Put,('products/'+ product.id), product, true) as Observable<Product>;
	}


	deleteProduct(id:number): Observable<Product>{
		return this.sendRequest(RequestMethod.Delete,('products/'+id), null, true) as Observable<Product>;
	}

	getOrders():Observable<Order[]>{
		return this.sendRequest(RequestMethod.Get,'orders', null, true) as Observable<Order[]>;
	}
	deleteOrder(id:number):Observable<Order>{
		return this.sendRequest(RequestMethod.Delete,('orders/'+id), null, true) as Observable<Order>;
	}
	updateOrder(order:Order):Observable<Order>{
		return this.sendRequest(RequestMethod.Put,('orders/'+order.id), order, true) as Observable<Order>;
	}

	saveOrder(order:Order): Observable<Order>{
		return this.sendRequest(RequestMethod.Post,'orders', order) as Observable<Order>;
	}

	private sendRequest(verb:RequestMethod, url:string,	body?:Product | Order, auth:boolean = false):Observable<Product |Product[] | Order| Order[] > {
		let request = new Request({
			method:verb,
			url:this.baseUrl + url,
			body:body
		});
		if(auth && this.auth_token !=null){
			request.headers.set("Authorization", 'Bearer<'+ this.auth_token +'>');
		}
		
		return this.http.request(request).map(response => response.json());


		
	}

}<file_sep>/src/app/admin/auth.component.ts
import { Component } from "@angular/core";
import { NgForm } from "@angular/forms";
import { Router } from "@angular/router";
import { AuthService } from "../model/auth.service";

@Component({
	moduleId: module.id,
	templateUrl:"auth.component.html"
})
export class AuthComponent{
	public username:string;
	public password:string;
	public errorMessage:string;

	constructor(private router:Router, private auth:AuthService){

	}
	authenticate(form:NgForm){
		if (form.valid) {
			this.auth.authenticate(this.username, this.password).subscribe(response => {
				if(response){
					this.router.navigateByUrl('/admin/main');
				}
				this.errorMessage = "Uwierzytelnianie zakończyło się niepowodzeniem"
			})
			
		}
		else{
			this.errorMessage = "Nieprawidłowe dane";
		}
	}
}<file_sep>/data.js
module.exports = function(){
	return {
		products:[
			{id:1, name:"Kajak", category:"Sporty wodne",
			description:"Łódka przeznaczona dla jednej osoby", price:275	},
			{id:2, name:"<NAME>", category:"Sporty wodne",
			description:"Chronii dodaje uroku", price:48.95	},
			{id:3, name:"Piłka", category:"Piłka nożna",
			description:"Zatwierdzone przez FIFA masa i jakość", price:19.50	},
			{id:4, name:"<NAME>", category:"Piłka narożna",
			description:"Nadadzą twojemu boisku profesjonalny wygląd", price:34.95	},
			{id:5, name:"Stadion", category:"Piłka nożna",
			description:"Składany stadion na 35000 osób", price:79500	},
			{id:6, name:"Czapka", category:"Szachy",
			description:"Zwiększa efektywność mózgu o 75%", price:16	},
			{id:7, name:"<NAME>", category:"Szachy",
			description:"Zmniejsza szanse przeciwnika", price:29.95	},
			{id:8, name:"<NAME>", category:"Szachy",
			description:"Przyjemna gra dla całej rodziny", price:75	},
			{id:9, name:"<NAME>", category:"Szachy",
			description:"Pokryty złotem i wysadzany diamentami król", price:1200},
		],
		orders:[]
	}
}<file_sep>/src/app/store/counter.directive.ts
import {Directive, ViewContainerRef,TemplateRef, Input, Attribute, SimpleChanges } from '@angular/core';

@Directive({
	selector:"[counterOf]"
})
export class CounterDirective {
	constructor(private container:ViewContainerRef, private template: TemplateRef<Object>){

	}
	@Input('counterOf')
	counter:number;

	ngOnChanges(changes:SimpleChanges){
		this.container.clear();
		for (let i = 0; i < this.counter; i++) {
			this.container.createEmbeddedView(this.template,new CounterDirectiveContext(i+1));
		}
	}
}

class CounterDirectiveContext{
	constructor(public $implicit:any){}
}<file_sep>/src/app/admin/productEditor.component.ts
import { Component } from "@angular/core";
import { Router, ActivatedRoute } from "@angular/router";
import { NgForm } from "@angular/forms";
import { Product } from "../model/product.model";
import { ProductRepository } from "../model/product.repository";

@Component({
	moduleId: module.id,
	templateUrl: "productEditor.component.html"
})
export class ProductEditorComponent{
	editing:boolean = false;
	product: Product = new Product();

	constructor(private repository: ProductRepository, private router: Router, activateRoute:ActivatedRoute){
		this.editing = activateRoute.snapshot.params["mode"]== "edit";
		if (this.editing) {
			Object.assign(this.product, repository.getProduct(activateRoute.snapshot.params["id"]));
		}

	}
	save(form:NgForm){
		this.repository.saveProduct(this.product);
		this.router.navigateByUrl("/admin/main/products")

	}
	
}<file_sep>/src/app/model/static.datasource.ts
import { Injectable } from "@angular/core";
import { Product } from "./product.model";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/from";
import { Order } from "./order.model"

@Injectable()
export class StaticDataSource {
  private products: Product[] = [
    new Product(1, "Produkt 1", "Kategoria 1", "Produkt 1 (Kategoria 1)", 100),
    new Product(2, "Produkt 2", "Kategoria 1", "Produkt 2 (Kategoria 1)", 100),
    new Product(3, "Produkt 3", "Kategoria 1", "Produkt 3 (Kategoria 1)", 100),
    new Product(4, "Produkt 4", "Kategoria 1", "Produkt 4 (Kategoria 1)", 100),
    new Product(5, "Produkt 5", "Kategoria 1", "Produkt 5 (Kategoria 1)", 100),
    new Product(6, "Produkt 6", "Kategoria 1", "Produkt 6 (Kategoria 1)", 100),
    new Product(7, "Produkt 7", "Kategoria 1", "Produkt 7 (Kategoria 1)", 100),
    new Product(8, "Produkt 8", "Kategoria 1", "Produkt 8 (Kategoria 1)", 100),
    new Product(9, "Produkt 9", "Kategoria 1", "Produkt 9 (Kategoria 1)", 100),
    new Product(10, "Produkt 10", "Kategoria 2", "Produkt 10 (Kategoria 2)", 100),
    new Product(11, "Produkt 11", "Kategoria 2", "Produkt 11 (Kategoria 2)", 100),
    new Product(12, "Produkt 12", "Kategoria 2", "Produkt 12 (Kategoria 2)", 100),
    new Product(13, "Produkt 13", "Kategoria 2", "Produkt 13 (Kategoria 2)", 100),
    new Product(14, "Produkt 14", "Kategoria 2", "Produkt 14 (Kategoria 2)", 100),
    new Product(15, "Produkt 15", "Kategoria 2", "Produkt 15 (Kategoria 2)", 100),
    new Product(16, "Produkt 16", "Kategoria 2", "Produkt 16 (Kategoria 2)", 100),
    new Product(17, "Produkt 17", "Kategoria 2", "Produkt 17 (Kategoria 2)", 100),
    new Product(18, "Produkt 18", "Kategoria 3", "Produkt 18 (Kategoria 3)", 100),
    new Product(19, "Produkt 19", "Kategoria 3", "Produkt 19 (Kategoria 3)", 100),
    new Product(20, "Produkt 20", "Kategoria 3", "Produkt 20 (Kategoria 3)", 100),
    new Product(21, "Produkt 21", "Kategoria 3", "Produkt 21 (Kategoria 3)", 100),
  ];

  getProducts(): Observable<Product[]>{
  	return Observable.from([this.products]);
  }
  saveOrder(order:Order):Observable<Order>{
    console.log(JSON.stringify(order));
    return Observable.from([order])

  }

}