-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a364c4f
commit 60459a6
Showing
7 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { Product } from "./product.model"; | ||
|
||
@Injectable() | ||
export class Cart { | ||
|
||
public lines: CartLine[] = []; | ||
public itemCount: number = 0; | ||
public cartPrice: number = 0; | ||
|
||
addLine(product: Product, quantity: number) { | ||
let line = this.lines.find(line => line.product.id == product.id); | ||
if (line != undefined) { | ||
line.quantity += quantity; | ||
} else { | ||
this.lines.push(new CartLine(product, quantity)); | ||
} | ||
this.recalculate(); | ||
} | ||
|
||
updateQuantity(product: Product, quantity: number) { | ||
let line = this.lines.find(line => line.product.id == product.id); | ||
if (line != undefined) { | ||
line.quantity = quantity; | ||
} | ||
this.recalculate(); | ||
} | ||
|
||
removeLine(id: number) { | ||
let index = this.lines.findIndex(line => line.product.id == id); | ||
this.lines.slice(index, 1); | ||
this.recalculate(); | ||
} | ||
|
||
clear() { | ||
this.lines = []; | ||
this.itemCount = 0; | ||
this.cartPrice = 0; | ||
} | ||
|
||
private recalculate() { | ||
this.itemCount = 0; | ||
this.cartPrice = 0; | ||
this.lines.forEach(line => { | ||
this.itemCount += line.quantity; | ||
this.cartPrice += (line.quantity * line.product.price) | ||
}) | ||
} | ||
|
||
} | ||
|
||
export class CartLine { | ||
constructor(public product: Product, public quantity: number) {} | ||
|
||
get lineTotal() { | ||
return this.quantity * this.product.price; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { NgModule } from "@angular/core"; | ||
import { ProductRepository } from "./product.repository"; | ||
import { StaticDataSource } from "./static.datasource"; | ||
import { Cart } from "./cart.model"; | ||
|
||
@NgModule({ | ||
providers: [ProductRepository, StaticDataSource] | ||
providers: [ProductRepository, StaticDataSource, Cart] | ||
}) | ||
|
||
export class ModelModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="float-end"> | ||
<small> | ||
Your cart: | ||
<span *ngIf="cart.itemCount > 0"> | ||
{{ cart.itemCount }} item(s) | ||
{{ cart.cartPrice | currency: "USD":"symbol":"2.2-2"}} | ||
</span> | ||
<span *ngIf="cart.itemCount == 0"> | ||
(empty) | ||
</span> | ||
</small> | ||
<button class="btn btn-sm bg-dark text-white" [disabled]="cart.itemCount == 0"> | ||
<i class="fa fa-shopping-cart"></i> | ||
</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component } from "@angular/core"; | ||
import { Cart } from "../model/cart.model"; | ||
|
||
@Component({ | ||
selector: "cart-summary", | ||
templateUrl: "cartSummary.component.html" | ||
}) | ||
|
||
export class CartSummaryComponent { | ||
constructor(public cart: Cart) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters