Skip to content

Commit

Permalink
Merge branch 'feature/120' of https://github.com/qp-official-org/QP_B…
Browse files Browse the repository at this point in the history
…ACKEND into feature/120
  • Loading branch information
Jinho622 committed Feb 19, 2024
2 parents 2b2ed2d + c436472 commit 47cbf69
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 71 deletions.
17 changes: 4 additions & 13 deletions src/main/java/qp/official/qp/converter/AnswerConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AnswerConverter {

public static Answer toAnswer(AnswerRequestDTO.AnswerCreateDTO request){
return Answer.builder()
.title(request.getTitle())
.content(request.getContent())
.category(request.getCategory())
.answerGroup(request.getAnswerGroup())
Expand Down Expand Up @@ -52,16 +51,13 @@ private static List<ParentAnswerPreviewDTO> getParentAnswerPreviewDTOS(
List<ParentAnswerPreviewDTO> parentAnswerPreviewDTOList = parentAnswerList.getContent()
.stream()
.map(answer -> new ParentAnswerPreviewDTO(
UserConverter.toUserPreviewWithAnswerDTO(answer.getUser()),
answer.getAnswerId(),
answer.getUser().getUserId(),
answer.getUser().getNickname(),
answer.getUser().getRole(),
answer.getUser().getProfileImage(),
answer.getTitle(),
answer.getContent(),
answer.getCategory(),
answer.getAnswerGroup(),
answer.getAnswerLikesList().size()
answer.getAnswerLikesList().size(),
answer.getChildren().size()
))
.collect(Collectors.toList());
return parentAnswerPreviewDTOList;
Expand All @@ -83,12 +79,8 @@ public static AnswerResponseDTO.ChildAnswerPreviewListDTO childAnswerPreviewList
private static List<ChildAnswerPreviewDTO> getChildAnswerPreviewDTOS(Page<Answer> childAnswerList) {
List<ChildAnswerPreviewDTO> childAnswerDTOList = childAnswerList.getContent().stream()
.map(answer -> new ChildAnswerPreviewDTO(
UserConverter.toUserPreviewWithAnswerDTO(answer.getUser()),
answer.getAnswerId(),
answer.getUser().getUserId(),
answer.getUser().getNickname(),
answer.getUser().getRole(),
answer.getUser().getProfileImage(),
answer.getTitle(),
answer.getContent(),
answer.getCategory(),
answer.getAnswerGroup(),
Expand All @@ -102,7 +94,6 @@ private static List<ChildAnswerPreviewDTO> getChildAnswerPreviewDTOS(Page<Answer
public static AnswerResponseDTO.UpdateResultDTO toUpdateResultDTO(Answer answer) {
return AnswerResponseDTO.UpdateResultDTO.builder()
.answerId(answer.getAnswerId())
.title(answer.getTitle())
.content(answer.getContent())
.updatedAt(answer.getUpdatedAt())
.build();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/qp/official/qp/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static UserResponseDTO.GetUserInfoDTO toUserGetInfoDTO(User user) {
public static UserResponseDTO.UpdateUserInfoDTO toUserUpdateDTO(User user) {
return UserResponseDTO.UpdateUserInfoDTO.builder()
.userId(user.getUserId())
.role(user.getRole())
.nickname(user.getNickname())
.profileImage(user.getProfileImage())
.updatedAt(user.getUpdatedAt())
Expand All @@ -50,7 +51,16 @@ public static UserResponseDTO.AutoLoginDTO toUserAutoLoginDTO(TokenResponseDTO r
public static UserResponseDTO.UserPreviewInQuestionDTO toUserPreviewWithQuestionDTO(User user) {
return UserResponseDTO.UserPreviewInQuestionDTO.builder()
.userId(user.getUserId())
.ROLE(user.getRole())
.role(user.getRole())
.profileImage(user.getProfileImage())
.build();
}

public static UserResponseDTO.UserPreviewInAnswerDTO toUserPreviewWithAnswerDTO(User user) {
return UserResponseDTO.UserPreviewInAnswerDTO.builder()
.userId(user.getUserId())
.nickname(user.getNickname())
.role(user.getRole())
.profileImage(user.getProfileImage())
.build();
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/qp/official/qp/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public class Answer extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long answerId;

@Column(nullable = false, length = 50)
private String title;

@Column(columnDefinition = "TEXT")
private String content;

Expand Down Expand Up @@ -108,7 +105,6 @@ public void setUser(User user) {


public void update(AnswerRequestDTO.AnswerUpdateDTO request) {
this.title = request.getTitle();
this.content = request.getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import qp.official.qp.domain.Hashtag;
import qp.official.qp.domain.Question;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.ChildStatus;
import qp.official.qp.domain.mapping.UserQuestionAlarm;
import qp.official.qp.repository.HashtagRepository;
import qp.official.qp.repository.QuestionHashTagRepository;
Expand All @@ -17,6 +18,7 @@
import qp.official.qp.repository.UserRepository;
import qp.official.qp.web.dto.QuestionRequestDTO;

import java.util.ArrayList;
import java.util.List;

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.json.simple.parser.ParseException;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.Role;
import qp.official.qp.web.dto.UserRequestDTO;
import java.io.IOException;
public interface UserService {
Expand All @@ -13,4 +14,6 @@ public interface UserService {
User autoSignIn(Long userId);
User deleteUser(Long userId);
User updateUserPoint(Long userId, UserRequestDTO.UpdateUserPointRequestDTO requestDTO);

User changeUserRole(Long userId, Role role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ public User updateUserPoint(Long userId, UserRequestDTO.UpdateUserPointRequestDT

return user;
}

@Override
@Transactional
public User changeUserRole(Long userId, Role role) {
User user = userRepository.findById(userId).get();
user.updateRole(role);
return user;
}
}
19 changes: 17 additions & 2 deletions src/main/java/qp/official/qp/web/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import qp.official.qp.apiPayload.code.status.SuccessStatus;
import qp.official.qp.converter.UserConverter;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.Role;
import qp.official.qp.service.TokenService.TokenService;
import qp.official.qp.service.UserService.UserService;
import qp.official.qp.validation.annotation.ExistUser;
Expand Down Expand Up @@ -125,7 +126,7 @@ public ApiResponse<UserResponseDTO.deleteUserDTO> delete(
);
}

@Operation(summary = "테스트 유저 생성", description =
@Operation(summary = "(개발용) 테스트 유저 생성", description =
"# Test User를 생성합니다. 다른 기능을 테스트 할때 이용 하세요 \n" +
"새로운 유저의 token, refreshToken도 같이 반환합니다."
, security = {@SecurityRequirement(name = "accessToken"), @SecurityRequirement(name = "refreshToken")}
Expand All @@ -142,11 +143,25 @@ public ApiResponse<UserResponseDTO.UserSignUpResultDTO> createTestUser() {
);
}

@Operation(summary = "(개발용) 계정 Role 변경", description = "특정 유저의 Role을 전환합니다.")
@PostMapping("/{userId}")
public ApiResponse<UserResponseDTO.UpdateUserInfoDTO> changeUserRole(
@PathVariable @ExistUser Long userId,
@RequestParam Role role
) {
return ApiResponse.onSuccess(
SuccessStatus.User_OK,
UserConverter.toUserUpdateDTO(
userService.changeUserRole(userId, role)
)
);
}

@PatchMapping("/point/{userId}")
@Operation(
summary = "유저 point 충전 또는 차감 API"
, description = "Header에 accessToken 필요. path variable로 userId를 입력하고, Request body에 충전 또는 차감할 point를 입력하세요. \n" +
"Response의 point는 회원이 보유한 총 point입니다."
"Response의 point는 회원이 보유한 총 point입니다."
, security = @SecurityRequirement(name = "accessToken")
)
public ApiResponse<UserResponseDTO.UpdateUserPointDTO> UpdateUserPoint(
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/qp/official/qp/web/dto/AnswerRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public static class AnswerCreateDTO {
@ExistUser
Long userId;
@NotBlank
String title;
@NotBlank
String content;
@NotNull(message = "카테고리는 'PARENT', 'CHILD' 중 하나여야 합니다.")
Category category;
Expand All @@ -36,8 +34,6 @@ public static class AnswerUpdateDTO {
@ExistUser
Long userId;
@NotBlank
String title;
@NotBlank
String content;
}
}
14 changes: 3 additions & 11 deletions src/main/java/qp/official/qp/web/dto/AnswerResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ public static class ParentAnswerPreviewListDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class ParentAnswerPreviewDTO {
UserResponseDTO.UserPreviewInAnswerDTO user;
Long answerId;
Long userId;
String nickname;
Role role;
String profileImage;
String title;
String content;
Category category;
Long answerGroup;
Integer likeCount;
Integer childAnswerCount;
}

@Builder
Expand All @@ -69,12 +66,8 @@ public static class ChildAnswerPreviewListDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class ChildAnswerPreviewDTO {
UserResponseDTO.UserPreviewInAnswerDTO user;
Long answerId;
Long userId;
String nickname;
Role role;
String profileImage;
String title;
String content;
Category category;
Long answerGroup;
Expand All @@ -87,7 +80,6 @@ public static class ChildAnswerPreviewDTO {
@AllArgsConstructor
public static class UpdateResultDTO {
Long answerId;
String title;
String content;
Long likeCount;
LocalDateTime updatedAt;
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/qp/official/qp/web/dto/UserResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class GetUserInfoDTO {
@AllArgsConstructor
public static class UpdateUserInfoDTO {
Long userId;
Role role;
String nickname;
String profileImage;
LocalDateTime updatedAt;
Expand Down Expand Up @@ -88,7 +89,18 @@ public static class AutoLoginDTO {
public static class UserPreviewInQuestionDTO {
Long userId;
String profileImage;
Role ROLE;
Role role;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class UserPreviewInAnswerDTO {
Long userId;
String nickname;
String profileImage;
Role role;
}

@Builder
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/qp/official/qp/QpBackendApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,106 @@
package qp.official.qp;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import qp.official.qp.domain.Answer;
import qp.official.qp.domain.Hashtag;
import qp.official.qp.domain.Question;
import qp.official.qp.domain.User;
import qp.official.qp.domain.enums.Category;
import qp.official.qp.domain.enums.ChildStatus;
import qp.official.qp.service.AnswerService.AnswerCommandServiceImpl;
import qp.official.qp.service.QuestionService.QuestionCommandServiceImpl;
import qp.official.qp.service.UserService.UserServiceImpl;
import qp.official.qp.web.dto.AnswerRequestDTO;
import qp.official.qp.web.dto.QuestionRequestDTO;

import java.util.ArrayList;

@SpringBootTest
class QpBackendApplicationTests {

@Autowired
private UserServiceImpl userService;

@Autowired
private QuestionCommandServiceImpl questionCommandService;

@Autowired
private AnswerCommandServiceImpl answerCommandService;

@Test
void contextLoads() {
}

// Create 30 dummy questions
// Create 3 parent answers for each question
// Create 2 child answers for each parent answer
// 부모답변1-자식답변1, 부모답변2-자식답변2, 부모답변3 형식으로 되어 있다.
// @Test
// @Rollback(value = false)
// public void createDummyQuestion() {
// User testUser = userService.createTestUser();
//
// // 질문 30개 생성
// int questionSize = 30;
// for (int i = 0; i < questionSize; i++){
// String title = "title" + i;
// String content = "content" + i;
// ArrayList<Long> hashtagIds = new ArrayList<>();
// ArrayList<Hashtag> hashtags = new ArrayList<>();
//
// int hashtagSize = 2;
// for (int j = 1; j <= hashtagSize; j++) {
// Hashtag newHashTag = Hashtag.builder()
// .hashtagId(((long) j))
// .hashtag("hashtag" + j)
// .questionHashTagList(new ArrayList<>())
// .build();
// hashtags.add(newHashTag);
// }
//
// QuestionRequestDTO.CreateDTO newQuestion = QuestionRequestDTO.CreateDTO.builder()
// .userId(testUser.getUserId())
// .title(title)
// .content(content)
// .childStatus(ChildStatus.INACTIVE)
// .hashtag(hashtagIds)
// .build();
// Question question = questionCommandService.createQuestion(newQuestion);
//
// // parent 답변 3개 생성
// int parentAnswerSize = 3;
// for(int j = 0; j < parentAnswerSize; j++){
// String parentAnswerTitle = "parentTitle"+j;
// String parentAnswerContent = "parentContent"+j;
// AnswerRequestDTO.AnswerCreateDTO newAnswer = AnswerRequestDTO.AnswerCreateDTO.builder()
// .userId(testUser.getUserId())
// .title(parentAnswerTitle)
// .content(parentAnswerContent)
// .category(Category.PARENT)
// .build();
// Answer parent = answerCommandService.createAnswer(newAnswer, question.getQuestionId());
//
// // child 답변은 두 개만 생성
// if(j < parentAnswerSize-1) {
// String childAnswerTitle = "childAnswerTitle";
// String childAnswerContent = "childAnswerContent";
// AnswerRequestDTO.AnswerCreateDTO newChildAnswer = AnswerRequestDTO.AnswerCreateDTO.builder()
// .userId(testUser.getUserId())
// .title(childAnswerTitle)
// .content(childAnswerContent)
// .category(Category.CHILD)
// .answerGroup(parent.getAnswerId())
// .build();
// answerCommandService.createAnswer(newChildAnswer, question.getQuestionId());
// }
//
// }
//
// }
//
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AnswerLikesRepositoryTest {

void setUp(){
answer = Answer.builder()
.title("testTitle")
.content("testContent")
.build();

answerRepository.save(answer);
Expand Down
Loading

0 comments on commit 47cbf69

Please sign in to comment.