Commit 331baf6d authored by David Hardy's avatar David Hardy

Fixed image service

parent 95e20a8c
import {Injectable} from '@angular/core';
import {combineLatest, from, Observable, of} from 'rxjs';
import {combineLatest, from, Observable, of, timer} from 'rxjs';
import {LoginService} from './login.service';
import {flatMap, map, switchMap, take, tap} from 'rxjs/operators';
import {flatMap, map, switchMap, take, takeWhile, tap} from 'rxjs/operators';
// import {AngularFirestore} from '@angular/fire/firestore';
// import {AngularFireStorage} from '@angular/fire/storage';
import {ImageData, VoteContainer} from '@apu/model';
......@@ -58,9 +58,9 @@ export class ImageService {
// });
// // ######################
// return this.db.collection<ImageData>(`images`).valueChanges();
return this.db.collection<ImageData>(`images`).valueChanges();
return of(this.myData);
}
delete(imageId: string): void {
......@@ -80,42 +80,71 @@ export class ImageService {
* Toggle the comments in this method
*/
const fileType = file.name.split('.').reverse()[0];
return this.loginService.userInfoProvider$().pipe(
take(1),
switchMap(info => this.db.collection<ImageData>(`images`).add({
user: info.uid,
username: info.displayName,
title: title
})),
tap(ref => this.db.doc<ImageData>(`images/${ref.id}`).set({id: ref.id}, {merge: true})),
map(ref => this.storage.upload(`images/${ref.id}.${fileType}`, file)),
switchMap(uploadTask => uploadTask.percentageChanges())
return timer(0, 200).pipe(
map(value => value * 25),
takeWhile(percentage => percentage <= 100)
);
// const fileType = file.name.split('.').reverse()[0];
// return this.loginService.userInfoProvider$().pipe(
// take(1),
// switchMap(info => this.db.collection<ImageData>(`images`).add({
// user: info.uid,
// username: info.displayName,
// title: title
// })),
// tap(ref => this.db.doc<ImageData>(`images/${ref.id}`).set({id: ref.id}, {merge: true})),
// map(ref => this.storage.upload(`images/${ref.id}.${fileType}`, file)),
// switchMap(uploadTask => uploadTask.percentageChanges())
// );
}
castVoteTask$(imageId: string, vote: Vote): Observable<void> {
return this.loginService.uidProvider$().pipe(
take(1),
switchMap(uid => this.db.doc<VoteContainer>(`images/${imageId}/votes/${uid}`).set({vote: vote}))
);
/**
* FixMe: Step 3.1
* Toggle the comments in this method
*/
// return this.loginService.uidProvider$().pipe(
// take(1),
// switchMap(uid => this.db.doc<VoteContainer>(`images/${imageId}/votes/${uid}`).set({vote: vote}))
// );
return of();
}
getVoteScoreTask$(imageId: string): Observable<number> {
return of('').pipe(
flatMap(() => combineLatest(
this.db.collection<VoteContainer>(`images/${imageId}/votes/`, ref => ref.where('vote', '==', 'UP')).valueChanges(),
this.db.collection<VoteContainer>(`images/${imageId}/votes/`, ref => ref.where('vote', '==', 'DOWN')).valueChanges()
)),
map(([upVotes, downVotes]) => (upVotes ? upVotes.length : 0) - (downVotes ? downVotes.length : 0))
);
/**
* FixMe: Step 3.1
* Toggle the comments in this method
*/
// return of('').pipe(
// flatMap(() => combineLatest(
// this.db.collection<VoteContainer>(`images/${imageId}/votes/`, ref => ref.where('vote', '==', 'UP')).valueChanges(),
// this.db.collection<VoteContainer>(`images/${imageId}/votes/`, ref => ref.where('vote', '==', 'DOWN')).valueChanges()
// )),
// map(([upVotes, downVotes]) => (upVotes ? upVotes.length : 0) - (downVotes ? downVotes.length : 0))
// );
return of(42);
}
getMyVoteProvider$(imageId: string): Observable<Vote> {
return this.loginService.uidProvider$().pipe(
take(1),
switchMap(uid => this.db.doc<VoteContainer>(`images/${imageId}/votes/${uid}`).valueChanges()),
map(vc => vc ? Vote[vc.vote] : Vote[Vote.BLANK])
);
/**
* FixMe: Step 3.1
* Toggle the comments in this method
*/
// return this.loginService.uidProvider$().pipe(
// take(1),
// switchMap(uid => this.db.doc<VoteContainer>(`images/${imageId}/votes/${uid}`).valueChanges()),
// map(vc => vc ? Vote[vc.vote] : Vote[Vote.BLANK])
// );
return of(Vote.BLANK);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment