Skip to content

Commit

Permalink
#40 팔로잉, 팔로잉 & 팔로워 list api
Browse files Browse the repository at this point in the history
  • Loading branch information
domathang committed May 11, 2022
1 parent 69c9065 commit ea77402
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.foreveryone.knowing.controller;

import com.foreveryone.knowing.dto.request.mypage.FollowingRequest;
import com.foreveryone.knowing.dto.request.mypage.InterestCategoriesRequest;
import com.foreveryone.knowing.dto.request.mypage.NicknameRequest;
import com.foreveryone.knowing.dto.response.mypage.FollowResponse;
import com.foreveryone.knowing.dto.response.mypage.InterestResponse;
import com.foreveryone.knowing.service.MyPageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -42,6 +45,28 @@ public void saveInterests(@Valid @RequestBody InterestCategoriesRequest interest
@PutMapping("/profile")
@ResponseStatus(HttpStatus.CREATED)
public void updatePicture(@RequestPart(value = "file")MultipartFile file) {
System.out.println("프로필 수정");
myPageService.updatePicture(file);
}

@PostMapping("/following")
@ResponseStatus(HttpStatus.CREATED) @PreAuthorize("isAuthenticated()")
public void following(@Valid @RequestBody FollowingRequest request) {
System.out.println("팔로잉");
myPageService.following(request.getUserId());
}

@GetMapping("/follower")
@ResponseStatus(HttpStatus.OK) @PreAuthorize("isAuthenticated()")
public List<FollowResponse> queryFollowers() {
System.out.println("팔로워 리스트");
return myPageService.queryFollower();
}

@GetMapping("/following")
@ResponseStatus(HttpStatus.OK) @PreAuthorize("isAuthenticated()")
public List<FollowResponse> queryFollowing() {
System.out.println("팔로잉 리스트");
return myPageService.queryFollowing();
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/foreveryone/knowing/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import com.foreveryone.knowing.dto.request.mypage.InterestCategoriesRequest;
import com.foreveryone.knowing.dto.request.mypage.NicknameRequest;
import com.foreveryone.knowing.dto.response.mypage.FollowResponse;
import com.foreveryone.knowing.dto.response.mypage.InterestResponse;
import com.foreveryone.knowing.entity.mypage.Follow;
import com.foreveryone.knowing.entity.mypage.FollowId;
import com.foreveryone.knowing.entity.mypage.Interest;
import com.foreveryone.knowing.entity.mypage.InterestId;
import com.foreveryone.knowing.entity.auth.User;
import com.foreveryone.knowing.error.exceptions.NotFoundException;
import com.foreveryone.knowing.file.FileUploadProvider;
import com.foreveryone.knowing.repository.mypage.FollowRepository;
import com.foreveryone.knowing.repository.mypage.InterestCategoryRepository;
import com.foreveryone.knowing.repository.mypage.InterestRepository;
import com.foreveryone.knowing.repository.auth.UserRepository;
Expand All @@ -29,6 +33,7 @@ public class MyPageService {
private final InterestCategoryRepository interestCategoryRepository;
private final AuthFacade authFacade;
private final FileUploadProvider fileUploadProvider;
private final FollowRepository followRepository;

@Transactional
public void updateNickname(NicknameRequest nicknameRequest) {
Expand Down Expand Up @@ -89,4 +94,33 @@ public void updatePicture(MultipartFile file) {
currentUser.updateProfile(fileName);
userRepository.save(currentUser);
}

public void following(Integer userId) {
User currentUser = authFacade.getCurrentUser();
User following = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("id: {" + userId + "}User Not Found"));

followRepository.save(Follow.builder()
.followId(FollowId.builder()
.follower(currentUser.getId())
.following(following.getId())
.build())
.following(following)
.follower(currentUser)
.build());
}

public List<FollowResponse> queryFollowing() {
User currentUser = authFacade.getCurrentUser();
return followRepository.findAllByFollower(currentUser).stream()
.map(follow -> FollowResponse.of(follow.getFollowing()))
.collect(Collectors.toList());
}

public List<FollowResponse> queryFollower() {
User currentUser = authFacade.getCurrentUser();
return followRepository.findAllByFollowing(currentUser).stream()
.map(follow -> FollowResponse.of(follow.getFollower()))
.collect(Collectors.toList());
}
}

0 comments on commit ea77402

Please sign in to comment.