1
\$\begingroup\$

My aim is to prevent authenticated users from voting more than once. I am using angularfire2. The code gives me the expected behavior and I was wondering the better way of doing this. The rateStatus(status: Status) function works, but is there any better way I would or should write this?

service.ts

@Injectable()
export class StatusService {
path:any;
urlService: 'eStatus';
_uid;
_username;
user: {};
_tags;
private isloggedIn = new BehaviorSubject(false);
statusList: FirebaseListObservable<any[]>;
  constructor(
    private storeHelper: StoreHelper,
    private af: AngularFire,
    private _http: Http
  ) {

    this.af.auth.subscribe(user => {
       if(user) {
           this._uid = user.uid;
           this._username = user.auth.displayName;
       }});

    this.statusList = this.af.database.list('eStatus',  {
      query: {
        orderByChild: 'createdAt'
      }
    });

  }


  rateStatus(status: Status) {
    this.path = this.af.database.object(`eStatus/${status.sid}`);
    let rStatusUser = this.af.database.object(`eRatingUsers/${status.sid}`);
    rStatusUser.subscribe(value => {
      if(value.uid == this._uid){
        console.log("already voted");
      } else {
         return rStatusUser.set({
          uid: this._uid
        }) 
        .then(_ =>  this.path.update({ rating: status.rating + 1 }))
        .catch(err => console.log(err, 'You dont have access!')); 
      }

    });

  }


}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.