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

117 [feature] admin API 검색 기능 추가 #118

Merged
merged 5 commits into from
Oct 11, 2022
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
25 changes: 25 additions & 0 deletions src/main/java/com/jasik/momsnaggingapi/domain/admin/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

Expand Down Expand Up @@ -40,4 +41,28 @@ public static class DashBoardResponse {
private ServiceData service;
private GradeData grade;
}

@Schema(description = "문의 조회 시 응답 클래스")
@Getter @Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class QuestionResponse {

@Schema(description = "문의사항 id", defaultValue = "1")
private Long id;

@Schema(description = "문의 제목", defaultValue = "문의있습니다")
private String title;

@Schema(description = "문의 내용", defaultValue = "있었는데 없습니다.")
private String context;

@Schema(description = "사용자 id", defaultValue = "")
private String userId;

@Schema(description = "문의 작성 일시", defaultValue = "2022-05-08 12:00:00")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand Down Expand Up @@ -59,22 +60,38 @@ public ResponseEntity<Admin.DashBoardResponse> getAdmin() {
@GetMapping("/users")
@Operation(summary = "회원 정보 전체 가져오기", description = "관리자에서 보여줄 유저 리스트를 조회합니다.")
public ResponseEntity<Page<User.AdminResponse>> getAllUsers(
@PageableDefault(size=10, sort="id", direction = Sort.Direction.ASC) Pageable pageable
@Parameter(description = "페이지 정보 \n\n page - 페이지 번호. 0부터 시작 \n\n " +
"size - default 10으로 설정. 다를 경우에만 전송 \n\n " +
"sort - 해당 칼럼 오름차순 정렬. default id 이므로 다를 경우에만 전송" +
"(ex. a 칼럼으로 오름차순 정렬 - [\"a\"], 내림차순 정렬 - [\"a,DESC\"]")
@PageableDefault(size=10, sort="id", direction = Sort.Direction.ASC) Pageable pageable,
@Parameter(required = false, description = "검색할 아이디")
@RequestParam(value = "search", required = false) String search
) {
return ResponseEntity.ok().body(adminService.getUsers(pageable));
return ResponseEntity.ok().body(adminService.getUsers(pageable, search));
}

@GetMapping("/questions")
@Operation(summary = "문의사항 전체 가져오기", description = "관리자에서 보여줄 문의사항 리스트를 조회합니다.")
public ResponseEntity<Page<Question.QuestionResponse>> getAllQuestions(
@PageableDefault(size=10, sort="id", direction = Sort.Direction.ASC) Pageable pageable
public ResponseEntity<Page<Admin.QuestionResponse>> getAllQuestions(
@Parameter(description = "페이지 정보 \n\n page - 페이지 번호. 0부터 시작 \n\n " +
"size - default 10으로 설정. 다를 경우에만 전송 \n\n " +
"sort - 해당 칼럼 오름차순 정렬. default id 이므로 다를 경우에만 전송" +
"(ex. a 칼럼으로 오름차순 정렬 - [\"a\"], 내림차순 정렬 - [\"a,DESC\"]")
@PageableDefault(size=10, sort="id", direction = Sort.Direction.ASC) Pageable pageable,
@Parameter(required = false, description = "검색할 아이디")
@RequestParam(value = "search", required = false) String search
) {
return ResponseEntity.ok().body(adminService.getQuestions(pageable));
return ResponseEntity.ok().body(adminService.getQuestions(pageable, search));
}

@GetMapping("/sign-out")
@Operation(summary = "탈퇴사유 전체 가져오기", description = "관리자에서 보여줄 탈퇴사유 리스트를 조회합니다.")
public ResponseEntity<Page<Question.SignOutReasonResponse>> getAllSignOutReasons(
@Parameter(description = "페이지 정보 \n\n page - 페이지 번호. 0부터 시작 \n\n " +
"size - default 10으로 설정. 다를 경우에만 전송 \n\n " +
"sort - 해당 칼럼 오름차순 정렬. default id 이므로 다를 경우에만 전송 " +
"(ex. a 칼럼으로 오름차순 정렬 - [\"a\"], 내림차순 정렬 - [\"a,DESC\"])")
@PageableDefault(size=10, sort="id", direction = Sort.Direction.ASC) Pageable pageable
) {
return ResponseEntity.ok().body(adminService.getSignOutReasons(pageable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.jasik.momsnaggingapi.domain.push.Push;
import com.jasik.momsnaggingapi.domain.push.Push.PushType;
import com.jasik.momsnaggingapi.domain.push.service.PushService;
import com.jasik.momsnaggingapi.domain.question.Interface.QuestionUserInterface;
import com.jasik.momsnaggingapi.domain.question.Question;
import com.jasik.momsnaggingapi.domain.question.service.QuestionService;
import com.jasik.momsnaggingapi.domain.schedule.Interface.ScheduleNaggingInterface;
Expand Down Expand Up @@ -66,8 +67,8 @@ public Admin.GradeData getGradeData() {
return gradeData;
}

public Page<User.AdminResponse> getUsers(Pageable pageable) {
Page<User> userPage = userService.findAllUsers(pageable);
public Page<User.AdminResponse> getUsers(Pageable pageable, String search) {
Page<User> userPage = userService.findAllUsers(pageable, search);
Page<User.AdminResponse> userDataList = userPage.map(m -> User.AdminResponse.builder()
.id(m.getId())
.provider(m.getProvider())
Expand All @@ -90,14 +91,15 @@ public Page<Question.SignOutReasonResponse> getSignOutReasons(Pageable pageable)
.build());
}

public Page<Question.QuestionResponse> getQuestions(Pageable pageable) {
Page<Question> questionPage = questionService.findAllQuestions(pageable);
return questionPage.map(q -> Question.QuestionResponse.builder()
.id(q.getId())
.title(q.getTitle())
.context(q.getContext())
.createdAt(q.getCreatedAt())
.userId(q.getUserId()) // TODO: userId -> personalId 값으로 변경
public Page<Admin.QuestionResponse> getQuestions(Pageable pageable, String search) {
Page<QuestionUserInterface> questionPage = questionService.findAllQuestions(pageable, search);

return questionPage.map(q -> Admin.QuestionResponse.builder()
.id(q.getQuestion().getId())
.title(q.getQuestion().getTitle())
.context(q.getQuestion().getContext())
.createdAt(q.getQuestion().getCreatedAt())
.userId(q.getUser().getPersonalId())
.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jasik.momsnaggingapi.domain.question.Interface;

import com.jasik.momsnaggingapi.domain.question.Question;
import com.jasik.momsnaggingapi.domain.user.User;

public interface QuestionUserInterface {
Question getQuestion();
User getUser();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.jasik.momsnaggingapi.domain.question.repository;

import com.jasik.momsnaggingapi.domain.question.Interface.QuestionUserInterface;
import com.jasik.momsnaggingapi.domain.question.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface QuestionRepository extends JpaRepository<Question, Long> {
@Transactional
@Query("select q as question, u as user from Question q inner join User u " +
"on q.userId = u.id " +
"where (:personalId is null or u.personalId LIKE %:personalId%) and q.isQ = :isQ")
Page<QuestionUserInterface> findAllQuestions(@Param("personalId") String personalId, @Param("isQ") boolean isQ, Pageable pageable);

Page<Question> findAllByIsQ(boolean isQ, Pageable pageable);

Long countByIsQ(boolean isQ);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package com.jasik.momsnaggingapi.domain.question.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jasik.momsnaggingapi.domain.diary.Diary;
import com.jasik.momsnaggingapi.domain.diary.Diary.DiaryResponse;
import com.jasik.momsnaggingapi.domain.question.Interface.QuestionUserInterface;
import com.jasik.momsnaggingapi.domain.question.Question;
import com.jasik.momsnaggingapi.domain.question.Question.QuestionResponse;
import com.jasik.momsnaggingapi.domain.question.repository.QuestionRepository;
import com.jasik.momsnaggingapi.domain.schedule.Schedule;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.jasik.momsnaggingapi.domain.user.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
Expand Down Expand Up @@ -59,8 +50,8 @@ public Long countAllQuestions() {
}

@Transactional
public Page<Question> findAllQuestions(Pageable pageable) {
return questionRepository.findAllByIsQ(true, pageable);
public Page<QuestionUserInterface> findAllQuestions(Pageable pageable, String search) {
return questionRepository.findAllQuestions(search, true, pageable);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.jasik.momsnaggingapi.domain.user.repository;

import com.jasik.momsnaggingapi.domain.user.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.awt.print.Pageable;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByPersonalId(String personalId);
Optional<User> findByProviderCode(String ProviderCode);
Optional<User> findByProviderCodeAndProvider(String providerCode, String provider);
Page<User> findByPersonalIdContainingIgnoreCase(String personalId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public User.UserResponse findUser(Long id) {
return modelMapper.map(user, User.UserResponse.class);
}

public Page<User> findAllUsers(Pageable pageable) {
return userRepository.findAll(pageable);
public Page<User> findAllUsers(Pageable pageable, String search) {
return userRepository.findByPersonalIdContainingIgnoreCase(search, pageable);
}

@Transactional
Expand Down