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 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
3 changes: 2 additions & 1 deletion admin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dependencies {
implementation project(':common:domain')
implementation project(':common:format')
implementation project(':common:exception')

implementation project(':common:auth')

testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Expand Down
6 changes: 6 additions & 0 deletions admin/src/main/java/org/yapp/auth/application/AdminRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.yapp.auth.application;

public enum AdminRole {
ADMIN,
ROLE_ADMIN;
}
40 changes: 40 additions & 0 deletions admin/src/main/java/org/yapp/auth/application/AuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.yapp.auth.application;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.yapp.auth.AuthToken;
import org.yapp.auth.AuthTokenGenerator;
import org.yapp.auth.application.dto.LoginDto;
import org.yapp.domain.user.User;
import org.yapp.error.code.auth.AuthErrorCode;
import org.yapp.error.exception.ApplicationException;
import org.yapp.user.application.UserService;

@Slf4j
@Service
@RequiredArgsConstructor
public class AuthService {

private final UserService userService;

private final AuthTokenGenerator authTokenGenerator;

@Value("${admin.password}")
private String PWD;

public AuthToken login(LoginDto loginDto) {
String oauthId = loginDto.oauthId();
String password = loginDto.password();

User loginUser = userService.getUserByOauthId(oauthId);

if (password.equals(PWD) && loginUser.getRole().equals(AdminRole.ADMIN.toString())) {
return authTokenGenerator.generate(loginUser.getId(), null,
AdminRole.ROLE_ADMIN.toString());
} else {
throw new ApplicationException(AuthErrorCode.ACCESS_DENIED);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.yapp.auth.application.dto;

public record LoginDto(String oauthId, String password) {

}
28 changes: 28 additions & 0 deletions admin/src/main/java/org/yapp/auth/presentation/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.yapp.auth.presentation;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.auth.AuthToken;
import org.yapp.auth.application.AuthService;
import org.yapp.auth.presentation.request.LoginRequest;
import org.yapp.util.CommonResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/v1/auth/")
public class AuthController {

private final AuthService authService;

@PostMapping("/login")
public ResponseEntity<CommonResponse<AuthToken>> login(
@RequestBody @Valid LoginRequest loginRequest) {
AuthToken authToken = authService.login(loginRequest.toDto());
return ResponseEntity.ok(CommonResponse.createSuccess(authToken));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.yapp.auth.presentation.request;

import jakarta.validation.constraints.NotNull;
import org.yapp.auth.application.dto.LoginDto;

public record LoginRequest(@NotNull String loginId, @NotNull String password) {

public LoginDto toDto() {
return new LoginDto(loginId, password);
}
}
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) {

}
7 changes: 6 additions & 1 deletion admin/src/main/java/org/yapp/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatchers;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.yapp.jwt.JwtFilter;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

private final JwtFilter jwtFilter;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
Expand All @@ -33,6 +37,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.permitAll()
.anyRequest()
.hasAnyRole("ADMIN"))
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}

Expand All @@ -47,6 +52,6 @@ private CorsConfigurationSource corsConfigurationSource() {
}

private RequestMatcher getMatcherForAnyone() {
return RequestMatchers.anyOf(antMatcher("/admin/v1/login/**"));
return RequestMatchers.anyOf(antMatcher("/admin/v1/auth/login/**"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.yapp.profile.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yapp.domain.profile.Profile;
import org.yapp.domain.profile.ProfileRejectHistory;
import org.yapp.domain.profile.ProfileStatus;
import org.yapp.domain.user.RoleStatus;
import org.yapp.domain.user.User;
import org.yapp.error.dto.ProfileErrorCode;
import org.yapp.error.exception.ApplicationException;
import org.yapp.profile.dao.ProfileRejectHistoryRepository;
import org.yapp.user.application.UserService;

@Service
@RequiredArgsConstructor
public class AdminProfileService {

private final ProfileRejectHistoryRepository profileRejectHistoryRepository;
private final UserService userService;

@Transactional
public void updateProfileStatus(Long userId, boolean reasonImage, boolean reasonDescription) {
User user = userService.getUserById(userId);
Profile profile = user.getProfile();

if (profile == null) {
throw new ApplicationException(ProfileErrorCode.NOTFOUND_PROFILE);
}

if (reasonImage || reasonDescription) {
rejectProfile(user.getProfile(), reasonImage, reasonDescription);
} else {
passProfile(profile);
}
}

private void rejectProfile(Profile profile, boolean reasonImage, boolean reasonDescription) {
profileRejectHistoryRepository.save(ProfileRejectHistory.builder()
.user(profile.getUser())
.reasonImage(reasonImage)
.reasonDescription(reasonDescription)
.build());

profile.updateProfileStatus(ProfileStatus.REJECTED);
profile.getUser().updateUserRole(RoleStatus.PENDING.getStatus());
}

private void passProfile(Profile profile) {
profile.updateProfileStatus(ProfileStatus.APPROVED);
profile.getUser().updateUserRole(RoleStatus.USER.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public User getUserById(Long userId) {
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
}

public User getUserByOauthId(String oauthId) {
return userRepository.findByOauthId(oauthId)
.orElseThrow(() -> new ApplicationException(UserErrorCode.NOTFOUND_USER));
}

public PageResponse<UserProfileValidationResponse> getUserProfilesWithPagination(int page,
int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Expand Down
2 changes: 2 additions & 0 deletions admin/src/main/java/org/yapp/user/dao/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findById(Long userId);

Optional<User> findByOauthId(String oauthId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.yapp.user.presentation;

import jakarta.validation.constraints.NotNull;

public record UpdateProfileStatusRequest(@NotNull boolean rejectImage,
@NotNull boolean rejectDescription) {

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.yapp.user.presentation;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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 org.yapp.profile.application.AdminProfileService;
import org.yapp.user.application.UserService;
import org.yapp.user.presentation.response.UserProfileDetailResponses;
import org.yapp.user.presentation.response.UserProfileValidationResponse;
Expand All @@ -19,6 +23,7 @@
public class UserController {

private final UserService userService;
private final AdminProfileService adminProfileService;

@GetMapping("")
public ResponseEntity<CommonResponse<PageResponse<UserProfileValidationResponse>>> getUsers(
Expand All @@ -33,9 +38,20 @@ public ResponseEntity<CommonResponse<PageResponse<UserProfileValidationResponse>

@GetMapping("/{userId}")
public ResponseEntity<CommonResponse<UserProfileDetailResponses>> getUserProfile(
@PathVariable long userId) {
@PathVariable Long userId) {

UserProfileDetailResponses userProfileDetails = userService.getUserProfileDetails(userId);
return ResponseEntity.ok(CommonResponse.createSuccess(userProfileDetails));
}

@PostMapping("/{userId}/profile")
public ResponseEntity<CommonResponse<Void>> updateUserProfileStatus(
@PathVariable Long userId,
@RequestBody @Valid UpdateProfileStatusRequest request) {

adminProfileService.updateProfileStatus(userId, request.rejectImage(),
request.rejectDescription());

return ResponseEntity.ok(CommonResponse.createSuccess(null));
}
}
Loading