From 158cc9bf1891134ad2689db260e13b7810e10f1e Mon Sep 17 00:00:00 2001 From: minuk926 Date: Fri, 29 Apr 2022 11:40:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20-=20=EC=82=AD=EC=A0=9C=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/ctgy/controller/BoardController.java | 13 +++++++++++++ .../biz/ctgy/repository/IBoardRepository.java | 4 ++++ .../com/xit/biz/ctgy/service/IBoardService.java | 2 ++ .../xit/biz/ctgy/service/impl/BoardService.java | 17 +++++++++++++++++ .../biz/ctgy/service/impl/CtgyFileService.java | 1 - 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/xit/biz/ctgy/controller/BoardController.java b/src/main/java/com/xit/biz/ctgy/controller/BoardController.java index cc43fb8..5fe9aea 100644 --- a/src/main/java/com/xit/biz/ctgy/controller/BoardController.java +++ b/src/main/java/com/xit/biz/ctgy/controller/BoardController.java @@ -4,6 +4,8 @@ import com.xit.biz.ctgy.dto.BoardDto; import com.xit.biz.ctgy.service.IBoardService; import com.xit.core.api.IRestResponse; import com.xit.core.api.RestResponse; +import com.xit.core.util.AssertUtils; +import com.xit.core.util.Checks; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -17,6 +19,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import javax.annotation.Nonnull; + @Tag(name = "BoardController", description = "게시글 관리") @RestController @RequestMapping("/api/v1/ctgy/board") @@ -59,4 +63,13 @@ public class BoardController { service.saveBoard(dto); return RestResponse.of(HttpStatus.OK); } + + @Operation(summary = "게시글 삭제", description = "게시글 삭제") + @PostMapping(value = "/{ciCode}") + public ResponseEntity removeBoard(@PathVariable @Nonnull final Long ciCode) { + AssertUtils.isTrue(!Checks.isEmpty(ciCode), "게시글이 선택되지 않았습니다."); + service.removeBoard(ciCode); + + return RestResponse.of(HttpStatus.OK); + } } diff --git a/src/main/java/com/xit/biz/ctgy/repository/IBoardRepository.java b/src/main/java/com/xit/biz/ctgy/repository/IBoardRepository.java index 8b36488..e050030 100644 --- a/src/main/java/com/xit/biz/ctgy/repository/IBoardRepository.java +++ b/src/main/java/com/xit/biz/ctgy/repository/IBoardRepository.java @@ -5,6 +5,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.util.List; + public interface IBoardRepository extends JpaRepository, IBoardRepositoryCustom { @Query(value = "SELECT max(e.ci_code) + 1 FROM min_civ_board680 e", nativeQuery = true) @@ -13,4 +15,6 @@ public interface IBoardRepository extends JpaRepository, I @Modifying(clearAutomatically = true) @Query("UPDATE MinCivBoard680 m SET m.ciHit = m.ciHit + 1 WHERE m.ciCode = :ciCode") int updateInHitForMinCivBoard680(Long ciCode); + + List findByCiRef(Long ciCode); } diff --git a/src/main/java/com/xit/biz/ctgy/service/IBoardService.java b/src/main/java/com/xit/biz/ctgy/service/IBoardService.java index 5b3c25e..3f432e4 100644 --- a/src/main/java/com/xit/biz/ctgy/service/IBoardService.java +++ b/src/main/java/com/xit/biz/ctgy/service/IBoardService.java @@ -13,4 +13,6 @@ public interface IBoardService { int modifyByCiCode(Long ciCode); void saveBoard(BoardDto dto); + + void removeBoard(Long ciCode); } diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/BoardService.java b/src/main/java/com/xit/biz/ctgy/service/impl/BoardService.java index 86c8aa4..a4f3767 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/BoardService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/BoardService.java @@ -1,5 +1,6 @@ package com.xit.biz.ctgy.service.impl; +import com.google.common.base.Objects; import com.xit.biz.ctgy.dto.BoardDto; import com.xit.biz.ctgy.dto.struct.MinCivBoard680Mapstruct; import com.xit.biz.ctgy.entity.MinCivBoard680; @@ -7,6 +8,7 @@ import com.xit.biz.ctgy.repository.IBoardRepository; import com.xit.biz.ctgy.service.IBoardService; import com.xit.core.constant.ErrorCode; import com.xit.core.exception.CustomBaseException; +import com.xit.core.oauth2.utils.HeaderUtil; import com.xit.core.support.jpa.JpaUtil; import com.xit.core.util.Checks; import com.xit.core.util.CommUtil; @@ -20,6 +22,8 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.contains; @Service @@ -79,4 +83,17 @@ public class BoardService implements IBoardService { } repository.save(entity); } + + @Override + @Transactional + public void removeBoard(Long ciCode) { + + MinCivBoard680 savedEntity = repository.findById(ciCode).orElseThrow(() -> new CustomBaseException(ErrorCode.NOT_FOUND)); + if(!Objects.equal(HeaderUtil.getUserId(), savedEntity.getCiId())) throw new CustomBaseException("삭제 권한(게시글 소유자)이 없는 사용자 입니다"); + + // 댓글 존재 여부 조회 + List list = repository.findByCiRef(ciCode); + if(list.size() > 1) throw new CustomBaseException("삭제 할 수 없는 게시글 입니다[댓글 존재]"); + repository.deleteById(ciCode); + } } diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java index 89fccd4..128c63b 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java @@ -9,7 +9,6 @@ import com.xit.core.constant.ErrorCode; import com.xit.core.exception.CustomBaseException; import com.xit.core.util.AssertUtils; import com.xit.core.util.Checks; -import com.xit.core.util.DateUtil; import io.jsonwebtoken.lang.Assert; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j;