Skip to content

Commit

Permalink
Merge pull request #127 from ttakkeun/feat/126
Browse files Browse the repository at this point in the history
[Feat] Tip 삭제 API
  • Loading branch information
riadan710 authored Aug 21, 2024
2 parents 9e9bd7b + b676cc7 commit f579b12
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static <T> ApiResponse<T> of(BaseCode code, T result){
return new ApiResponse<>(true, code.getReasonHttpStatus().getCode() , code.getReasonHttpStatus().getMessage(), result);
}

// 응답이 필요없을때
public static ApiResponse<Void> onSuccess() {
return new ApiResponse<>(true, SuccessStatus._OK.getCode(), SuccessStatus._OK.getMessage(), null);
}

// 실패한 경우
public static <T> ApiResponse<T> onFailure(String code, String message, T data) {
return new ApiResponse<>(false, code, message, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public enum ErrorStatus implements BaseErrorCode {
// 투두 에러
TODO_ID_NOT_AVAILABLE(HttpStatus.BAD_REQUEST, "TODO4000", "유효하지 않은 Todo ID입니다."),
TODO_STATUS_IS_DONE(HttpStatus.BAD_REQUEST, "TODO4001", "투두 항목이 이미 완료된 상태입니다."),
TODO_STATUS_IS_ONPROGRESS(HttpStatus.BAD_REQUEST, "TODO4002", "투두 항목이 완료되지 않았습니다.");
TODO_STATUS_IS_ONPROGRESS(HttpStatus.BAD_REQUEST, "TODO4002", "투두 항목이 완료되지 않았습니다."),

// Tip 에러
TIP_ID_NOT_AVAILABLE(HttpStatus.BAD_REQUEST, "TIP4000", "유효하지 않은 Tip ID입니다.");


private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ApiResponse<TipResponseDTO> createTip(
@RequestParam("category") Category category) {

TipCreateRequestDTO request = new TipCreateRequestDTO(title, content, category);
TipResponseDTO result = tipService.createTip(request, member.getMemberId(), null);
TipResponseDTO result = tipService.createTip(request, member.getMemberId());
return ApiResponse.onSuccess(result);
}

Expand Down Expand Up @@ -87,5 +87,14 @@ public ApiResponse<List<TipResponseDTO>> getBestTips(
) {
List<TipResponseDTO> result = tipService.getBestTips(member);
return ApiResponse.onSuccess(result);
}
}

@Operation(summary = "팁 삭제 API")
@DeleteMapping("/{tip_id}")
public ApiResponse<Void> deleteTips(
@PathVariable("tip_id") Long tipId
) {
tipService.deleteTip(tipId);
return ApiResponse.onSuccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface LikeTipRepository extends JpaRepository<LikeTip, Long> {

boolean existsByTipAndMember(Tip tip, Member member);

void deleteAllByTip(Tip tip);

//boolean existsIsLike(Tip tip, Member member);
}
18 changes: 17 additions & 1 deletion src/main/java/ttakkeun/ttakkeun_server/service/TipService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ttakkeun.ttakkeun_server.apiPayLoad.ExceptionHandler;
import ttakkeun.ttakkeun_server.dto.tip.TipCreateRequestDTO;
import ttakkeun.ttakkeun_server.dto.tip.TipResponseDTO;

Expand All @@ -23,6 +24,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static ttakkeun.ttakkeun_server.apiPayLoad.code.status.ErrorStatus.TIP_ID_NOT_AVAILABLE;


@Service
@RequiredArgsConstructor
Expand All @@ -36,7 +39,7 @@ public class TipService {

// 팁 생성
@Transactional
public TipResponseDTO createTip(TipCreateRequestDTO request, Long memberId, List<MultipartFile> imageFiles) {
public TipResponseDTO createTip(TipCreateRequestDTO request, Long memberId) {
Member member = memberService.findMemberById(memberId)
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 사용자 ID입니다."));

Expand Down Expand Up @@ -178,6 +181,19 @@ public List<TipResponseDTO> getBestTips(Member member) {
))
.collect(Collectors.toList());
}

@Transactional
public void deleteTip(Long tipId) {
// 먼저 해당 팁이 존재하는지 확인
Tip tip = tipRepository.findById(tipId)
.orElseThrow(() -> new ExceptionHandler(TIP_ID_NOT_AVAILABLE));

// LikeTip 관련 데이터 삭제
likeTipRepository.deleteAllByTip(tip);

// 팁 삭제
tipRepository.delete(tip);
}
}


Expand Down

0 comments on commit f579b12

Please sign in to comment.