Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore'
  3. import { Song } from './song';
  4. import { Review } from './review';
  5.  
  6. @Injectable({
  7. providedIn: 'root'
  8. })
  9. export class SongService {
  10.  
  11. //Path to songs in database
  12. private dbPath = '/songs';
  13.  
  14. //Reference for songs and reviews as an AngularFirestoreCollection from database
  15. songsRef: AngularFirestoreCollection<Song> = null;
  16. reviewsRef: AngularFirestoreCollection<Review> = null;
  17.  
  18. constructor(private db: AngularFirestore) {
  19. //When initiated, get all the song information from database
  20. this.songsRef = db.collection(this.dbPath);
  21. }
  22.  
  23.  
  24. //Create song in database using the Song model and set initial values that are not retrieved from form
  25. async createSong(song: Song) {
  26. song.hidden = false;
  27. song.reviews = 0;
  28. song.total = 0;
  29. return this.songsRef.add({...song});
  30. }
  31.  
  32. getSong(key: string): AngularFirestoreDocument {
  33. return this.db.doc(this.dbPath + '/' + key);
  34. }
  35.  
  36.  
  37. //Add a review to a specific song and update the properties of the song that represent reviews
  38. addReview(song: Song, review: Review) {
  39. let path = this.dbPath + '/' + song.key + '/reviews';
  40. this.reviewsRef = this.db.collection(path);
  41. this.reviewsRef.add({...review});
  42. let songUpdate = {
  43. reviews: (song.reviews + 1),
  44. total: (song.total + review.rating)
  45. };
  46. this.updateSong(song.key, songUpdate);
  47. this.getReviews(song);
  48. }
  49.  
  50. //Get all reviews for a specific song from the database
  51. getReviews(song: Song) {
  52. let path = this.dbPath + '/' + song.key + '/reviews';
  53. let songInfoRef = this.db.collection(path);
  54. return songInfoRef;
  55. }
  56.  
  57. //Function to update any value of a song in the database using its key
  58. updateSong(key: string, value: any): Promise<void> {
  59. return this.songsRef.doc(key).update(value);
  60. }
  61.  
  62. //Function to delete a song from the database using its key
  63. deleteSong(key: string): Promise<void> {
  64. return this.songsRef.doc(key).delete();
  65. }
  66.  
  67. //Function to retrieve all songs from database
  68. getSongsList(): AngularFirestoreCollection<Song> {
  69. return this.songsRef;
  70. }
  71.  
  72. //Function to delete all songs from database
  73. deleteAll() {
  74. this.songsRef.get().subscribe(
  75. querySnapshot => {
  76. querySnapshot.forEach((doc) => {
  77. doc.ref.delete();
  78. });
  79. },
  80. error => {
  81. console.log('Error: ', error);
  82. });
  83. }
  84.  
  85. //Function to toggle a song's hidden attribute to determine if to display it to users
  86. updateHidden(song: Song) {
  87. return this.songsRef.doc(song.key).update({
  88. hidden: !song.hidden
  89. });
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement