diff --git a/src/main/java/com/io/realworld/domain/aggregate/profile/controller/ProfilesController.java b/src/main/java/com/io/realworld/domain/aggregate/profile/controller/ProfilesController.java index 135fe12..7d99d57 100644 --- a/src/main/java/com/io/realworld/domain/aggregate/profile/controller/ProfilesController.java +++ b/src/main/java/com/io/realworld/domain/aggregate/profile/controller/ProfilesController.java @@ -5,10 +5,7 @@ import com.io.realworld.domain.aggregate.profile.service.ProfileService; import com.io.realworld.domain.aggregate.user.dto.UserAuth; import lombok.extern.log4j.Log4j2; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Log4j2 @RestController @@ -17,14 +14,20 @@ public class ProfilesController { private final ProfileService profileService; - public ProfilesController(ProfileService profileService){ + public ProfilesController(ProfileService profileService) { this.profileService = profileService; } @GetMapping("/{username}") - public ProfileResponse getProfile(@AuthenticationPrincipal UserAuth userAuth, @PathVariable String username){ - log.info("current user :{}, find user : {}",userAuth.getUsername(),username); - return profileService.getProfile(userAuth,username); + public ProfileResponse getProfile(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("username") String username) { + log.info("current user :{}, find user : {}", userAuth.getUsername(), username); + return profileService.getProfile(userAuth, username); + } + + @PostMapping("/{username}/follow") + public ProfileResponse followUser(@AuthenticationPrincipal UserAuth userAuth, @PathVariable("username") String username) { + log.info("current user : {} , Follow User : {}",userAuth.getUsername(), username); + return profileService.followUser(userAuth, username); } diff --git a/src/main/java/com/io/realworld/domain/aggregate/profile/entity/Follow.java b/src/main/java/com/io/realworld/domain/aggregate/profile/entity/Follow.java index 06771df..1184d7c 100644 --- a/src/main/java/com/io/realworld/domain/aggregate/profile/entity/Follow.java +++ b/src/main/java/com/io/realworld/domain/aggregate/profile/entity/Follow.java @@ -1,9 +1,14 @@ package com.io.realworld.domain.aggregate.profile.entity; import com.io.realworld.domain.aggregate.user.entity.User; +import lombok.*; import javax.persistence.*; +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor @Table(name="follows", uniqueConstraints = { @UniqueConstraint(name="follow_followee_pair_unique",columnNames = {"followee","follower"}) }) diff --git a/src/main/java/com/io/realworld/domain/aggregate/profile/repository/ProfileRepository.java b/src/main/java/com/io/realworld/domain/aggregate/profile/repository/ProfileRepository.java index f0e9bdd..abbcb85 100644 --- a/src/main/java/com/io/realworld/domain/aggregate/profile/repository/ProfileRepository.java +++ b/src/main/java/com/io/realworld/domain/aggregate/profile/repository/ProfileRepository.java @@ -9,5 +9,4 @@ public interface ProfileRepository extends JpaRepository { Optional findByFolloweeIdAndFollowerId(Long followeeId, Long followerId); - } diff --git a/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileService.java b/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileService.java index 12b125c..1e0d809 100644 --- a/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileService.java +++ b/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileService.java @@ -6,4 +6,6 @@ import com.io.realworld.domain.aggregate.user.dto.UserAuth; public interface ProfileService { ProfileResponse getProfile(UserAuth userAuth, String username); + ProfileResponse followUser(UserAuth userAuth, String username); + } diff --git a/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileServiceImpl.java b/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileServiceImpl.java index 8cc3373..26b4f12 100644 --- a/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileServiceImpl.java +++ b/src/main/java/com/io/realworld/domain/aggregate/profile/service/ProfileServiceImpl.java @@ -9,13 +9,17 @@ import com.io.realworld.domain.aggregate.user.repository.UserRepository; import com.io.realworld.exception.CustomException; import com.io.realworld.exception.Error; import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.mockito.internal.matchers.Null; import org.springframework.stereotype.Service; +import java.util.Objects; import java.util.Optional; +@Log4j2 @Service @RequiredArgsConstructor -public class ProfileServiceImpl implements ProfileService{ +public class ProfileServiceImpl implements ProfileService { private final UserRepository userRepository; private final ProfileRepository profileRepository; @@ -25,16 +29,24 @@ public class ProfileServiceImpl implements ProfileService{ Optional wantFindUser = Optional.ofNullable(userRepository.findByUsername(username).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND))); Boolean followStatus = profileRepository.findByFolloweeIdAndFollowerId(userAuth.getId(), wantFindUser.get().getId()).isPresent(); + return convertProfile(followStatus, wantFindUser); + } + + @Override + public ProfileResponse followUser(UserAuth userAuth, String username) { + Optional wantFindUser = Optional.ofNullable(userRepository.findByUsername(username).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND))); + Optional currentUser = userRepository.findById(userAuth.getId()); + Follow followPair = Follow.builder().followee(wantFindUser.get()).follower(currentUser.get()).build(); + Boolean followStatus = profileRepository.save(followPair).getFollowee().equals(wantFindUser.get()); return convertProfile(followStatus,wantFindUser); } - private ProfileResponse convertProfile(Boolean followStatus, Optional user){ + private ProfileResponse convertProfile(Boolean followStatus, Optional user) { return ProfileResponse.builder(). username(user.get().getUsername()). bio(user.get().getBio()). image(user.get().getImage()). following(followStatus).build(); - } }