feat: v2 Board Service 적용
parent
0a5e2859a4
commit
9984274cdf
@ -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<BoardDto> findAll(final BoardDto dto, Pageable pageable);
|
||||
Page<MinCivBoard680> findAll2(final MinCivBoard680 entity, Pageable pageable);
|
||||
|
||||
int modifyByCiCode(Long ciCode);
|
||||
|
||||
void saveBoard(BoardDto dto);
|
||||
|
||||
void removeBoard(Long ciCode);
|
||||
}
|
@ -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<BoardDto> findAll(final BoardDto dto, Pageable pageable) {
|
||||
pageable = JpaUtil.getPagingInfo(pageable);
|
||||
return repository.findAll(dto, pageable);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<MinCivBoard680> findAll2(final MinCivBoard680 entity, Pageable pageable) {
|
||||
pageable = JpaUtil.getPagingInfo(pageable);
|
||||
ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
|
||||
.withMatcher("ciTitle", contains())
|
||||
.withMatcher("ciName", contains());
|
||||
Example<MinCivBoard680> 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<MinCivBoard680> list = repository.findByCiRef(ciCode);
|
||||
if(list.size() > 1) throw new CustomBaseException("삭제 할 수 없는 게시글 입니다[댓글 존재]");
|
||||
repository.deleteById(ciCode);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue