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

[refactor #48] 상호작용 API 리팩토링 #49

Merged
merged 4 commits into from
Aug 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -17,14 +16,13 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/question-posts")
public class InteractionController {

private final InteractionService interactionService;

@PostMapping("/{questionPostId}/activated")
@PostMapping("/api/question-posts/{questionPostId}/activated")
public ResponseEntity<InteractionResponse> activateInteraction(
@PathVariable Long questionPostId,
@PathVariable("questionPostId") Long questionPostId,
@RequestParam String type,
@AuthenticationPrincipal Member member
) {
Expand All @@ -36,7 +34,7 @@ public ResponseEntity<InteractionResponse> activateInteraction(
return ResponseEntity.ok(response);
}

@PostMapping("/{questionPostId}/inactivated")
@PostMapping("/api/question-posts/{questionPostId}/inactivated")
public ResponseEntity<InteractionResponse> inactivateInteraction(
@PathVariable("questionPostId") Long questionPostId,
@RequestParam("type") String type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dnd.gongmuin.post_interaction.domain;

import java.util.Objects;

import com.dnd.gongmuin.common.entity.TimeBaseEntity;
import com.dnd.gongmuin.common.exception.runtime.ValidationException;
import com.dnd.gongmuin.post_interaction.exception.InteractionErrorCode;
Expand Down Expand Up @@ -49,17 +51,10 @@ public static Interaction of(InteractionType type, Long memberId, Long questionP
return new Interaction(type, memberId, questionPostId);
}

public void updateIsInteractedTrue() {
if (Boolean.TRUE.equals(isInteracted)) {
public void updateIsInteracted(boolean updateStatus) {
if (Objects.equals(isInteracted, updateStatus)) {
throw new ValidationException(InteractionErrorCode.ALREADY_INTERACTED);
}
isInteracted = true;
}

public void updateIsInteractedFalse() {
if (Boolean.FALSE.equals(isInteracted)) {
throw new ValidationException(InteractionErrorCode.ALREADY_UNINTERACTED);
}
isInteracted = false;
isInteracted = updateStatus;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dnd.gongmuin.post_interaction.domain;

import com.dnd.gongmuin.common.entity.TimeBaseEntity;
import com.dnd.gongmuin.common.exception.runtime.ValidationException;
import com.dnd.gongmuin.post_interaction.exception.InteractionErrorCode;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -48,6 +50,9 @@ public int increaseCount() {
}

public int decreaseCount() {
if (count == 0) {
throw new ValidationException(InteractionErrorCode.UNINTERACTION_NOT_ALLOWED);
}
return --count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public enum InteractionErrorCode implements ErrorCode {

NOT_FOUND_POST_INTERACTION("상호작용 이력이 존재하지 않습니다.", "PI_001"),
ALREADY_INTERACTED("이미 상호작용한 게시글입니다.", "PI_002"),
ALREADY_UNINTERACTED("이미 상호작용 취소한 게시글입니다.", "PI_003"),
ALREADY_INTERACTED("이미 해당 작업을 수행했습니다.", "PI_002"),
UNINTERACTION_NOT_ALLOWED("상호작용 수가 0이하가 될 수 없습니다.", "PI_003"),
INTERACTION_NOT_ALLOWED("본인 게시물은 상호작용할 수 없습니다", "PI_004");

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void validateIfPostExistsAndNotQuestioner(
QuestionPost questionPost = questionPostRepository.findById(questionPostId)
.orElseThrow(() -> new NotFoundException(QuestionPostErrorCode.NOT_FOUND_QUESTION_POST));
if (questionPost.isQuestioner(memberId)) {
throw new ValidationException(InteractionErrorCode.ALREADY_UNINTERACTED);
throw new ValidationException(InteractionErrorCode.INTERACTION_NOT_ALLOWED);
}
}

Expand All @@ -98,10 +98,10 @@ private int updateInteractionAndCount(
InteractionCount interactionCount = getPostInteractionCount(questionPostId, type);

if (isActivate) { //활성화
interaction.updateIsInteractedTrue();
interaction.updateIsInteracted(true);
count = interactionCount.increaseCount();
} else { // 비활성화
interaction.updateIsInteractedFalse();
interaction.updateIsInteracted(false);
count = interactionCount.decreaseCount();
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnd.gongmuin.common.dto.PageResponse;
Expand All @@ -29,14 +28,13 @@
@Tag(name = "질문글 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/question-posts")
public class QuestionPostController {

private final QuestionPostService questionPostService;

@Operation(summary = "질문글 등록 API", description = "질문글을 등록한다")
@ApiResponse(useReturnTypeSchema = true)
@PostMapping
@PostMapping("/api/question-posts")
public ResponseEntity<RegisterQuestionPostResponse> registerQuestionPost(
@Valid @RequestBody RegisterQuestionPostRequest request,
@AuthenticationPrincipal Member member
Expand All @@ -47,7 +45,7 @@ public ResponseEntity<RegisterQuestionPostResponse> registerQuestionPost(

@Operation(summary = "질문글 상세 조회 API", description = "질문글을 아이디로 상세조회한다.")
@ApiResponse(useReturnTypeSchema = true)
@GetMapping("/{questionPostId}")
@GetMapping("/api/question-posts/{questionPostId}")
public ResponseEntity<QuestionPostDetailResponse> getQuestionPostById(
@PathVariable("questionPostId") Long questionPostId
) {
Expand All @@ -57,7 +55,7 @@ public ResponseEntity<QuestionPostDetailResponse> getQuestionPostById(

@Operation(summary = "질문글 검색 API", description = "질문글을 키워드로 검색하고 정렬, 필터링을 한다.")
@ApiResponse(useReturnTypeSchema = true)
@GetMapping("/search")
@GetMapping("/api/question-posts/search")
public ResponseEntity<PageResponse<QuestionPostSimpleResponse>> searchQuestionPost(
@Valid @ModelAttribute QuestionPostSearchCondition condition,
Pageable pageable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnd.gongmuin.s3.dto.ImagesUploadRequest;
Expand All @@ -23,14 +22,13 @@
@Tag(name = "S3 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/files")
public class S3Controller {

private final S3Service s3Service;

@Operation(summary = "이미지 등록 API", description = "1~10장의 이미지를 등록한다.")
@ApiResponse(useReturnTypeSchema = true)
@PostMapping("/images")
@PostMapping("/api/files/images")
public ResponseEntity<ImagesUploadResponse> uploadImages(
@ModelAttribute @Valid ImagesUploadRequest request
) {
Expand All @@ -40,7 +38,7 @@ public ResponseEntity<ImagesUploadResponse> uploadImages(

@Operation(summary = "동영상 등록 API", description = "최대 45MB의 동영상을 등록한다.")
@ApiResponse(useReturnTypeSchema = true)
@PostMapping("/videos")
@PostMapping("/api/files/videos")
public ResponseEntity<VideoUploadResponse> uploadVideo(
@ModelAttribute @Valid VideoUploadRequest request
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void activateInteraction_update() {
questionPost.getId());
InteractionCount interactionCount = InteractionCountFixture.interactionCount(type,
interactor.getId());
interaction.updateIsInteractedFalse();
interaction.updateIsInteracted(false);
interactionCount.decreaseCount();

given(interactionRepository.existsByQuestionPostIdAndMemberIdAndType(
Expand Down
Loading