Skip to content

Commit

Permalink
advanced voting service
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanCo committed Mar 24, 2019
1 parent 4b0df63 commit 7f00f6e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
16 changes: 1 addition & 15 deletions src/app/components/candidate/candidate.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,14 @@
</button>
</ng-container>
<ng-template #rating>
<button (click)="onVote(1)" mat-mini-fab color="primary" [matBadge]="0" matBadgePosition="above after" matBadgeColor="accent">
<button (click)="onVote(1)" mat-mini-fab color="primary" [matBadge]="candidate.votes ? (candidate.votes[auth.user?.uid] || 0) : 0" matBadgePosition="above after" matBadgeColor="accent">
<mat-icon>thumb_up</mat-icon>
</button>
<button (click)="onVote(-1)" mat-mini-fab color="accent">
<mat-icon>thumb_down</mat-icon>
</button>
</ng-template>
</mat-card-actions>

<!--<mat-card-actions class="ml-auto pr-2" *ngIf="!(auth.user$ | async)">-->
<!--<button (click)="notLoggedIn()" mat-mini-fab>-->
<!--<mat-icon>thumb_up</mat-icon>-->
<!--</button>-->
<!--</mat-card-actions>-->
<!--<mat-card-actions class="ml-auto pr-2" *ngIf="candidate.user.uid !== (auth.user$ | async)?.uid && ratingService.rating$ | async as rating">-->
<!--<button (click)="onUpVote()" mat-mini-fab color="primary" [matBadge]="rating[candidate.user.uid] || 0" matBadgePosition="above after" matBadgeColor="accent">-->
<!--<mat-icon>thumb_up</mat-icon>-->
<!--</button>-->
<!--<button (click)="onDownVote()" mat-mini-fab color="accent">-->
<!--<mat-icon>thumb_down</mat-icon>-->
<!--</button>-->
<!--</mat-card-actions>-->
</mat-card-header>
<img mat-card-image [src]="candidate.imageURL" [alt]="candidate.title">
<mat-card-content>
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/candidate/candidate.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class CandidateComponent implements OnInit {
}

onVote(vote: number) {
this.voteService.setVote(this.candidate, vote);
this.voteService.setVote(this.candidate, this.auth.user, vote);
}

}
29 changes: 20 additions & 9 deletions src/app/services/vote.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material';
import { AuthService } from './auth.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { Candidate } from '../types/candidate.model';

Expand All @@ -10,23 +9,35 @@ import { Candidate } from '../types/candidate.model';
export class VoteService {

constructor(private db: AngularFirestore,
private snackBar: MatSnackBar,
private auth: AuthService) { }
private snackBar: MatSnackBar) { }

setVote(candidate: Candidate, vote: number) {
if (!this.auth.user) {
setVote(candidate: Candidate, user: User, vote: number) {
if (!user) {
this.snackBar.open('Please login to vote');
return;
}

candidate.votes = candidate.votes || {};
return Promise.all([
this.updateCandidateVotes(candidate, user, vote),
this.updateUserVotes(user, vote)
])
}

let currentVote = (candidate.votes[this.auth.user.uid] || 0) + vote;
updateCandidateVotes(candidate: Candidate, user: User, vote: number) {
candidate.votes = candidate.votes || {};
let currentVote = (candidate.votes[user.uid] || 0) + vote;
if (currentVote < 0) currentVote = 0;

candidate.votes[this.auth.user.uid] = currentVote;
candidate.votes[user.uid] = currentVote;
candidate.totalVotes = (candidate.totalVotes || 0) + vote;

return this.db.doc<Candidate>(`candidates/${candidate.user.uid}`).update(candidate);
}

this.db.doc<Candidate>(`candidates/${candidate.user.uid}`).update(candidate)
updateUserVotes(user: User, vote: number) {
let userTotalVotes = (user.totalVotes || 0) + vote;
if (userTotalVotes < 0) userTotalVotes = 0;

return this.db.doc<User>(`users/${user.uid}`).update({ totalVotes: userTotalVotes });
}
}
1 change: 1 addition & 0 deletions src/app/types/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface User {
photoURL?: string;
displayName?: string;
myCustomData?: string;
totalVotes?: number;
}

0 comments on commit 7f00f6e

Please sign in to comment.