Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Team-baebae/baebae-BE in…
Browse files Browse the repository at this point in the history
…to feature/answer/#39
  • Loading branch information
jihyo-j committed May 22, 2024
2 parents 3491a55 + b17e996 commit df9e238
Show file tree
Hide file tree
Showing 41 changed files with 519 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
run: |
cd baebae-BE
chmod +x ./gradlew
./gradlew build -x test
./gradlew build
7 changes: 5 additions & 2 deletions .github/workflows/deploy-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ jobs:

- name: Create and configure application.yml
run: |
mkdir -p baebae-BE/src/main/resources
mkdir -p baebae-BE/src/test/resources
echo "${{ secrets.APPLICATION_YML }}" > baebae-BE/src/main/resources/application.yml
echo "${{ secrets.APPLICATION_DEPLOY_YML }}" > baebae-BE/src/main/resources/application-deploy.yml
echo "${{ secrets.APPLICATION_TEST_YML }}" > baebae-BE/src/test/resources/application-test.yml
- name: create-json
id: create-json
Expand All @@ -39,7 +42,7 @@ jobs:
run: |
cd baebae-BE
chmod +x ./gradlew
./gradlew build -x test
./gradlew build
- name: Create deployment package
run: |
Expand All @@ -63,4 +66,4 @@ jobs:
run: |
cd baebae-BE
chmod +x request_source_deploy_start.sh
./request_source_deploy_start.sh
./request_source_deploy_start.sh
2 changes: 1 addition & 1 deletion .github/workflows/deploy-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: |
cd baebae-BE
chmod +x ./gradlew
./gradlew build -x test
./gradlew build
- name: Create deployment package
run: |
Expand Down
2 changes: 1 addition & 1 deletion .idea/dbnavigator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.web.baebaeBE.domain.answer.service.AnswerService;

import com.web.baebaeBE.domain.reaction.entity.ReactionValue;
import com.web.baebaeBE.global.authorization.annotation.AuthorizationAnswer;
import com.web.baebaeBE.global.authorization.annotation.AuthorizationMember;
import com.web.baebaeBE.global.authorization.annotation.AuthorizationQuestion;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -26,6 +29,7 @@ public class AnswerController implements AnswerApi {
private final AnswerService answerService;

@PostMapping(value = "/{memberId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@AuthorizationMember
public ResponseEntity<AnswerDetailResponse> createAnswer(@PathVariable Long memberId,
@RequestPart(value = "imageFile") MultipartFile imageFile,
@RequestPart AnswerCreateRequest request) {
Expand All @@ -43,6 +47,7 @@ public ResponseEntity<Page<AnswerDetailResponse>> getAllAnswers(
}

@PutMapping(value = "/{answerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@AuthorizationAnswer
public ResponseEntity<AnswerDetailResponse> updateAnswer(@PathVariable Long answerId,
@RequestPart(value = "imageFile", required = false) MultipartFile imageFile,
@RequestPart AnswerCreateRequest request) {
Expand All @@ -51,6 +56,7 @@ public ResponseEntity<AnswerDetailResponse> updateAnswer(@PathVariable Long answ
}

@DeleteMapping("/{answerId}")
@AuthorizationAnswer
public ResponseEntity<Void> deleteAnswer(@PathVariable Long answerId) {
answerService.deleteAnswer(answerId);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum AnswerError implements ErrorCode {
NO_EXIST_ANSWER(HttpStatus.NOT_FOUND, "M-002", "존재하지 않는 답변입니다."),
NO_EXIST_QUESTION(HttpStatus.NOT_FOUND, "M-003", "존재하지 않는 질문입니다."),
IMAGE_PROCESSING_ERROR(HttpStatus.NOT_FOUND, "M-004", "이미지 업로드 오류입니다."),
ALREADY_REACTED(HttpStatus.NOT_FOUND, "M-005", "이미 반응하였습니다.");
ALREADY_REACTED(HttpStatus.NOT_FOUND, "M-005", "이미 반응하였습니다."),
ALREADY_ANSWERED_QUESTION(HttpStatus.CONFLICT, "M-006", "이미 답변한 질문입니다.");

private final HttpStatus httpStatus;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,24 @@ public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long membe
Question question = questionRepository.findById(request.getQuestionId())
.orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_QUESTION));

if(question.isAnswered() == true)
throw new BusinessException(AnswerError.ALREADY_ANSWERED_QUESTION); // 이미 답변한 질문이면 예외처리

// Answer 생성
Answer answer = answerMapper.toEntity(request, question, member);
Answer savedAnswer = answerRepository.save(answer);
String imageUrl;
if (!imageFile.isEmpty()) {
try (InputStream inputStream = imageFile.getInputStream()) {
String imageUrl = s3ImageStorageService.uploadFile(member.getId().toString(), answer.getId().toString(), "image", 0, inputStream, imageFile.getSize(), imageFile.getContentType());
imageUrl = s3ImageStorageService.uploadFile(member.getId().toString(), answer.getId().toString(), "image", 0, inputStream, imageFile.getSize(), imageFile.getContentType());
answer.setImageFile(imageUrl);
} catch (IOException e) {
throw new BusinessException(AnswerError.IMAGE_PROCESSING_ERROR);
}
} else{
imageUrl = s3ImageStorageService.getDefaultFileUrl(); // 기본이미지
}
answer.setImageFile(imageUrl);

// ReactionCount 생성
ReactionCount reactionCount = ReactionCount.builder()
Expand All @@ -82,15 +89,6 @@ public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long membe
.build();
reactionCountJpaRepository.save(reactionCount);

// 알림 생성 및 전송
NotificationRequest.create notificationDto = new NotificationRequest.create(
member.getId(),
"귀하의 질문에 새로운 답변이 등록되었습니다!",
question.getContent(),
NotificationRequest.EventType.NEW_ANSWER,
null
);
notificationService.createNotification(notificationDto);
// 질문의 isAnswered 상태를 true로 업데이트
question.setAnswered(true);
questionRepository.save(question);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.web.baebaeBE.domain.categorized.answer.controller;

import com.web.baebaeBE.domain.categorized.answer.controller.api.CategorizedAnswerApi;
import com.web.baebaeBE.domain.categorized.answer.dto.CategorizedAnswerRequest;
import com.web.baebaeBE.domain.categorized.answer.dto.CategorizedAnswerResponse;
import com.web.baebaeBE.domain.categorized.answer.service.CategorizedAnswerService;
import com.web.baebaeBE.global.authorization.annotation.AuthorizationAnswer;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/categorizedAnswer")
public class CategorizedAnswerController implements CategorizedAnswerApi {

private final CategorizedAnswerService categorizedAnswerService;


@GetMapping("{answerId}")
@AuthorizationAnswer
public ResponseEntity<List<CategorizedAnswerResponse.CategoryInformationResponse>> getCategoriesByAnswerId(
@PathVariable Long answerId
) {
return ResponseEntity.ok(categorizedAnswerService.getCategoriesByAnswerId(answerId));
}

@PutMapping("/{answerId}")
@AuthorizationAnswer
public ResponseEntity<Void> updateCategoriesByAnswerId(@PathVariable Long answerId, @RequestBody CategorizedAnswerRequest.CategoryList categoryIds) {
categorizedAnswerService.updateCategoriesByAnswerId(answerId, categoryIds);
return ResponseEntity.noContent().build();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.web.baebaeBE.domain.categorized.answer.controller.api;

import com.web.baebaeBE.domain.categorized.answer.dto.CategorizedAnswerRequest;
import com.web.baebaeBE.domain.categorized.answer.dto.CategorizedAnswerResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Tag(name = "CategorizedAnswer", description = "카테고리 내부의 피드에 관련된 API")
public interface CategorizedAnswerApi {

@Operation(summary = "피드가 속한 카테고리 조회",
description = "Answer ID를 받아 해당 Answer에 연결된 모든 카테고리를 조회합니다.",
security = @SecurityRequirement(name = "bearerAuth")
)
@Parameter(
in = ParameterIn.HEADER,
name = "Authorization", required = true,
schema = @Schema(type = "string"),
description = "Bearer [Access 토큰]")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공")
})
@GetMapping("{answerId}")
ResponseEntity<List<CategorizedAnswerResponse.CategoryInformationResponse>> getCategoriesByAnswerId(
@Parameter(description = "Answer의 ID", required = true) @PathVariable Long answerId
);


@Operation(summary = "피드가 속한 카테고리 수정",
description = "Answer ID와 Category ID 리스트를 받아 피드가 속할 카테고리 정보를 수정합니다.",
security = @SecurityRequirement(name = "bearerAuth")
)
@Parameter(
in = ParameterIn.HEADER,
name = "Authorization", required = true,
schema = @Schema(type = "string"),
description = "Bearer [Access 토큰]")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "수정 성공")
})
@PutMapping("/{answerId}")
public ResponseEntity<Void> updateCategoriesByAnswerId(@PathVariable Long answerId, @RequestBody CategorizedAnswerRequest.CategoryList categoryIds) ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.web.baebaeBE.domain.categorized.answer.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

public class CategorizedAnswerRequest {

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class CategoryList{
private List<Long> categoryIds;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.web.baebaeBE.domain.categorized.answer.dto;

import com.web.baebaeBE.domain.category.dto.CategoryResponse;
import com.web.baebaeBE.domain.category.entity.Category;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;

public class CategorizedAnswerResponse {

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class CategoryInformationResponse {
private Long categoryId;
private String categoryName;
private String categoryImage;

public static CategorizedAnswerResponse.CategoryInformationResponse of(Category category) {
return CategorizedAnswerResponse.CategoryInformationResponse.builder()
.categoryId(category.getId())
.categoryName(category.getCategoryName())
.categoryImage(category.getCategoryImage())
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.web.baebaeBE.domain.category.entity.Category;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.time.LocalDateTime;

Expand All @@ -18,14 +20,17 @@ public class CategorizedAnswer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "categorized_answer_id")
private Long id;

@ManyToOne
@JoinColumn(name = "category_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Category category;

@ManyToOne
@JoinColumn(name = "answer_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Answer answer;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface CategorizedAnswerRepository extends JpaRepository<CategorizedAnswer, Long> {
Page<CategorizedAnswer> findByAnswer_Member_IdAndCategory_Id(Long memberId, Long categoryId, Pageable pageable);
Page<CategorizedAnswer> findByAnswer_Member_Id(Long memberId, Pageable pageable);
}
List<CategorizedAnswer> findAllByAnswerId(Long answerId);
}
Loading

0 comments on commit df9e238

Please sign in to comment.