Skip to content

Instantly share code, notes, and snippets.

@almino
Last active March 19, 2018 02:08
Show Gist options
  • Save almino/fb4b6cfca0105f112c7ab417bcfd9bf0 to your computer and use it in GitHub Desktop.
Save almino/fb4b6cfca0105f112c7ab417bcfd9bf0 to your computer and use it in GitHub Desktop.
Meus primeiros passos com Firestore
import { Address } from 'ngx-google-places-autocomplete/objects/address';
export interface Price {
value: number
date: Date
place?: Address
url?: URL
}
export interface Measurement {
value: number
unit: string
}
export interface Packaging {
barcode: number
measurement: Measurement
price: Price[]
}
export interface Product {
id: number
name: string
brand?: string
manufacturer?: string
packaging: Packaging[]
}
{
"products": [
{
"id": 1,
"name": "Achocolatado",
"brand": "Nescau",
"manufacturer": "Nestlé",
"packaging": [
{
"barcode": 7891000372401,
"measurement": { "value": 3, "unit": "kg" },
"price": [
{
"value": 57.81,
"date": 1521412653726,
"url": "https://www.ndays.com.br/nescau-caixa-3x3kg?gclid=CjwKCAjwnLjVBRAdEiwAKSGPI2z3axTioMSx_TLG1NgZ62TCMO_yBj-2-gUe8MHL9-tV4DOAU51QCRoCMywQAvD_BwE",
"place": null
}
]
}
]
}
]
}
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import { Product } from './data-model';
@Injectable()
export class ProductService {
protected ref: string = 'products';
protected collection: AngularFirestoreCollection<Product>;
constructor(
private db: AngularFirestore
) { }
getProducts(): Observable<Product[]> {
this.collection = this.db.collection<Product>(this.ref);
return this.collection.snapshotChanges().map(
actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Product;
const id = a.payload.doc.id;
return { id, ...data };
})
}
);
}
getProduct(id: any) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment