Skip to content

Commit

Permalink
fix: 모든 답변조회시 카테고리 필터링 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyo-j committed May 13, 2024
1 parent e6201ce commit 7edd025
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.web.baebaeBE.domain.answer.dto.AnswerDetailResponse;
import com.web.baebaeBE.domain.answer.dto.AnswerResponse;
import com.web.baebaeBE.domain.answer.service.AnswerService;
import com.web.baebaeBE.domain.category.entity.Category;
import com.web.baebaeBE.domain.category.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -22,7 +24,7 @@
@RequestMapping("/api/answers")
public class AnswerController implements AnswerApi {
private final AnswerService answerService;

private final CategoryService categoryService;
@PostMapping(value = "/{memberId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<AnswerDetailResponse> createAnswer(@PathVariable Long memberId,
@RequestPart(value = "imageFile") MultipartFile imageFile,
Expand All @@ -38,9 +40,17 @@ public ResponseEntity<List<AnswerResponse>> getAnswersByMemberId(@PathVariable L
}

@GetMapping()
public ResponseEntity<List<AnswerDetailResponse>> getAllAnswers(@RequestParam Long memberId, Pageable pageable) {
Page<AnswerDetailResponse> answers = answerService.getAllAnswers(memberId, pageable);
return ResponseEntity.ok(answers.getContent());
public ResponseEntity<Page<AnswerDetailResponse>> getAllAnswers(
@RequestParam Long memberId,
@RequestParam(required = false) Long category,
Pageable pageable) {

Category cat = null;
if (category != null) {
cat = categoryService.getCategoryByNameOrId(category);
}
Page<AnswerDetailResponse> answers = answerService.getAllAnswers(memberId, cat, pageable);
return ResponseEntity.ok(answers);
}

@PutMapping(value = "/{answerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,45 @@ ResponseEntity<List<AnswerResponse>> getAnswersByMemberId(
@PathVariable Long memberId);
@Operation(
summary = "모든 답변 조회",
description = "모든 답변을 페이지네이션으로 조회합니다.",
description = "주어진 회원 ID와 선택적 카테고리에 따라 모든 답변을 페이지네이션으로 조회합니다.",
security = @SecurityRequirement(name = "bearerAuth")
)
@Parameter(
in = ParameterIn.HEADER,
name = "Authorization", required = true,
schema = @Schema(type = "string"),
description = "Bearer [Access 토큰]")
@ApiResponse(responseCode = "200", description = "답변 조회 성공",
description = "Bearer [Access token]"
)
@Parameter(
in = ParameterIn.QUERY,
name = "memberId",
required = true,
schema = @Schema(type = "integer"),
description = "회원 ID"
)
@Parameter(
in = ParameterIn.QUERY,
name = "category",
required = false,
schema = @Schema(type = "Integer"),
description = "카테고리 ID"
)
@ApiResponse(
responseCode = "200",
description = "답변 조회 성공",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = Page.class)))
@RequestMapping(method = RequestMethod.GET)
ResponseEntity<List<AnswerDetailResponse>> getAllAnswers(
schema = @Schema(implementation = Page.class))
)
@ApiResponse(
responseCode = "404",
description = "카테고리를 찾을 수 없습니다",
content = @Content(mediaType = "application/json")
)
@GetMapping()
ResponseEntity<Page<AnswerDetailResponse>> getAllAnswers(
@RequestParam Long memberId,
@RequestParam(required = false) Long category,
Pageable pageable);

@Operation(
summary = "답변 수정",
description = "기존 답변을 수정합니다.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.web.baebaeBE.domain.answer.entity;

import com.web.baebaeBE.domain.categorized.answer.entity.CategorizedAnswer;
import com.web.baebaeBE.domain.category.entity.Category;
import com.web.baebaeBE.domain.member.entity.Member;
import com.web.baebaeBE.domain.question.entity.Question;
import jakarta.persistence.*;
Expand All @@ -25,10 +26,14 @@ public class Answer {
@Column(name = "answer_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", nullable = false)
private Question question;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
Expand Down Expand Up @@ -73,12 +78,12 @@ public class Answer {



public static Answer of(Long id, Question question, Member member, String content,
public static Answer of(Long id, Question question, Category category, Member member, String content,
List<String> imageFiles, String musicName, String musicPicture,
String musicAudio, List<String> linkAttachments, int heartCount,
int curiousCount, int sadCount, LocalDateTime createdDate) {

return new Answer(id, question, member, imageFiles, content, musicName,
return new Answer(id, question, category, member, imageFiles, content, musicName,
musicPicture, musicAudio, linkAttachments, heartCount,
curiousCount, sadCount, createdDate,null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.web.baebaeBE.domain.answer.repository;

import com.web.baebaeBE.domain.answer.entity.Answer;
import com.web.baebaeBE.domain.category.entity.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,6 +10,8 @@

public interface AnswerJpaRepository extends JpaRepository<Answer, Long> {
Page<Answer> findAllByMemberId(Long memberId, Pageable pageable);
Page<Answer> findAllByMemberIdAndCategory(Long memberId, Category category, Pageable pageable);

List<Answer> findByMemberId(Long memberId);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.web.baebaeBE.domain.answer.repository;

import com.web.baebaeBE.domain.answer.entity.Answer;
import com.web.baebaeBE.domain.category.entity.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand All @@ -12,5 +13,7 @@ public interface AnswerRepository {
List<Answer> findByMemberId(Long memberId);
Answer save(Answer answer);
Page<Answer> findAllByMemberId(Long memberId, Pageable pageable);
Page<Answer> findAllByMemberIdAndCategory(Long memberId, Category category, Pageable pageable);

void delete(Answer answer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.web.baebaeBE.domain.answer.entity.Answer;
import com.web.baebaeBE.domain.answer.repository.AnswerJpaRepository;
import com.web.baebaeBE.domain.answer.repository.AnswerRepository;
import com.web.baebaeBE.domain.category.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -42,6 +43,10 @@ public Page<Answer> findAllByMemberId(Long memberId, Pageable pageable) {
return jpaRepository.findAllByMemberId(memberId, pageable);
}

@Override
public Page<Answer> findAllByMemberIdAndCategory(Long memberId, Category category, Pageable pageable) {
return jpaRepository.findAllByMemberIdAndCategory(memberId, category, pageable);
}
@Override
public void delete(Answer answer) {
jpaRepository.delete(answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.web.baebaeBE.domain.answer.dto.AnswerResponse;
import com.web.baebaeBE.domain.answer.exception.AnswerError;
import com.web.baebaeBE.domain.answer.repository.AnswerMapper;
import com.web.baebaeBE.domain.category.entity.Category;
import com.web.baebaeBE.domain.login.exception.LoginException;
import com.web.baebaeBE.domain.member.entity.Member;
import com.web.baebaeBE.domain.member.repository.MemberRepository;
Expand Down Expand Up @@ -72,9 +73,13 @@ public List<AnswerResponse> getAnswersByMemberId(Long memberId) {
}

@Transactional
public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Pageable pageable) {
Page<Answer> answerPage = answerRepository.findAllByMemberId(memberId, pageable);

public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Category category, Pageable pageable) {
Page<Answer> answerPage;
if (category == null) {
answerPage = answerRepository.findAllByMemberId(memberId, pageable);
} else {
answerPage = answerRepository.findAllByMemberIdAndCategory(memberId, category, pageable);
}
return answerPage.map(answer -> answerMapper.toDomain(answer, "FCM token needed"));
}

Expand Down Expand Up @@ -119,4 +124,5 @@ public void updateReactionCounts(Long answerId, int heartCount, int curiousCount
answer.setSadCount(sadCount);
answerRepository.save(answer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public void addAnswerToCategory(Long categoryId, Long answerId) {
.build();
categoryAnswerRepository.save(categorizedAnswer);
}

public Category getCategoryByNameOrId(Long identifier) {
return categoryRepository.findById(identifier).orElse(null);
}
public void deleteCategory(Long categoryId) {
categoryRepository.deleteById(categoryId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.springframework.beans.factory.annotation.Value;

import java.io.FileInputStream;

public class FirebaseInitializer {
public static void initializeFirebase() {
@Value("${fcm.certification}")
private String certification;

@Value("${fcm.databaseurl}")
private String databaseUrl;

public void initializeFirebase() {
try {
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
FileInputStream serviceAccount = new FileInputStream(certification);

FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl(databaseUrl)
.build();

FirebaseApp.initializeApp(options);
Expand Down

0 comments on commit 7edd025

Please sign in to comment.