From 9984274cdf90846b9e5f9c7f4a52ff0575a41407 Mon Sep 17 00:00:00 2001 From: donggyu97 Date: Thu, 26 May 2022 15:37:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20v2=20Board=20Service=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ctgy/v2/controller/BoardController.java | 2 +- .../biz/ctgy/v2/service/IBoardService.java | 18 ++++ .../ctgy/v2/service/impl/BoardService.java | 99 +++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java create mode 100644 src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java diff --git a/src/main/java/com/xit/biz/ctgy/v2/controller/BoardController.java b/src/main/java/com/xit/biz/ctgy/v2/controller/BoardController.java index 0dcc7c9..9045e44 100644 --- a/src/main/java/com/xit/biz/ctgy/v2/controller/BoardController.java +++ b/src/main/java/com/xit/biz/ctgy/v2/controller/BoardController.java @@ -1,7 +1,7 @@ package com.xit.biz.ctgy.v2.controller; import com.xit.biz.ctgy.dto.BoardDto; -import com.xit.biz.ctgy.service.IBoardService; +import com.xit.biz.ctgy.v2.service.IBoardService; import com.xit.core.annotation.Secured; import com.xit.core.annotation.SecurityPolicy; import com.xit.core.api.IRestResponse; diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java new file mode 100644 index 0000000..9c62d63 --- /dev/null +++ b/src/main/java/com/xit/biz/ctgy/v2/service/IBoardService.java @@ -0,0 +1,18 @@ +package com.xit.biz.ctgy.v2.service; + +import com.xit.biz.ctgy.dto.BoardDto; +import com.xit.biz.ctgy.entity.MinCivBoard680; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +public interface IBoardService { + + Page findAll(final BoardDto dto, Pageable pageable); + Page findAll2(final MinCivBoard680 entity, Pageable pageable); + + int modifyByCiCode(Long ciCode); + + void saveBoard(BoardDto dto); + + void removeBoard(Long ciCode); +} diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java new file mode 100644 index 0000000..f417951 --- /dev/null +++ b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java @@ -0,0 +1,99 @@ +package com.xit.biz.ctgy.v2.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; +import com.xit.biz.ctgy.repository.IBoardRepository; +import com.xit.biz.ctgy.v2.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; +import lombok.AllArgsConstructor; +import org.mapstruct.factory.Mappers; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +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 +@AllArgsConstructor +public class BoardService implements IBoardService { + + private final IBoardRepository repository; + private final PasswordEncoder passwordEncoder; + private final MinCivBoard680Mapstruct mapstruct = Mappers.getMapper(MinCivBoard680Mapstruct.class); + + @Transactional(readOnly = true) + public Page findAll(final BoardDto dto, Pageable pageable) { + pageable = JpaUtil.getPagingInfo(pageable); + return repository.findAll(dto, pageable); + } + + @Transactional(readOnly = true) + public Page findAll2(final MinCivBoard680 entity, Pageable pageable) { + pageable = JpaUtil.getPagingInfo(pageable); + ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll() + .withMatcher("ciTitle", contains()) + .withMatcher("ciName", contains()); + Example example = Example.of(entity, exampleMatcher); + return repository.findAll(example, pageable); + } + + @Override + @Transactional + public int modifyByCiCode(Long ciCode) { + return repository.updateInHitForMinCivBoard680(ciCode); + } + + @Override + @Transactional + public void saveBoard(BoardDto dto) { + MinCivBoard680 entity = null; + + // update 인 경우 + if(Checks.isNotEmpty(dto.getCiCode())) { + entity = repository.findById(dto.getCiCode()).orElseThrow(() -> new CustomBaseException(ErrorCode.DATA_NOT_FOUND)); + if(!entity.getCiPass().equals(passwordEncoder.encode(dto.getCiPass()))){ + throw new CustomBaseException(ErrorCode.MISMATCH_PASSWORD); + } + entity.setCiTitle(dto.getCiTitle()); + entity.setCiContents(dto.getCiContents()); + }else { + dto.setCiCode(repository.getCiCode()); + dto.setCiContentno(dto.getCiCode()); + if (Checks.isEmpty(dto.getCiRef()) || dto.getCiRef() == 0L) { + dto.setCiRef(dto.getCiCode()); + dto.setCiStep(0L); + dto.setCiRevel(0L); + } + dto.setCiPass(passwordEncoder.encode(dto.getCiPass())); + dto.setCiIp(CommUtil.getDeviceInfo().getIp()); + entity = mapstruct.toEntity(dto); + } + 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); + } +}