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

[PC-383] 어드민 차단 데이터 조회 기능 #28

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,53 @@
package org.yapp.block.application;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.yapp.block.dao.UserBlockRepository;
import org.yapp.block.presentation.response.UserBlockResponse;
import org.yapp.domain.block.UserBlock;
import org.yapp.domain.user.User;
import org.yapp.util.PageResponse;

@Service
@RequiredArgsConstructor
public class UserBlockService {

private final UserBlockRepository userBlockRepository;

public PageResponse<UserBlockResponse> getUserBlockPageResponse(int page, int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));

Page<UserBlock> userBlockPage = userBlockRepository.findAll(pageable);

List<UserBlockResponse> content = userBlockPage.getContent().stream()
.map(userBlock -> {
User blockingUser = userBlock.getBlockingUser();
User blockedUser = userBlock.getBlockedUser();

return new UserBlockResponse(
blockedUser.getId(),
blockedUser.getProfile().getProfileBasic().getNickname(),
blockedUser.getName(),
blockedUser.getProfile().getProfileBasic().getBirthdate(),
blockingUser.getId(),
blockingUser.getProfile().getProfileBasic().getNickname(),
blockingUser.getName(),
userBlock.getCreatedAt().toLocalDate());
}).toList();

return new PageResponse<>(
content,
userBlockPage.getNumber(),
userBlockPage.getSize(),
userBlockPage.getTotalPages(),
userBlockPage.getTotalElements(),
userBlockPage.isFirst(),
userBlockPage.isLast()
);
}
}
10 changes: 10 additions & 0 deletions admin/src/main/java/org/yapp/block/dao/UserBlockRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.yapp.block.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.yapp.domain.block.UserBlock;

@Repository
public interface UserBlockRepository extends JpaRepository<UserBlock, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.yapp.block.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yapp.block.application.UserBlockService;
import org.yapp.block.presentation.response.UserBlockResponse;
import org.yapp.util.CommonResponse;
import org.yapp.util.PageResponse;

@RestController()
@RequiredArgsConstructor
@RequestMapping("/admin/v1/blocks")
public class UserBlockController {

private final UserBlockService userBlockService;

@GetMapping("")
public ResponseEntity<CommonResponse<PageResponse<UserBlockResponse>>> getUsers(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {

PageResponse<UserBlockResponse> userBlockPageResponse = userBlockService.getUserBlockPageResponse(
page, size);

return ResponseEntity.ok(CommonResponse.createSuccess(userBlockPageResponse));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.yapp.block.presentation.response;

import java.time.LocalDate;

public record UserBlockResponse(
Long blockedUserId,
String BlockedUserNickname, String BlockedUserName,
LocalDate blockedUserBirthdate, Long blockingUserId,
String blockingUserNickname,
String blockingUserName, LocalDate BlockedDate) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.yapp.domain.block.application;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yapp.domain.block.BlockContact;
import org.yapp.domain.block.application.dto.BlockContactCreateDto;
import org.yapp.domain.block.dao.BlockContactRepository;
import org.yapp.domain.user.User;

@Service
@RequiredArgsConstructor
public class BlockContactService {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이름 명확해서 좋은 것 같습니다👍


private final BlockContactRepository blockContactRepository;

@Transactional()
public void blockPhoneNumbers(BlockContactCreateDto blockContactCreateDto) {
Long userId = blockContactCreateDto.userId();
List<String> phoneNumbers = blockContactCreateDto.phoneNumbers();
List<BlockContact> newBlockContacts = new ArrayList<>();

Set<String> blockedPhoneNumbers = blockContactRepository.findBlocksByUserId(userId)
.stream()
.map(BlockContact::getPhoneNumber)
.collect(Collectors.toSet());

phoneNumbers.stream()
.filter(phoneNumber -> !blockedPhoneNumbers.contains(phoneNumber))
.forEach(phoneNumber -> {
BlockContact blockContact = BlockContact.builder()
.user(User.builder().id(userId).build())
.phoneNumber(phoneNumber)
.build();
newBlockContacts.add(blockContact);
});

blockContactRepository.saveAll(newBlockContacts);
}

@Transactional(readOnly = false)
public List<BlockContact> findBlocksByUserId(Long userId) {
return blockContactRepository.findBlocksByUserId(userId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.yapp.domain.block.application.dto;

import java.util.List;

public record BlockContactCreateDto(Long userId, List<String> phoneNumbers) {

public BlockContactCreateDto(Long userId, List<String> phoneNumbers) {
this.userId = userId;
this.phoneNumbers = phoneNumbers.stream()
.map(phone -> phone.replaceAll("-", ""))
.toList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.yapp.domain.block.dao;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.yapp.domain.block.Block;

import java.util.List;
import org.yapp.domain.block.BlockContact;

@Repository
public interface BlockRepository extends JpaRepository<Block, Long> {
List<Block> findBlocksByUserId(Long userId);
public interface BlockContactRepository extends JpaRepository<BlockContact, Long> {

List<BlockContact> findBlocksByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.yapp.domain.block.presentation;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;
import org.yapp.domain.block.BlockContact;
import org.yapp.domain.block.application.BlockContactService;
import org.yapp.domain.block.application.dto.BlockContactCreateDto;
import org.yapp.domain.block.presentation.dto.request.BlockPhoneNumbersRequest;
import org.yapp.domain.block.presentation.dto.response.UserBlockContactResponses;
import org.yapp.util.CommonResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/blockContacts")
public class BlockContactController {

private final BlockContactService blockContactService;

@PostMapping("")
@Operation(summary = "핸드폰 번호 차단", description = "핸드폰 번호 리스트를 전달받고, 전달받은 핸드폰 번호 차단을 수행합니다.", tags = {
"차단"})
@ApiResponse(responseCode = "200", description = "핸드폰 차단 성공")
public ResponseEntity<CommonResponse<Void>> blockPhoneNumbers(
@AuthenticationPrincipal Long userId, @RequestBody BlockPhoneNumbersRequest request) {
blockContactService.blockPhoneNumbers(
new BlockContactCreateDto(userId, request.phoneNumbers()));
return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.createSuccessWithNoContent("핸드폰 번호 차단 성공"));
}

@GetMapping("")
@Operation(summary = "핸드폰 번호 차단", description = "핸드폰 번호 리스트를 전달받고, 전달받은 핸드폰 번호 차단을 수행합니다.", tags = {
"차단"})
Comment on lines +42 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 swagger 명세가 좀 이상한 것 같습니다.
차단 조회 아닌가요?

@ApiResponse(responseCode = "200", description = "핸드폰 차단 성공")
public ResponseEntity<CommonResponse<UserBlockContactResponses>> blockPhoneNumbers(
@AuthenticationPrincipal Long userId) {
List<BlockContact> blockContacts = blockContactService.findBlocksByUserId(userId);
return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.createSuccess(UserBlockContactResponses.from(userId,
blockContacts)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

import java.time.LocalDateTime;

public record BlockResponse(String phoneNumber, LocalDateTime blockedAt) {
public record BlockContactResponse(String phoneNumber, LocalDateTime blockedAt) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.yapp.domain.block.presentation.dto.response;

import java.util.List;
import org.yapp.domain.block.BlockContact;

public record UserBlockContactResponses(Long userId,
List<BlockContactResponse> blockContactResponses) {

public static UserBlockContactResponses from(Long userId, List<BlockContact> blockContacts) {
List<BlockContactResponse> blockContactResponses = blockContacts.stream().map(
blockContact -> new BlockContactResponse(blockContact.getPhoneNumber(),
blockContact.getCreatedAt())).toList();

return new UserBlockContactResponses(
userId,
blockContactResponses
);
}

}

This file was deleted.

Loading