Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

asp.net - How to return boolean in Angular

I have a backend method called LikeExists() to verify if a certain user has liked a certain post.

public async Task<bool> LikeExists(int postId)
        {
            var post = await _postRepository.GetPostByIdAsync(postId);
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            if (_context.Likes.Where(i => i.PostId == post.Id && i.UserId == user.Id).FirstOrDefault() != null) return true;

            return false;
        }

The method works fine in Postman, but it does not do the job in Angular. If a user presses a like button I first want to check if this user has already liked this post. If he has, he will unlike it and the like will be deleted from the database. If he hasn't liked it, he will like it and the like will be saved in the database.

likeExists(){
     this.postService.likeExists(this.post.id).subscribe((response: boolean) =>{
      this.like = response;
    });
  }

  likePost() {
    if(this.likeExists){
    this.postService.likePost(this.post.id, this.model).subscribe((response: Like) => {
      this.likee = response;
      console.log(response);
      this.toastr.success('Liked');
    }, error => {
      console.log(error);
      this.toastr.error(error.error);
    });
  } else {
      this.postService.deleteLike(this.post.id).subscribe(() => {
        this.toastr.success('Unliked');
      }, error => {
        console.log(error);
      })
    }
  }

The problem is it always enters the if{} clause and never the else{} clause. The method below returns an Observable. I think the problem is that it must return a boolean. How can I make this work?

This is the method in the postService:

likeExists(postId: number) {
    return this.http.get(this.baseUrl + 'like/exists/' + postId);
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try avoiding nested subscription since it will result in an unreadable and hard to maintain code, use rxjs pipes with operators instead, try something like this:

let id= this.post.id;
let likeExists$ = this.postService.likeExists(id);

likeExists$
    .pipe(
        switchMap(likeExists => {
           if (likeExists) {
           // delete like
               return this.postService.deleteLike(id);
           }
           // otherwise addlike
               return this.postService.addLike(id);
           
        })
    ).subscribe(
       res=> this.toastr.success('Success'), 
       err=> this.toastr.error('Failed')
       );

or even shorter

let id= this.post.id;
let likeExists$ = this.postService.likeExists(id);

likeExists$
    .pipe(switchMap(
       liked => liked ? this.postService.deleteLike(id) : this.postService.addLike(id)}))
    .subscribe(
       res=> this.toastr.success('Success'), 
       err=> this.toastr.error('Failed')
       );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...