Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR] 팔로우 기능 개발 #39

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.omoknoone.onionhotsayyo.follow.command.aggregate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "follow")
public class Follow {

@Id
@Column(name = "follow_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int followId;

@Column(name = "from_member_id")
private String fromMemberId;

@Column(name = "to_member_id")
private String toMemberId;

public Follow() {
}

public Follow(int followId, String fromMemberId, String toMemberId) {
this.followId = followId;
this.fromMemberId = fromMemberId;
this.toMemberId = toMemberId;
}

public int getFollowId() {
return followId;
}

public void setFollowId(int followId) {
this.followId = followId;
}

public String getFromMemberId() {
return fromMemberId;
}

public void setFromMemberId(String fromMemberId) {
this.fromMemberId = fromMemberId;
}

public String getToMemberId() {
return toMemberId;
}

public void setToMemberId(String toMemberId) {
this.toMemberId = toMemberId;
}

@Override
public String toString() {
return "Follow{" +
"followId=" + followId +
", fromMemberId='" + fromMemberId + '\'' +
", toMemberId='" + toMemberId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.omoknoone.onionhotsayyo.follow.command.controller;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.omoknoone.onionhotsayyo.follow.command.aggregate.Follow;
import org.omoknoone.onionhotsayyo.follow.command.dto.FollowDTO;
import org.omoknoone.onionhotsayyo.follow.command.service.FollowService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/follows")
public class FollowController {

private FollowService followService;
private ModelMapper modelMapper;

public FollowController(FollowService followService, ModelMapper modelMapper) {
this.followService = followService;
this.modelMapper = modelMapper;
}

// 팔로우
@PostMapping("/follow")
public ResponseEntity<Follow> followMember (@RequestBody FollowDTO newFollow) {

followService.followMember(newFollow);

Follow responseFollow = modelMapper.map(newFollow, Follow.class);

return ResponseEntity.status(HttpStatus.CREATED).body(responseFollow);
}

// 언팔로우
@DeleteMapping("/unfollow/{fromMemberId}/{toMemberId}")
public void unfollowMember(@PathVariable(name = "fromMemberId") String fromMemberId,
@PathVariable(name = "toMemberId") String toMemberId) {
followService.unfollowMember(fromMemberId, toMemberId);
}

// 내가 팔로우 한 목록 조회
@GetMapping("list/myfollow/{memberId}")
public ResponseEntity<List<MemberDTO>> viewFollowListByMe (@PathVariable(name = "memberId") String memberId) {
List<FollowDTO> followingDTOList = followService.selectAllFollowing(memberId);

/* memberId로 user 정보 모두 조회 하는 메소드 생성 요망 */
List<MemberDTO> memberDTOList = memberService.selectAllByMemberId(followingDTOList);

return ResponseEntity.status(HttpStatus.OK).body(memberDTOList);
}

// 나를 팔로우 한 목록 조회
@GetMapping("/list/myfollowers/{memberId}")
public ResponseEntity<List<MemberDTO>> viewMyFollowerList (@PathVariable(name = "memberId") String memberId) {
List<FollowDTO> followerDTOList = followService.selectAllFollower(memberId);
List<MemberDTO> memberDTOList = memberService.selectAllByMemberId(followerDTOList);

return ResponseEntity.status(HttpStatus.OK).body(memberDTOList);
}



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.omoknoone.onionhotsayyo.follow.command.dto;

public class FollowDTO {
private int followId;
private String fromMemberId;
private String toMemberId;

public FollowDTO() {
}

public FollowDTO(int followId, String fromMemberId, String toMemberId) {
this.followId = followId;
this.fromMemberId = fromMemberId;
this.toMemberId = toMemberId;
}

public int getFollowId() {
return followId;
}

public void setFollowId(int followId) {
this.followId = followId;
}

public String getFromMemberId() {
return fromMemberId;
}

public void setFromMemberId(String fromMemberId) {
this.fromMemberId = fromMemberId;
}

public String getToMemberId() {
return toMemberId;
}

public void setToMemberId(String toMemberId) {
this.toMemberId = toMemberId;
}

@Override
public String toString() {
return "FollowDTO{" +
"followId=" + followId +
", fromMemberId='" + fromMemberId + '\'' +
", toMemberId='" + toMemberId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.omoknoone.onionhotsayyo.follow.command.repository;

import java.util.List;

import org.omoknoone.onionhotsayyo.follow.command.aggregate.Follow;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FollowRepository extends JpaRepository<Follow, Integer> {
List<Follow> findAllByFromMemberId(String memberId);

List<Follow> findAllByToMemberId(String memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.omoknoone.onionhotsayyo.follow.command.service;

import java.util.List;

import org.omoknoone.onionhotsayyo.follow.command.dto.FollowDTO;

public interface FollowService {

// public boolean existsByFromMemberIdAndToMemberId(FollowDTO followDTO);
public void followMember(FollowDTO followDTO);

public void unfollowMember(String fromMemberId, String toMemberId);

List<FollowDTO> selectAllFollowing(String memberId);

List<FollowDTO> selectAllFollower(String memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.omoknoone.onionhotsayyo.follow.command.service;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.omoknoone.onionhotsayyo.follow.command.aggregate.Follow;
import org.omoknoone.onionhotsayyo.follow.command.dto.FollowDTO;
import org.omoknoone.onionhotsayyo.follow.command.repository.FollowRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FollowServiceImpl implements FollowService {

private final FollowRepository followRepository;
private final ModelMapper modelMapper;

public FollowServiceImpl(FollowRepository followRepository, ModelMapper modelMapper) {
this.followRepository = followRepository;
this.modelMapper = modelMapper;
}

@Transactional
@Override
// 사용자가 다른 사용자를 팔로우하는 메소드 followMember
public void followMember(FollowDTO followDTO) {
// if(!existsByFromMemberIdAndToMemberId(followDTO)) {
Follow follow = modelMapper.map(followDTO, Follow.class);
followRepository.save(follow);
// }
}

@Transactional
@Override
// 언팔로우
public void unfollowMember(String fromMemberId, String toMemberId) {
List<Follow> followList = followRepository.findAll();

for (Follow follow : followList) {
if (follow.getFromMemberId().equals(fromMemberId) &&
follow.getToMemberId().equals(toMemberId)) {
followRepository.deleteById(follow.getFollowId());
}
}
}

@Override
public List<FollowDTO> selectAllFollowing(String memberId) {
List<Follow> followList = followRepository.findAllByFromMemberId(memberId);
return followList.stream().map(follow -> modelMapper.map(followList, FollowDTO.class)).toList();
ms1011 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public List<FollowDTO> selectAllFollower(String memberId) {
List<Follow> followList = followRepository.findAllByToMemberId(memberId);
return followList.stream().map(follow -> modelMapper.map(followList, FollowDTO.class)).toList();
}

// @Override
// public boolean existsByFromMemberIdAndToMemberId(FollowDTO followDTO) {
//
// return false;
// }

}