Skip to content

Commit

Permalink
Merge pull request #25 from YAPP-Github/feat/PC-380-admin-profile-read
Browse files Browse the repository at this point in the history
[PC-380] 어드민 유저 프로필 조회 기능
  • Loading branch information
Lujaec authored Jan 25, 2025
2 parents fb13ec4 + 7364362 commit c1b81ef
Show file tree
Hide file tree
Showing 94 changed files with 2,103 additions and 328 deletions.
5 changes: 5 additions & 0 deletions admin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ repositories {
}

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
12 changes: 12 additions & 0 deletions admin/src/main/java/org/yapp/AdminApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.yapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AdminApplication {

public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
7 changes: 0 additions & 7 deletions admin/src/main/java/org/yapp/Main.java

This file was deleted.

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) {

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

import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;

import java.util.Collections;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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)
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource()))
.httpBasic(AbstractHttpConfigurer::disable)
.sessionManagement(configurer -> configurer.sessionCreationPolicy(
SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(registry -> registry
.requestMatchers(getMatcherForAnyone())
.permitAll()
.anyRequest()
.hasAnyRole("ADMIN"))
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}

private CorsConfigurationSource corsConfigurationSource() {
return request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedOriginPatterns(Collections.singletonList("*"));
return config;
};
}

private RequestMatcher getMatcherForAnyone() {
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
@@ -0,0 +1,12 @@
package org.yapp.profile.dao;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.yapp.domain.profile.ProfileRejectHistory;

@Repository
public interface ProfileRejectHistoryRepository extends JpaRepository<ProfileRejectHistory, Long> {

Optional<ProfileRejectHistory> findTopByUserIdOrderByCreatedAtDesc(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.yapp.profile.dao;

import io.lettuce.core.dynamic.annotation.Param;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.yapp.domain.profile.ProfileValueTalk;

@Repository
public interface ProfileValueTalkRepository extends JpaRepository<ProfileValueTalk, Long> {

@Query("SELECT pvt FROM ProfileValueTalk pvt " +
"JOIN FETCH pvt.valueTalk vt " +
"WHERE vt.isActive = true AND pvt.profile.id = :profileId")
List<ProfileValueTalk> findActiveProfileValueTalksByProfileId(
@Param("profileId") Long profileId);
}
Loading

0 comments on commit c1b81ef

Please sign in to comment.