Skip to content

Commit

Permalink
refactor: 차단 버그 해결 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxoznge authored Sep 23, 2024
1 parent 0def2b9 commit 17d83ea
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ddabong.ddabongdotchiBE.domain.blacklist.dto.request.BlacklistCreateRequest;
import com.ddabong.ddabongdotchiBE.domain.blacklist.dto.response.BlacklistCreateResponse;
import com.ddabong.ddabongdotchiBE.domain.blacklist.dto.response.BlacklistGetResponse;
import com.ddabong.ddabongdotchiBE.domain.blacklist.service.BlacklistQueryService;
Expand All @@ -31,23 +29,26 @@ public class BlacklistController {
private final BlacklistService blacklistService;
private final BlacklistQueryService blacklistQueryService;

/* 차단하기 */
@PostMapping("")
public ApiResponse<BlacklistCreateResponse> createBlacklist(
@UserResolver User authUser,
@RequestBody BlacklistCreateRequest request
@RequestParam Long targetId
) {
return ApiResponse.onSuccess(blacklistService.createBlacklist(authUser, request));
return ApiResponse.onSuccess(blacklistService.createBlacklist(authUser, targetId));
}

/* 차단 목록 조회 */
@GetMapping("")
public ApiResponse<List<BlacklistGetResponse>> getBlacklist(@UserResolver User user) {
return ApiResponse.onSuccess(blacklistQueryService.getBlacklist(user));
}

@DeleteMapping("/{targetId}")
/* 차단 해제 */
@DeleteMapping("")
public ApiResponse<String> deleteBlacklist(
@UserResolver User user,
@PathVariable Long targetId
@RequestParam Long targetId
) {
blacklistService.deleteBlacklist(user, targetId);
return ApiResponse.onSuccess("삭제 성공");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class BlacklistQueryService {

private final BlacklistRepository blacklistRepository;

/* 차단 목록 조회 */
public List<BlacklistGetResponse> getBlacklist(User user) {
return blacklistRepository.findByUser(user)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddabong.ddabongdotchiBE.domain.blacklist.dto.request.BlacklistCreateRequest;
import com.ddabong.ddabongdotchiBE.domain.blacklist.dto.response.BlacklistCreateResponse;
import com.ddabong.ddabongdotchiBE.domain.blacklist.entity.Blacklist;
import com.ddabong.ddabongdotchiBE.domain.blacklist.exception.BlacklistErrorCode;
Expand All @@ -26,21 +25,35 @@ public class BlacklistService {
private final BlacklistRepository blacklistRepository;
private final UserRepository userRepository;

public BlacklistCreateResponse createBlacklist(User user, BlacklistCreateRequest request) {
/* 차단하기 */
public BlacklistCreateResponse createBlacklist(User user, Long targetId) {

if (user.getUsername().equals(request.target()))
// 사용자가 자신의 ID를 차단하려는 경우 예외 발생
if (user.getId().equals(targetId)) {
throw new BlacklistExceptionHandler(BlacklistErrorCode.BLACKLIST_ERROR);
}

final User target = userRepository.findByUsername(request.target())
// 대상 사용자를 userRepository 조회
User target = userRepository.findById(targetId)
.orElseThrow(() -> new UserExceptionHandler(UserErrorCode.USER_NOT_FOUND));

if (blacklistRepository.existsByUserAndTarget(user, target))
// 이미 차단한 사용자인지 확인
if (blacklistRepository.existsByUserAndTarget(user, target)) {
throw new BlacklistExceptionHandler(BlacklistErrorCode.BLACKLIST_ALREADY_REPORTED);
}

// 차단 정보 저장
Blacklist blacklist = blacklistRepository.save(
Blacklist.builder()
.user(user) // 차단 요청을 한 사용자
.target(target) // 차단 대상 사용자
.build()
);

Blacklist blacklist = blacklistRepository.save(request.toEntity(user, target));
return BlacklistCreateResponse.from(blacklist);
}

/* 차단 해제 */
public void deleteBlacklist(User user, Long targetId) {
final Blacklist blacklist = blacklistRepository.findByUserAndTargetId(user, targetId)
.orElseThrow(() -> new BlacklistExceptionHandler(BlacklistErrorCode.BLACKLIST_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface UserRepository {

Optional<User> findByUsername(String username);

Optional<User> findById(Long id);

Boolean existsByUsername(String username);

Boolean existsByNickname(String nickname);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public Optional<User> findByUsername(String username) {
return userJpaRepository.findByUsername(username);
}

@Override
public Optional<User> findById(Long id) {
return userJpaRepository.findById(id);
}

@Override
public Boolean existsByUsername(String username) {
return userJpaRepository.existsByUsername(username);
Expand Down

0 comments on commit 17d83ea

Please sign in to comment.