'How to implement like and dislike a post in Spring Boot?

I am working for a mobile application and using Spring Boot as a backend and building REST APIs. So now, I need to implement like and dislike feature. That is if a user hits like (thumbs up icon) then count should be increased and that count should be displayed there and if hits dislike (thumbs down icon) then like count should be decreased and count should be displayed then also. But what I wrote for like and dislike is not as expected as it should be.

Actual working should be as follows:

1. If user A hits like to post A, then count of post A should be increased to 1. 2. If user B also hits like to post A then count of post A should be increased from 1 to 2. (means total like count of Post A is 2 now) 3. If user A again hits like button then it will be counted as dislike and count should get decreased from 2 to 1. (Now current count should be 1 again for post A).

So the like-dislike feature should work this way. But my code is not working as expected.

Below is my REST controller:

@RequestMapping(value = RestApiUrl.likepost, method = {RequestMethod.POST, RequestMethod.GET }, produces = {MediaType.APPLICATION_JSON_VALUE }) 
       public ResponseEntity<?> likepost(HttpServletRequest request, @RequestParam("userid") String userid, @RequestParam("postid") String smsid) {
      CustomResponse = ResponseFactory.getResponse(request);
      int lcount = 0, dcount = 0, likcount = 0,  postlike = 0;
      FavouritePost favPost = null;
      Post messageid = null, newpost = null;
              try {
                  Long postid = Long.parseLong(smsid);
                  Long uid = Long.parseLong(userid);
                  User userbyid = userDao.findByUserid(uid);
                  messageid = postDao.findByPostid(postid);
                  postlike = messageid.getLike_count();
                  
                  favPost = favouritePostDao.findByPostidAndUserid(messageid, userbyid);
                  if(favPost == null) {
                      favPost = new FavouritePost();
                      likcount = favPost.getLikecount();
                      
                      lcount = likcount + 1;
    
                      favPost.setUserid(userbyid);
                      favPost.setCreatedby(userbyid);
                      favPost.setPostid(messageid);
                      favPost.setLikecount(lcount);
                      favouritePostDao.save(favPost); 
                      
                      messageid.setModifiedby(userbyid);
                      messageid.setModifieddate(new Date());
                      messageid.setLike_count(postlike+1);
                      newpost = postDao.save(messageid);
                      
                      CustomResponse.setResponse(newpost);
                      CustomResponse.setStatus(CustomStatus.OK);
                      CustomResponse.setStatusCode(CustomStatus.OK_CODE);
                      CustomResponse.setResponseMessage(CustomStatus.SuccessMsg);
            
                  }else {
                      dcount = favPost.getDislikecount();
                      favPost.setUserid(userbyid);
                      favPost.setModifiedby(userbyid);
                      favPost.setModifieddate(new Date());;
                      favPost.setPostid(messageid);
                      favPost.setDislikecount(dcount+1);;
                       favouritePostDao.save(favPost);
                       
                       messageid.setModifiedby(userbyid);
                       messageid.setModifieddate(new Date());
                       if(postlike > 0) {
                              int count =postlike-1;
                              messageid.setLike_count(count);
                      }else {
                          int count =postlike+1;
                          messageid.setLike_count(count);
                      }
                       newpost = postDao.save(messageid);
                       
                       CustomResponse.setResponse(newpost);
                       CustomResponse.setStatus(CustomStatus.OK);
                       CustomResponse.setStatusCode(CustomStatus.OK_CODE);
                       CustomResponse.setResponseMessage(CustomStatus.SuccessMsg);
                  }
            } catch (Exception e) {
                e.printStackTrace();
                CustomResponse.setResponse("Error occurred! please try again");
                CustomResponse.setStatus(CustomStatus.Error);   
                CustomResponse.setStatusCode(CustomStatus.Error_CODE);
                CustomResponse.setResponseMessage(CustomStatus.ErrorMsg);
            }
        return new ResponseEntity<ResponseDao>(CustomResponse, HttpStatus.OK); 
      }

Please help me in the implementation of like-dislike feature for a mobile application. A concept/idea would be great of how to do this.

Like and dislike post



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source