Skip to content

Commit

Permalink
feat: 백문백답 삭제 API를 구현한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
min429 committed Oct 13, 2024
1 parent e76cef7 commit 181ec87
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
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.accompany.domain.auth.dto.jwt.JwtAuthentication;
import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest;
import com.dnd.accompany.domain.qna100.service.QnaService;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -32,4 +34,13 @@ public ResponseEntity<Void> createAndUpdate(
qnaService.saveAndUpdate(user.getId(), request);
return ResponseEntity.ok().build();
}

@Operation(summary = "백문백답 삭제")
@DeleteMapping
public ResponseEntity<Void> delete(
@RequestBody @Valid DeleteQnaRequest request,
@AuthenticationPrincipal JwtAuthentication user) {
qnaService.delete(user.getId(), request);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dnd.accompany.domain.qna100.api.dto;

import java.util.List;

public record DeleteQnaRequest(
List<Long> ids
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dnd.accompany.domain.qna100.infrastructure;

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

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -8,5 +11,6 @@

@Repository
public interface QnaRepository extends JpaRepository<Qna100, Long>, QnaRepositoryCustom {
void deleteAllByUserId(Long userId);
Optional<Qna100> findFirstByUserId(Long userId);
void deleteByIdIn(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
import java.util.Map;
import java.util.function.Function;

import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest;
import com.dnd.accompany.domain.qna100.entity.Qna100;
import com.dnd.accompany.domain.qna100.exception.Qna100AccessDeniedException;
import com.dnd.accompany.domain.qna100.exception.Qna100NotFoundException;
import com.dnd.accompany.domain.qna100.infrastructure.QnaRepository;
import com.dnd.accompany.global.common.response.ErrorCode;

import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -41,4 +46,16 @@ public void saveAndUpdate(Long userId, CreateAndUpdateQnaRequest request) {

qnaRepository.saveAll(newQnas);
}

@Transactional
public void delete(Long userId, DeleteQnaRequest request) {
Qna100 qna = qnaRepository.findFirstByUserId(userId)
.orElseThrow(() -> new Qna100NotFoundException(ErrorCode.QNA100_NOT_FOUND));

if(!userId.equals(qna.getUserId())){
throw new Qna100AccessDeniedException(ErrorCode.QNA100_ACCESS_DENIED);
}

qnaRepository.deleteByIdIn(request.ids());
}
}

0 comments on commit 181ec87

Please sign in to comment.