Skip to content

Commit

Permalink
Merge pull request #38 from Team-baebae/feature/question/#34
Browse files Browse the repository at this point in the history
Feature/question/#34
  • Loading branch information
jihyo-j authored May 5, 2024
2 parents 8b2b404 + d77b7fc commit 8cc6f04
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,17 @@ public class QuestionApplication {
private final QuestionMapper questionMapper;
private final MemberRepository memberRepository;

public QuestionDetailResponse createQuestion(QuestionCreateRequest request, Long senderId, Long receiverId, String token) {
Member sender = memberRepository.findById(senderId)
.orElseThrow(() -> new BusinessException(MemberError.NOT_EXIST_MEMBER));
Member receiver = memberRepository.findById(receiverId)
public QuestionDetailResponse createQuestion(QuestionCreateRequest request, Long memberId, String token) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new BusinessException(MemberError.NOT_EXIST_MEMBER));

Question questionEntity = questionMapper.toEntity(request, sender, receiver);
Question questionEntity = questionMapper.toEntity(request, member);
Question savedQuestionEntity = questionService.createQuestion(questionEntity);
return questionMapper.toDomain(savedQuestionEntity, token);
}

public Page<QuestionDetailResponse> getAllQuestions(Long memberId, Pageable pageable, boolean isSender) {
Page<Question> questionPage;
if (isSender) {
questionPage = questionService.getQuestionsBySenderId(memberId, pageable);
} else {
questionPage = questionService.getQuestionsByReceiverId(memberId, pageable);
}
public Page<QuestionDetailResponse> getAllQuestions(Long memberId, Pageable pageable) {
Page<Question> questionPage = questionService.getQuestionsByMemberId(memberId, pageable);
return questionPage.map(question -> questionMapper.toDomain(question, "appropriate token here"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ public Question createQuestion(Question question) {
}

@Transactional(readOnly = true)
public Page<Question> getQuestionsBySenderId(Long senderId, Pageable pageable) {
return questionRepository.findAllBySenderId(senderId, pageable);
}

@Transactional(readOnly = true)
public Page<Question> getQuestionsByReceiverId(Long receiverId, Pageable pageable) {
return questionRepository.findAllByReceiverId(receiverId, pageable);
public Page<Question> getQuestionsByMemberId(Long memberId, Pageable pageable) {
return questionRepository.findAllByMemberId(memberId, pageable);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ public class Question {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sender_id", nullable = false)
private Member sender;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "receiver_id", nullable = false)
private Member receiver;
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;
Expand All @@ -42,9 +38,9 @@ public class Question {
public void updateContent(String content) {
this.content = content;
}
public static Question of(Long id, Member sender, Member receiver, String content, String nickname, Boolean profileOnOff,
public static Question of(Long id, Member member, String content, String nickname, Boolean profileOnOff,
LocalDateTime createdDate) {
return new Question(id, sender, receiver, content, nickname, profileOnOff, createdDate);
return new Question(id, member, content, nickname, profileOnOff, createdDate);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionJpaRepository extends JpaRepository<Question, Long> {
Page<Question> findAllBySenderId(Long senderId, Pageable pageable);
Page<Question> findAllByReceiverId(Long receiverId, Pageable pageable);
Page<Question> findAllByMemberId(Long memberId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
@Component
@RequiredArgsConstructor
public class QuestionMapper {
public Question toEntity(QuestionCreateRequest request, Member sender, Member receiver) {
public Question toEntity(QuestionCreateRequest request, Member member) {
return Question.builder()
.sender(sender)
.receiver(receiver)
.member(member)
.content(request.getContent())
.nickname(request.getNickname())
.profileOnOff(request.getProfileOnOff())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
public interface QuestionRepository{
Optional<Question> findById(Long questionId);
Question save(Question questionEntity);
Page<Question> findAllBySenderId(Long senderId, Pageable pageable);
Page<Question> findAllByReceiverId(Long receiverId, Pageable pageable);
Page<Question> findAllByMemberId(Long memberId, Pageable pageable);
void delete(Question questionEntity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ public Question save(Question questionEntity) {
}

@Override
public Page<Question> findAllBySenderId(Long senderId, Pageable pageable) {
return questionJpaRepository.findAllBySenderId(senderId, pageable);
}

@Override
public Page<Question> findAllByReceiverId(Long receiverId, Pageable pageable) {
return questionJpaRepository.findAllByReceiverId(receiverId, pageable);
public Page<Question> findAllByMemberId(Long memberId, Pageable pageable) {
return questionJpaRepository.findAllByMemberId(memberId, pageable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,31 @@ public QuestionController(QuestionApplication questionApplication, JwtTokenProvi
@Operation(summary = "질문 생성")
@PostMapping("/member/{memberId}")
public ResponseEntity<QuestionDetailResponse> createQuestion(
@RequestBody QuestionCreateRequest questionDTO, @PathVariable Long memberId, @RequestParam Long receiverId,
@RequestBody QuestionCreateRequest questionDTO, @PathVariable Long memberId,
@RequestHeader("Authorization") String token) {

if (!tokenProvider.validToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
QuestionDetailResponse createdQuestion = questionApplication.createQuestion(questionDTO, memberId, receiverId, token);
QuestionDetailResponse createdQuestion = questionApplication.createQuestion(questionDTO, memberId, token);
return ResponseEntity.status(HttpStatus.CREATED).body(createdQuestion);
}

@Operation(summary = "질문 조회")
@GetMapping()
public ResponseEntity<List<QuestionDetailResponse>> getAllQuestions(
@RequestParam Long memberId,
@RequestParam boolean isSender,
Pageable pageable) {
Page<QuestionDetailResponse> questions = questionApplication.getAllQuestions(memberId, pageable, isSender);
Page<QuestionDetailResponse> questions = questionApplication.getAllQuestions(memberId, pageable);
return ResponseEntity.ok(questions.getContent());
}

@Operation(summary = "질문 수정")
@PutMapping("/{questionId}")
public ResponseEntity<Void> updateQuestion(@PathVariable Long questionId, @RequestParam String content, @RequestHeader("Authorization") String token) {
if (!tokenProvider.validToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
questionApplication.updateQuestion(questionId, content, token);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.web.baebaeBE.presentation.question.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
public class QuestionDetailResponse {
private Long questionId;
private String content;
private String userEmail;
private String nickname;
private LocalDateTime createdDate;
private String token;

public QuestionDetailResponse(Long questionId, String content, String userEmail, LocalDateTime createdDate, String token) {
public QuestionDetailResponse(Long questionId, String content, String nickname, LocalDateTime createdDate, String token) {
this.questionId = questionId;
this.content = content;
this.userEmail = userEmail;
this.nickname = nickname;
this.createdDate = createdDate;
this.token = token;
}
public static QuestionDetailResponse of(Long questionId, String content, String userEmail, LocalDateTime createdDate, String token) {
return new QuestionDetailResponse(questionId, content, userEmail, createdDate, token);
public static QuestionDetailResponse of(Long questionId, String content, String nickname, LocalDateTime createdDate, String token) {
return new QuestionDetailResponse(questionId, content, nickname, createdDate, token);
}
}

0 comments on commit 8cc6f04

Please sign in to comment.