feat: Follow User Service Implement

This commit is contained in:
minseokkang
2022-10-14 10:57:05 +09:00
parent d11dab2edf
commit 1ed78053ad
5 changed files with 33 additions and 12 deletions

View File

@@ -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);
}

View File

@@ -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"})
})

View File

@@ -9,5 +9,4 @@ public interface ProfileRepository extends JpaRepository<Follow,Long> {
Optional<Follow> findByFolloweeIdAndFollowerId(Long followeeId, Long followerId);
}

View File

@@ -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);
}

View File

@@ -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<User> 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<User> wantFindUser = Optional.ofNullable(userRepository.findByUsername(username).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND)));
Optional<User> 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> user){
private ProfileResponse convertProfile(Boolean followStatus, Optional<User> user) {
return ProfileResponse.builder().
username(user.get().getUsername()).
bio(user.get().getBio()).
image(user.get().getImage()).
following(followStatus).build();
}
}