File

src/app/services/firebase/firestore/firebase-firestore.service.ts

Description

Service to comunicate with the Firestore database

Example

Index

Properties
Methods

Constructor

constructor(afs: AngularFirestore)

Constructor

Parameters :
Name Type Optional Description
afs AngularFirestore

AngularFire Firestore

Methods

checkDisplayname
checkDisplayname(photographerUrl: string)

Check photographer url

Parameters :
Name Type Optional Description
photographerUrl string

URL

Returns : AngularFirestoreDocument<any>
createEvent
createEvent(event: Event)

Create event

Parameters :
Name Type Optional Description
event Event

Event

Returns : Promise<void>
createPriceList
createPriceList(priceList: PriceList, photographer: string)

Create photographer price list

Parameters :
Name Type Optional Description
priceList PriceList

Price list

photographer string

Photographer UID

Returns : Promise<void>
createPrintingHouse
createPrintingHouse(printingHouse: PrintingHouse)

Create prin ting house

Parameters :
Name Type Optional Description
printingHouse PrintingHouse

Printing house

Returns : Promise<void>
deleteEventImage
deleteEventImage(image: string)

Delete image firestore document

Parameters :
Name Type Optional Description
image string

Image ID

Returns : Promise<void>
deletePhotographerEvent
deletePhotographerEvent(id: string)

Set Event "deleted" value to true

Parameters :
Name Type Optional Description
id string

Event Id

Returns : Promise<void>
deleteUser
deleteUser(uid: string)

Delete user

Parameters :
Name Type Optional Description
uid string

User uid to delete

Returns : Promise<void>
getAllEvents
getAllEvents()

Get all events

getAllPhotographer
getAllPhotographer()

Get all photographer profiles

getAllTransactions
getAllTransactions()

Returns all transactions

getAllUser
getAllUser()

Get all user

getDefautlPrintingHouse
getDefautlPrintingHouse()

Returns default printing house

AngularFirestoreCollection

getEvent
getEvent(id: string)

Get event by id

Parameters :
Name Type Optional Description
id string

ID

getEventOriginalPictures
getEventOriginalPictures(id: string)

Get event original pictures

Parameters :
Name Type Optional Description
id string

ID

getEventPictures
getEventPictures(id: string)

Get event pictures

Parameters :
Name Type Optional Description
id string

ID

getId
getId()

Create a firestore UID

Returns : string
getPhotographerByUrl
getPhotographerByUrl(url: string)

Get photogreapher by url

Parameters :
Name Type Optional Description
url string

URL

getPhotographerEvents
getPhotographerEvents(uid: string)

Get photographer events

Parameters :
Name Type Optional Description
uid string

UID

getPhotographerProfile
getPhotographerProfile(uid: string)

Get photographer profile by id

Parameters :
Name Type Optional Description
uid string

UID

getPopularImages
getPopularImages()

Get most rated top 100 images

getPriceList
getPriceList(photographer: string)

Get photographer price list

Parameters :
Name Type Optional Description
photographer string

Photographer UID

getPrintingHouseById
getPrintingHouseById(id: string)

Returns a printing house

Parameters :
Name Type Optional Description
id string

Printing house id

getPrintingHouseByUser
getPrintingHouseByUser(uid: string)

Retruns a printing house

Parameters :
Name Type Optional Description
uid string

User Id

getTransactionsByPhotographer
getTransactionsByPhotographer(photographer: string)

Get transaction by photographer uid

Parameters :
Name Type Optional Description
photographer string
getTransactionsByUser
getTransactionsByUser(reference_id: string)

Get transaction by user uid

Parameters :
Name Type Optional Description
reference_id string
getUser
getUser(uid: string)

Get user

Parameters :
Name Type Optional Description
uid string

UID

saveTransaction
saveTransaction(transaction: Transaction)

Save transaction

Parameters :
Name Type Optional Description
transaction Transaction

Transaction

Returns : Promise<void>
updateImage
updateImage(image: EventPicture)

Update event picture information

Parameters :
Name Type Optional Description
image EventPicture

Image object

Returns : Promise<void>
updatePhotographerEvent
updatePhotographerEvent(event: Event)

Update an event

Parameters :
Name Type Optional Description
event Event

Event to update

Returns : Promise<void>
updatePriceList
updatePriceList(priceList: PriceList, photographer: string)

Update photographer price list

Parameters :
Name Type Optional Description
priceList PriceList

Price list

photographer string

Photographer UID

Returns : Promise<void>
updatePrintingHouse
updatePrintingHouse(printingHouse: PrintingHouse)
Parameters :
Name Type Optional Description
printingHouse PrintingHouse
Returns : Promise<void>
updateUserData
updateUserData(user: any)

Update user data

Parameters :
Name Type Optional Description
user any

User

Returns : Promise<void>

Properties

Private log
log:
Default value : Log.create('FirebaseFirestoreService')

Logger

import { Injectable } from '@angular/core';
import {
  AngularFirestore,
  AngularFirestoreCollection,
  AngularFirestoreDocument
} from 'angularfire2/firestore';
import { Log } from 'ng2-logger';

import { Event } from '../../../classes/event';
import { PriceList } from '../../../classes/price-list';
import { User } from '../../../classes/user';
import { EventPicture } from '../../../interfaces/event-picture';
import { PhotographerProfile } from '../../../interfaces/photographer-profile';
import { PrintingHouse } from '../../../interfaces/printing-house';
import { Transaction } from '../../../interfaces/transaction';

/**
 * Service to comunicate with the Firestore database
 * @author Daniel Sogl
 */
@Injectable()
export class FirebaseFirestoreService {
  /** Logger */
  private log = Log.create('FirebaseFirestoreService');

  /**
   * Constructor
   * @param  {AngularFirestore} afs AngularFire Firestore
   */
  constructor(private afs: AngularFirestore) {
    this.log.color = 'green';
    this.log.d('Service injected');
  }

  /************************************
   * Firestore: User
   ************************************/

  /**
   * Update user data
   * @param  {any} user User
   * @returns {Promise<void>}
   */
  updateUserData(user: any): Promise<void> {
    this.log.d('Update user in firestore', user);
    const userRef: AngularFirestoreDocument<any> = this.afs.doc(
      `users/${user.uid}`
    );
    if (!user.roles) {
      user = new User(user);
    }
    if (!user.photoUrl) {
      user.photoUrl =
        'https://s3.amazonaws.com/37assets/svn/765-default-avatar.png';
    }
    if (user.photographerUrl) {
      this.afs
        .doc(`photographerUrls/${user.photographerUrl}`)
        .set({ uid: user.uid });
    }
    return userRef.set(JSON.parse(JSON.stringify(user)));
  }

  /**
   * Get user
   * @param  {string} uid UID
   * @returns {AngularFirestoreDocument<User>}
   */
  getUser(uid: string): AngularFirestoreDocument<User> {
    return this.afs.doc<User>(`users/${uid}`);
  }

  /**
   * Check photographer url
   * @param  {string} photographerUrl URL
   * @returns {AngularFirestoreDocument<any>}
   */
  checkDisplayname(photographerUrl: string): AngularFirestoreDocument<any> {
    return this.afs.doc(`photographerUrls/${photographerUrl.toLowerCase()}`);
  }

  /**
   * Get all user
   * @returns {AngularFirestoreCollection<User>}
   */
  getAllUser(): AngularFirestoreCollection<User> {
    return this.afs.collection('users');
  }

  /**
   * Delete user
   * @param  {User} uid User uid to delete
   * @returns {Promise<void>}
   */
  deleteUser(uid: string): Promise<void> {
    return this.afs
      .collection('users')
      .doc(uid)
      .delete();
  }

  /************************************
   * Firestore: Printing houses
   ************************************/

  /**
   * Returns default printing house
   * @returns AngularFirestoreCollection
   */
  getDefautlPrintingHouse(): AngularFirestoreCollection<PrintingHouse> {
    return this.afs.collection('printing-houses', ref =>
      ref.where('isDefault', '==', true)
    );
  }

