Skip to content

Commit

Permalink
follow and unfollow service
Browse files Browse the repository at this point in the history
follow and unfollow logic
  • Loading branch information
pvishnu1 committed Jun 9, 2022
1 parent 13360a3 commit 91d1e48
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,126 @@
package com.ourapp.socialmedia.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ourapp.socialmedia.entity.FollowData;
import com.ourapp.socialmedia.entity.Followers;
import com.ourapp.socialmedia.entity.User;
import com.ourapp.socialmedia.entity.UserFollowInfo;
import com.ourapp.socialmedia.repository.FollowersRespository;
import com.ourapp.socialmedia.repository.UserRepository;
import com.ourapp.socialmedia.view.ResponseObjectService;

@RestController
@RequestMapping("/ourApp/follow")
public class FollowersAndFollowingController {

@Autowired
FollowersRespository followersRespository;

@Autowired
UserRepository userRepository;

private static final Logger logger = LoggerFactory.getLogger(PostController.class);

@PostMapping

@PostMapping("/followUser")
public ResponseEntity<String> followUser(@RequestBody FollowData followdata) {
logger.debug("followUser is ====>{}",followdata);
try {

followersRespository.save(new Followers(followdata.getFollower(), followdata.getFollowing()));
User user = userRepository.findByUserName(followdata.getUserName());
logger.debug("user is ====>{}",user);
followersRespository.save(new Followers(user.getId(), followdata.getId()));
return ResponseEntity.ok("followed ");
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@PostMapping("/unFollowUser")
public ResponseEntity<String> unFollowUser(@RequestBody FollowData followdata) {
logger.debug("unFollowUser is ====>{}",followdata);
try {
User user = userRepository.findByUserName(followdata.getUserName());
logger.debug("user is ====>{}",user);
followersRespository.unFollowUser(user.getId(), followdata.getId());
return ResponseEntity.ok("unFollowed ");
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}


@GetMapping("/followers/{userName}")
public int followersForUser(@PathVariable String userName){
User user = userRepository.findByUserName(userName);
return followersRespository.findFollowersById(user.getId()).size();
}


@GetMapping("/following/{userName}")
public int followingForUser(@PathVariable String userName){
User user = userRepository.findByUserName(userName);
return followersRespository.findFollowingById(user.getId()).size();
}

@GetMapping("/user/follow/available/{userName}")
public ResponseEntity<ResponseObjectService> getUsersAvailableForFollow(@PathVariable String userName) {
ResponseObjectService responseObj = new ResponseObjectService();
logger.debug("userName is ====>{}",userName);
try {
List<UserFollowInfo> userFollowInfo = new ArrayList<>();
User user = userRepository.findByUserName(userName);
logger.debug("user is ====>{}",user);
List<User> users = userRepository.findAll();
logger.debug("users is ====>{}",users);

if(user != null && users != null) {
List<Long> alreadyFollowingList = followersRespository.findFollowingById(user.getId());
logger.debug("alreadyFollowingList is ====>{}",alreadyFollowingList);
logger.debug("alreadyFollowingList after is ====>{}",alreadyFollowingList);

for(User userList : users) {
if(!user.getId().equals(userList.getId())) {
UserFollowInfo userInfo = new UserFollowInfo();
userInfo.setUserId(userList.getId());
userInfo.setUserName(userList.getUserName());
userInfo.setEmailId(userList.getEmailId());
if(alreadyFollowingList.contains(userList.getId())) {
userInfo.setFollowing(true);
}
int followersCount = followersRespository.findFollowersById(userList.getId()).size();
userInfo.setFollowersCount(followersCount > 0 ? String.valueOf(followersCount) : "0");
userFollowInfo.add(userInfo);
}
}
responseObj.setStatus("success");
responseObj.setMessage("success");
responseObj.setPayload(userFollowInfo);
logger.debug("UserFollowInfo list is ====>{}",userFollowInfo);
}else {
throw new Exception("FAILED");
}
} catch (Exception e) {
responseObj.setStatus("fail");
responseObj.setMessage("cannot find available users list : " + userName);
responseObj.setPayload(null);
}
logger.debug("ResponseObjectService is ====>{}",responseObj);
return new ResponseEntity<ResponseObjectService>(responseObj, HttpStatus.OK);
}


}
4 changes: 2 additions & 2 deletions src/main/java/com/ourapp/socialmedia/entity/FollowData.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@Setter
public class FollowData {

private Long follower;
private String userName;

private Long following;
private Long id;

}
16 changes: 11 additions & 5 deletions src/main/java/com/ourapp/socialmedia/entity/Followers.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.ourapp.socialmedia.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import lombok.Getter;
Expand All @@ -14,22 +17,25 @@
@Setter
@Entity
@Table(name="TBL_FOLLOWERS")
public class Followers {
public class Followers implements Serializable {

private static final long serialVersionUID = 4887904943282174032L;

@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.AUTO)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "follower_id_seq")
@SequenceGenerator(name = "follower_id_seq", sequenceName = "FOLLOWERID_SEQ")
private Long id;

@Column(name="follower_Id")
private Long followerId;

@Column(name = "following_Id")
private Long followingId;


public Followers(Long follower, Long following) {
this.followerId = follower;
this.followingId = following;
}



}
2 changes: 2 additions & 0 deletions src/main/java/com/ourapp/socialmedia/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(name="TBL_USER")
public class User implements Serializable {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/ourapp/socialmedia/entity/UserFollowInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ourapp.socialmedia.entity;

import lombok.Data;

@Data
public class UserFollowInfo {

private Long userId;

private String userName;

private boolean isFollowing;

private String emailId;

private String followersCount;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@

import com.ourapp.socialmedia.entity.Followers;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;



@Repository
public interface FollowersRespository extends JpaRepository<Followers, Long> {

@SuppressWarnings("unchecked")
Followers save(Followers entity);


@Query(value = "select distinct(following_id) from TBL_FOLLOWERS where follower_id = ?1", nativeQuery = true)
List<Long> findFollowingById(Long id);

@Query(value = "select distinct(follower_id) from TBL_FOLLOWERS where following_id = ?1", nativeQuery = true)
List<Long> findFollowersById(Long id);

@Query(value = "select distinct(follower_id) from TBL_FOLLOWERS where following_id != ?1", nativeQuery = true)
List<Long> findFollowingList(Long id);

@Transactional
@Modifying
@Query(value = "delete from TBL_FOLLOWERS where follower_id = ?1 and following_id = ?2", nativeQuery = true)
void unFollowUser(Long followingId, Long followerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public AuthenticationManager authenticationManagerBean() throws Exception {
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests().antMatchers("/ourApp/user/authenticate")
.permitAll().antMatchers("/ourApp/user/register")
.permitAll().antMatchers("/ourApp/user/login").permitAll().antMatchers("/ourApp/feedback/register").permitAll()
.permitAll().antMatchers("/ourApp/follow").permitAll()
.antMatchers("/ourApp/user/login").permitAll().antMatchers("/ourApp/feedback/register").permitAll()
.anyRequest().authenticated()
.and().exceptionHandling().and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
Expand Down

0 comments on commit 91d1e48

Please sign in to comment.