Skip to content

Commit b17a36b

Browse files
committed
feat: 어드민 차단 기능
1 parent e391cc5 commit b17a36b

4 files changed

Lines changed: 40 additions & 29 deletions

File tree

src/main/java/com/example/solidconnection/admin/controller/AdminUserBanController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.example.solidconnection.admin.controller;
22

33
import org.springframework.http.ResponseEntity;
4-
import org.springframework.web.bind.annotation.*;
4+
import org.springframework.web.bind.annotation.PatchMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
510

611
import com.example.solidconnection.admin.dto.UserBanRequest;
712
import com.example.solidconnection.admin.service.AdminUserBanService;

src/main/java/com/example/solidconnection/admin/service/AdminUserBanService.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AdminUserBanService {
3636

3737
@Transactional
3838
public void banUser(long userId, UserBanRequest request) {
39-
ZonedDateTime now = ZonedDateTime.now(UTC); //TODO UTC 확인
39+
ZonedDateTime now = ZonedDateTime.now(UTC);
4040
validateNotAlreadyBanned(userId, now);
4141
validateReportExists(userId);
4242

@@ -45,27 +45,6 @@ public void banUser(long userId, UserBanRequest request) {
4545
updateUserStatus(userId, UserStatus.BANNED);
4646
}
4747

48-
@Transactional
49-
public void unbanUser(long userId, long adminId) {
50-
UserBan userBan = findBannedUser(userId, ZonedDateTime.now(UTC));
51-
userBan.manuallyUnban(adminId);
52-
processUnban(userId);
53-
}
54-
55-
@Transactional
56-
@Scheduled(cron = "0 0 0 * * *")
57-
public void expireUserBans() {
58-
List<UserBan> expiredBans = userBanRepository.findAllByIsUnbannedFalseAndExpiredAtBefore(ZonedDateTime.now(UTC));
59-
for (UserBan userBan : expiredBans) {
60-
processUnban(userBan.getBannedUserId());
61-
}
62-
}
63-
64-
public void processUnban(long userId) {
65-
updateReportedContentIsDeleted(userId, false);
66-
updateUserStatus(userId, UserStatus.REPORTED);
67-
}
68-
6948
private void validateNotAlreadyBanned(long userId, ZonedDateTime now) {
7049
if (userBanRepository.existsByBannedUserIdAndIsUnbannedFalseAndExpiredAtAfter(userId, now)) {
7150
throw new CustomException(ErrorCode.ALREADY_BANNED_USER);
@@ -89,15 +68,36 @@ private void createUserBan(long userId, UserBanRequest request, ZonedDateTime no
8968
userBanRepository.save(userBan);
9069
}
9170

71+
private void updateUserStatus(long userId, UserStatus status) {
72+
SiteUser user = siteUserRepository.findById(userId)
73+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
74+
user.updateUserStatus(status);
75+
}
76+
77+
@Transactional
78+
public void unbanUser(long userId, long adminId) {
79+
UserBan userBan = findBannedUser(userId, ZonedDateTime.now(UTC));
80+
userBan.manuallyUnban(adminId);
81+
processUnban(userId);
82+
}
83+
9284
private UserBan findBannedUser(long userId, ZonedDateTime now) {
9385
return userBanRepository
9486
.findTopByBannedUserIdAndIsUnbannedFalseAndExpiredAtAfterOrderByCreatedAtDesc(userId, now)
9587
.orElseThrow(() -> new CustomException(ErrorCode.NOT_BANNED_USER));
9688
}
9789

98-
private void updateUserStatus(long userId, UserStatus status) {
99-
SiteUser user = siteUserRepository.findById(userId)
100-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
101-
user.updateUserStatus(status);
90+
@Transactional
91+
@Scheduled(cron = "0 0 0 * * *")
92+
public void expireUserBans() {
93+
List<UserBan> expiredBans = userBanRepository.findAllByIsUnbannedFalseAndExpiredAtBefore(ZonedDateTime.now(UTC));
94+
for (UserBan userBan : expiredBans) {
95+
processUnban(userBan.getBannedUserId());
96+
}
97+
}
98+
99+
private void processUnban(long userId) {
100+
updateReportedContentIsDeleted(userId, false);
101+
updateUserStatus(userId, UserStatus.REPORTED);
102102
}
103103
}

src/main/java/com/example/solidconnection/siteuser/domain/SiteUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class SiteUser extends BaseEntity {
7272

7373
@Column(nullable = false)
7474
@Enumerated(EnumType.STRING)
75-
private UserStatus userStatus;
75+
private UserStatus userStatus = UserStatus.ACTIVE;
7676

7777
public SiteUser(
7878
String email,

src/test/java/com/example/solidconnection/report/service/ReportServiceTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import static org.assertj.core.api.Assertions.assertThatCode;
55

66
import com.example.solidconnection.chat.domain.ChatMessage;
7+
import com.example.solidconnection.chat.domain.ChatParticipant;
78
import com.example.solidconnection.chat.domain.ChatRoom;
89
import com.example.solidconnection.chat.fixture.ChatMessageFixture;
10+
import com.example.solidconnection.chat.fixture.ChatParticipantFixture;
911
import com.example.solidconnection.chat.fixture.ChatRoomFixture;
1012
import com.example.solidconnection.common.exception.CustomException;
1113
import com.example.solidconnection.common.exception.ErrorCode;
@@ -52,6 +54,9 @@ class ReportServiceTest {
5254
@Autowired
5355
private ChatRoomFixture chatRoomFixture;
5456

57+
@Autowired
58+
private ChatParticipantFixture chatParticipantFixture;
59+
5560
@Autowired
5661
private ChatMessageFixture chatMessageFixture;
5762

@@ -65,7 +70,8 @@ void setUp() {
6570
Board board = boardFixture.자유게시판();
6671
post = postFixture.게시글(board, siteUser);
6772
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
68-
chatMessage = chatMessageFixture.메시지("채팅", siteUser.getId(), chatRoom);
73+
ChatParticipant chatParticipant = chatParticipantFixture.참여자(siteUser.getId(), chatRoom);
74+
chatMessage = chatMessageFixture.메시지("채팅", chatParticipant.getId(), chatRoom);
6975
}
7076

7177
@Nested

0 commit comments

Comments
 (0)