  /**
   * Returns a printing house
   * @param  {string} id Printing house id
   * @returns {AngularFirestoreDocument<PrintingHouse>}
   */
  getPrintingHouseById(id: string): AngularFirestoreDocument<PrintingHouse> {
    return this.afs.collection('printing-houses').doc(id);
  }

  /**
   * Retruns a printing house
   * @param  {string} uid User Id
   * @returns {AngularFirestoreCollection<PrintingHouse>}
   */
  getPrintingHouseByUser(
    uid: string
  ): AngularFirestoreCollection<PrintingHouse> {
    return this.afs.collection('printing-houses', ref =>
      ref.where('uid', '==', uid)
    );
  }

  /**
   * Create prin ting house
   * @param  {PrintingHouse} printingHouse Printing house
   * @returns {Promise<void>}
   */
  createPrintingHouse(printingHouse: PrintingHouse): Promise<void> {
    return this.afs
      .collection('printing-houses')
      .doc(printingHouse.id)
      .set(JSON.parse(JSON.stringify(printingHouse)));
  }

  updatePrintingHouse(printingHouse: PrintingHouse): Promise<void> {
    return this.afs
      .collection('printing-houses')
      .doc(printingHouse.id)
      .update(JSON.parse(JSON.stringify(printingHouse)));
  }

  /************************************
   * Firestore: Price Lists
   ************************************/

  /**
   * Get photographer price list
   * @param  {string} photographer Photographer UID
   * @returns {AngularFirestoreDocument<PriceList>}
   */
  getPriceList(photographer: string): AngularFirestoreDocument<PriceList> {
    return this.afs.collection('price-lists').doc(photographer);
  }

  /**
   * Create photographer price list
   * @param  {PriceList} priceList Price list
   * @param  {string} photographer Photographer UID
   * @returns {Promise<void>}
   */
  createPriceList(priceList: PriceList, photographer: string): Promise<void> {
    return this.afs
      .collection('price-lists')
      .doc(photographer)
      .set(JSON.parse(JSON.stringify(priceList)));
  }

  /**
   * Update photographer price list
   * @param  {PriceList} priceList Price list
   * @param  {string} photographer Photographer UID
   * @returns {Promise<void>}
   */
  updatePriceList(priceList: PriceList, photographer: string): Promise<void> {
    return this.afs
      .collection('price-lists')
      .doc(photographer)
      .update(JSON.parse(JSON.stringify(priceList)));
  }

  /************************************
   * Firestore: Transactions
   ************************************/

  /**
   * Save transaction
   * @param  {Transaction} transaction Transaction
   * @returns {Promise<void>}
   */
  saveTransaction(transaction: Transaction): Promise<void> {
    return this.afs
      .collection('transactions')
      .doc(transaction.invoice_number)
      .set(JSON.parse(JSON.stringify(transaction)));
  }

  /**
   * Returns all transactions
   * @returns {AngularFirestoreCollection<Transaction>}
   */
  getAllTransactions(): AngularFirestoreCollection<Transaction> {
    return this.afs.collection('transactions');
  }

  /**
   * Get transaction by user uid
   * @param  {string} reference_id
   * @returns {AngularFirestoreCollection<Transaction>}
   */
  getTransactionsByUser(
    reference_id: string
  ): AngularFirestoreCollection<Transaction> {
    return this.afs.collection('transactions', ref =>
      ref.where('reference_id', '==', reference_id)
    );
  }

  /**
   * Get transaction by photographer uid
   * @param  {string} reference_id
   * @returns {AngularFirestoreCollection<Transaction>}
   */
  getTransactionsByPhotographer(
    photographer: string
  ): AngularFirestoreCollection<Transaction> {
    return this.afs.collection('transactions', ref =>
      ref.where('photographer', '==', photographer)
    );
  }

  /************************************
   * Firestore: Events
   ************************************/

  /**
   * Get event by id
   * @param  {string} id ID
   * @returns {AngularFirestoreDocument<Event>}
   */
  getEvent(id: string): AngularFirestoreDocument<Event> {
    return this.afs.doc(`events/${id}`);
  }

  /**
   * Create event
   * @param  {Event} event Event
   * @returns {Promise<void>}
   */
  createEvent(event: Event): Promise<void> {
    return this.afs
      .collection('events')
      .doc(event.id)
      .set(JSON.parse(JSON.stringify(event)));
  }

