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

9조 BE 코드리뷰 4회차 #59

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -4,12 +4,18 @@
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.helpmeCookies.global.exception.user.ResourceNotFoundException;
import com.sun.jdi.request.DuplicateRequestException;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public String handleResourceNotFoundException() {
return "Resource not found";
return "해당 리소스를 찾을 수 없습니다.";
}

@ExceptionHandler(DuplicateRequestException.class)
public String handleDuplicateRequestException() {
return "이미 생성되었거나 중복된 요청입니다.";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.helpmeCookies.global.exception.user;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException() {
super("Resource not found");
public ResourceNotFoundException(String message) {
super(message);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/helpmeCookies/global/jwt/JwtToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@Getter
@Builder
public class JwtToken {
private String accessToken;
private String refreshToken;
private final String accessToken;
private final String refreshToken;
}
12 changes: 6 additions & 6 deletions src/main/java/com/helpmeCookies/global/jwt/JwtUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
@Builder
@Getter
public class JwtUser implements UserDetails {
private Long id;
private String username;
private final Long id;
private final String username;
private Collection<? extends GrantedAuthority> authorities;

public static JwtUser of(Long id) {
Expand All @@ -41,22 +41,22 @@ public String getUsername() {

@Override
public boolean isAccountNonExpired() {
return true;
return false;
}

@Override
public boolean isAccountNonLocked() {
return true;
return false;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
return false;
}

@Override
public boolean isEnabled() {
return true;
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
import com.helpmeCookies.user.dto.response.ArtistDetailsRes;
import com.helpmeCookies.user.service.ArtistService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@Tag(name = "작가 관련 기능", description = "작가 관련 API")
public class ArtistController {
private final ArtistService artistService;
private final JwtProvider jwtProvider;


// 작가 등록(학생)
@Operation(summary = "학생 작가 등록", description = "학생 작가 등록")
@PostMapping("/v1/artists/students")
public ResponseEntity<String> registerStudents(
@RequestBody StudentArtistReq artistDetailsReq,
Expand All @@ -34,7 +37,7 @@ public ResponseEntity<String> registerStudents(
return ResponseEntity.ok().build();
}

// 작가 등록(사업자)
@Operation(summary = "사업자 작가 등록", description = "사업자 작가 등록")
@PostMapping("/v1/artists/bussinesses")
public ResponseEntity<String> registerbussinsess(
@RequestBody BusinessArtistReq businessArtistReq,
Expand All @@ -44,14 +47,7 @@ public ResponseEntity<String> registerbussinsess(
return ResponseEntity.ok().build();
}

// 작가 목록 조회(페이징)
// TODO: 6주차 회의 이후 추가
@GetMapping("/v1/artists")
public String getArtists() {
return "ok";
}

// 작가 프로필 조회
@Operation(summary = "작가 프로필 조회", description = "작가 프로필 조회")
@GetMapping("/v1/artists/{userId}")
public ArtistDetailsRes getArtist(
@AuthenticationPrincipal JwtUser jwtUser,
Expand All @@ -60,7 +56,7 @@ public ArtistDetailsRes getArtist(
return artistService.getArtistDetails(userId);
}

// 자기 자신 작가 프로필 조회
@Operation(summary = "작가 프로필 조회", description = "자신의 작가 프로필 조회")
@GetMapping("/v1/artist")
public ArtistDetailsRes getArtist(
@AuthenticationPrincipal JwtUser jwtUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
import com.helpmeCookies.user.dto.response.UserFollowingRes;
import com.helpmeCookies.user.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@Tag(name = "유저 및 팔로우 기능", description = "유저 및 팔로우 기능과 관련된 API")
public class UserController {
private final UserService userService;


// 유저 일반 정보 조회
@Operation(summary = "유저 일반 정보 조회", description = "로그인한 유저의 username, imageUrl, hashtag를 조회한다.")
@GetMapping("/v1/users")
public UserCommonInfoRes getUsers(
@AuthenticationPrincipal JwtUser jwtUser
Expand All @@ -40,6 +43,7 @@ public UserCommonInfoRes getUsers(
}

// 유저 상세 정보 조회
@Operation(summary = "유저 상세 정보 조회", description = "로그인한 유저의 이름, 주소를 비롯한 개인정보를 함께 조회한다.")
@GetMapping("/v1/users/details")
public UserDetailsInfoRes getUserDetails(
@AuthenticationPrincipal JwtUser jwtUser
Expand All @@ -48,6 +52,7 @@ public UserDetailsInfoRes getUserDetails(
}

// 유저 타입 조회
@Operation(summary = "유저 타입 조회", description = "로그인한 유저의 타입과 권한을 조회한다.")
@GetMapping("/v1/users/type")
public UserTypeDto getUserType(
@AuthenticationPrincipal JwtUser jwtUser
Expand All @@ -56,6 +61,7 @@ public UserTypeDto getUserType(
}

// 유저 정보 수정
@Operation(summary = "유저 정보 수정", description = "로그인한 유저의 개인정보를 수정한다.")
@PutMapping("/v1/users")
public String updateUserInfo(
@AuthenticationPrincipal JwtUser jwtUser,
Expand All @@ -66,7 +72,7 @@ public String updateUserInfo(
return "ok";
}

// 아티스트 팔로우하기
@Operation(summary = "아티스트 팔로우하기", description = "로그인한 유저가 특정 아티스트를 팔로우한다.")
@PostMapping("/v1/users/following/{artistId}")
public String followArtist(
@AuthenticationPrincipal JwtUser jwtUser,
Expand All @@ -76,7 +82,7 @@ public String followArtist(
return "ok";
}

// 아티스트 팔로우 취소하기
@Operation(summary = "아티스트 팔로우 취소하기", description = "로그인한 유저가 특정 아티스트를 팔로우 취소한다.")
@DeleteMapping("/v1/users/following/{artistId}")
public String unfollowArtist(
@AuthenticationPrincipal JwtUser jwtUser,
Expand All @@ -86,7 +92,7 @@ public String unfollowArtist(
return "ok";
}

// 유저 팔로우 목록 조회
@Operation(summary = "팔로잉 목록 조회", description = "로그인한 유저의 팔로우한 아티스트 목록을 조회한다.")
@GetMapping("/v1/users/following")
public Page<UserFollowingRes> getFollowingList(
@AuthenticationPrincipal JwtUser jwtUser,
Expand All @@ -96,6 +102,7 @@ public Page<UserFollowingRes> getFollowingList(
}

// 유저 탈퇴
@Operation(summary = "유저 탈퇴", description = "로그인한 유저의 정보를 삭제한다.")
@DeleteMapping("/v1/users")
public String deleteUser(
@AuthenticationPrincipal JwtUser jwtUser
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/helpmeCookies/user/service/ArtistService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.helpmeCookies.global.exception.user.ResourceNotFoundException;
import com.helpmeCookies.user.dto.ArtistInfoDto;
import com.helpmeCookies.user.dto.BusinessArtistDto;
import com.helpmeCookies.user.dto.StudentArtistDto;
Expand All @@ -18,6 +19,7 @@
import com.helpmeCookies.user.repository.BusinessArtistRepository;
import com.helpmeCookies.user.repository.StudentArtistRepository;
import com.helpmeCookies.user.repository.UserRepository;
import com.sun.jdi.request.DuplicateRequestException;

import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -66,7 +68,7 @@ public void registerBusinessArtist(BusinessArtistReq businessArtistReq, Long use
User user = userRepository.getReferenceById(userId);

if (artistInfoRepository.existsByUserId(userId)) {
throw new IllegalArgumentException("이미 등록된 아티스트입니다.");
throw new DuplicateRequestException("이미 등록된 아티스트입니다.");
}

// BusinessArtist 생성
Expand All @@ -93,22 +95,22 @@ public void registerBusinessArtist(BusinessArtistReq businessArtistReq, Long use
@Transactional
public ArtistDetailsRes getArtistDetails(Long userId) {
ArtistInfo artistInfo = artistInfoRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 아티스트입니다."));
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다."));
ArtistInfoDto artistInfoDto = ArtistInfoDto.fromEntity(artistInfo);

switch (artistInfo.getArtistType()) {
case STUDENT:
StudentArtist studentArtist = studentArtistRepository.findByArtistInfo(artistInfo)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 학생 아티스트입니다."));
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 학생 아티스트입니다."));
StudentArtistDto studentArtistDto = StudentArtistDto.from(studentArtist);
return ArtistDetailsRes.from(artistInfoDto, studentArtistDto);
case BUSINESS:
BusinessArtist businessArtist = businessArtistRepository.findByArtistInfo(artistInfo)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사업자 아티스트입니다."));
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 사업자 아티스트입니다."));
BusinessArtistDto businessArtistDto = BusinessArtistDto.from(businessArtist);
return ArtistDetailsRes.from(artistInfoDto, businessArtistDto);
default:
throw new IllegalArgumentException("존재하지 않는 아티스트입니다.");
throw new ResourceNotFoundException("존재하지 않는 아티스트입니다.");
}
}
}
18 changes: 9 additions & 9 deletions src/main/java/com/helpmeCookies/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.helpmeCookies.global.exception.user.ResourceNotFoundException;
import com.helpmeCookies.user.dto.UserDto;
import com.helpmeCookies.user.dto.UserFollowingDto;
import com.helpmeCookies.user.dto.UserInfoDto;
import com.helpmeCookies.user.dto.UserTypeDto;
import com.helpmeCookies.user.dto.response.UserFollowingRes;
Expand All @@ -18,6 +17,7 @@
import com.helpmeCookies.user.repository.ArtistInfoRepository;
import com.helpmeCookies.user.repository.SocialRepository;
import com.helpmeCookies.user.repository.UserRepository;
import com.sun.jdi.request.DuplicateRequestException;

import lombok.RequiredArgsConstructor;

Expand All @@ -32,7 +32,7 @@ public class UserService {
@Transactional
public UserInfoDto getUserInfo(Long userId) {
UserInfo userInfo = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException())
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다."))
.getUserInfo();

return UserInfoDto.fromEntity(userInfo);
Expand All @@ -42,7 +42,7 @@ public UserInfoDto getUserInfo(Long userId) {
public UserDto updateUserInfo(UserInfoDto userInfoDto, Long userId) {

User existingUser = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다."));

existingUser.updateUserInfo(userInfoDto.toEntity());

Expand Down Expand Up @@ -71,13 +71,13 @@ public Page<UserFollowingRes> getFollowingWithPaging(Long userId, Pageable pagea
@Transactional
public void followArtist(Long userId, Long artistId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다."));

ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다."));

if (socialRepository.existsByFollowerAndFollowing(user, artistInfo)) {
throw new IllegalArgumentException("이미 팔로우한 아티스트입니다.");
throw new DuplicateRequestException("이미 팔로우한 아티스트입니다.");
}

Social social = Social.builder()
Expand All @@ -91,13 +91,13 @@ public void followArtist(Long userId, Long artistId) {
@Transactional
public void unfollowArtist(Long userId, Long artistId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다."));

ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다."));

Social social = socialRepository.findByFollowerAndFollowing(user, artistInfo)
.orElseThrow(() -> new ResourceNotFoundException());
.orElseThrow(() -> new ResourceNotFoundException("팔로우하지 않은 아티스트입니다."));

socialRepository.delete(social);
}
Expand Down
Loading