feat: 게시판 관리 - 삭제 반영

dev
minuk926 3 years ago
parent 002057a0fb
commit 158cc9bf18

@ -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<? extends IRestResponse> removeBoard(@PathVariable @Nonnull final Long ciCode) {
AssertUtils.isTrue(!Checks.isEmpty(ciCode), "게시글이 선택되지 않았습니다.");
service.removeBoard(ciCode);
return RestResponse.of(HttpStatus.OK);
}
}

@ -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<MinCivBoard680, Long>, 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<MinCivBoard680, Long>, I
@Modifying(clearAutomatically = true)
@Query("UPDATE MinCivBoard680 m SET m.ciHit = m.ciHit + 1 WHERE m.ciCode = :ciCode")
int updateInHitForMinCivBoard680(Long ciCode);
List<MinCivBoard680> findByCiRef(Long ciCode);
}

@ -13,4 +13,6 @@ public interface IBoardService {
int modifyByCiCode(Long ciCode);
void saveBoard(BoardDto dto);
void removeBoard(Long ciCode);
}

@ -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<MinCivBoard680> list = repository.findByCiRef(ciCode);
if(list.size() > 1) throw new CustomBaseException("삭제 할 수 없는 게시글 입니다[댓글 존재]");
repository.deleteById(ciCode);
}
}

@ -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;

Loading…
Cancel
Save