  /**
   * Get all events
   * @returns {AngularFirestoreCollection<Event>}
   */
  getAllEvents(): AngularFirestoreCollection<Event> {
    return this.afs.collection('events');
  }

  /**
   * Get photographer events
   * @param  {string} uid UID
   * @returns {AngularFirestoreCollection<Event[]>}
   */
  getPhotographerEvents(uid: string): AngularFirestoreCollection<Event> {
    return this.afs.collection('events', ref =>
      ref.where('photographerUid', '==', uid)
    );
  }

  /**
   * Set Event "deleted" value to true
   * @param  {string} id Event Id
   * @returns {Promise<void>}
   */
  deletePhotographerEvent(id: string): Promise<void> {
    return this.afs
      .collection('events')
      .doc(id)
      .update({ deleted: true });
  }

  /**
   * Update an event
   * @param  {Event} event Event to update
   * @returns {Promise<void>}
   */
  updatePhotographerEvent(event: Event): Promise<void> {
    return this.afs
      .collection('events')
      .doc(event.id)
      .update(JSON.parse(JSON.stringify(event)));
  }

  /************************************
   * Firestore: Images
   ************************************/

  /**
   * Get event pictures
   * @param  {string} id ID
   * @returns {AngularFirestoreCollection<EventPicture>}
   */
  getEventPictures(id: string): AngularFirestoreCollection<EventPicture> {
    return this.afs.collection('public-images', ref =>
      ref.where('event', '==', id)
    );
  }

  /**
   * Get event original pictures
   * @param  {string} id ID
   * @returns {AngularFirestoreCollection<EventPicture>}
   */
  getEventOriginalPictures(
    id: string
  ): AngularFirestoreCollection<EventPicture> {
    return this.afs.collection('original-images', ref =>
      ref.where('event', '==', id)
    );
  }

  /**
   * Delete image firestore document
   * @param  {string} image Image ID
   * @returns {Promise<void>}
   */
  deleteEventImage(image: string): Promise<void> {
    return this.afs
      .collection('public-images')
      .doc(image)
      .delete();
  }

  /**
   * Update event picture information
   * @param  {EventPicture} image Image object
   * @returns {Promise<void>}
   */
  updateImage(image: EventPicture): Promise<void> {
    return this.afs
      .collection('public-images')
      .doc(image.id)
      .update(JSON.parse(JSON.stringify(image)));
  }

  /**
   * Get most rated top 100 images
   * @returns {AngularFirestoreCollection<EventPicture>}
   */
  getPopularImages(): AngularFirestoreCollection<EventPicture> {
    return this.afs.collection('public-images', ref =>
      ref
        .where('ratings', '>', 0)
        .orderBy('ratings', 'desc')
        .limit(100)
    );
  }

  /************************************
   * Firestore: Photographer Profile
   ************************************/

  /**
   * Get photographer profile by id
   * @param  {string} uid UID
   * @returns {AngularFirestoreDocument<PhotographerProfile>}
   */
  getPhotographerProfile(
    uid: string
  ): AngularFirestoreDocument<PhotographerProfile> {
    return this.afs.doc(`/photographer/${uid}`);
  }

  /**
   * Get photogreapher by url
   * @param  {string} url URL
   * @returns {AngularFirestoreCollection<PhotographerProfile>}
   */
  getPhotographerByUrl(
    url: string
  ): AngularFirestoreCollection<PhotographerProfile> {
    return this.afs.collection('photographer', ref =>
      ref.where('profileUrl', '==', url)
    );
  }

  /**
   * Get all photographer profiles
   * @returns {AngularFirestoreCollection<PhotographerProfile>}
   */
  getAllPhotographer(): AngularFirestoreCollection<PhotographerProfile> {
    return this.afs.collection('photographer');
  }

  /************************************
   * Firestore: Utility
   ************************************/

  /**
   * Create a firestore UID
   * @returns {string}
   */
  getId(): string {
    return this.afs.createId();
  }
}

results matching ""

    No results matching ""