공통코드 기능 수정
This commit is contained in:
@@ -3,24 +3,27 @@ package com.kamco.cd.kamcoback.postgres.core;
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto.Basic;
|
||||
import com.kamco.cd.kamcoback.common.service.BaseCoreService;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.AnimalEntity;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.ZooEntity;
|
||||
import com.kamco.cd.kamcoback.postgres.repository.CommonCodeRepository;
|
||||
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.SearchReq;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class CommonCodeCoreService implements BaseCoreService<CommonCodeDto.Basic, Long, SearchReq> {
|
||||
public class CommonCodeCoreService implements BaseCoreService<CommonCodeDto.Basic, Long, SearchReq> {
|
||||
|
||||
private final CommonCodeRepository commonCodeRepository;
|
||||
|
||||
|
||||
public List<CommonCodeDto.Basic> findAll() {
|
||||
return commonCodeRepository.findByAll().stream().map(CommonCodeEntity::toDto).toList();
|
||||
}
|
||||
|
||||
public CommonCodeDto.Basic save(CommonCodeDto.AddReq req) {
|
||||
if(req.getParentId() != null){
|
||||
CommonCodeEntity parentCommonCodeEntity = commonCodeRepository.findById(req.getParentId())
|
||||
@@ -32,18 +35,34 @@ public class CommonCodeCoreService implements BaseCoreService<CommonCodeDto.Bas
|
||||
}
|
||||
|
||||
CommonCodeEntity entity = new CommonCodeEntity(req.getCode(), req.getName(), req.getDescription(), req.getOrder(), req.isUsed());
|
||||
CommonCodeEntity saved = commonCodeRepository.save(entity);
|
||||
return saved.toDto();
|
||||
return commonCodeRepository.save(entity).toDto();
|
||||
}
|
||||
|
||||
public CommonCodeDto.Basic update(Long id, CommonCodeDto.ModifyReq req) {
|
||||
CommonCodeEntity found = commonCodeRepository.findByCodeId(id)
|
||||
.orElseThrow(()->new NoSuchElementException("common code 를 찾을 수 없습니다. id : " + id));
|
||||
|
||||
CommonCodeEntity entity = new CommonCodeEntity( id, req.getName(), req.getDescription(), req.getOrder(), req.isUsed(), found.getDeleted());
|
||||
|
||||
return commonCodeRepository.save(entity).toDto();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Long aLong) {
|
||||
public void remove(Long id) {
|
||||
CommonCodeEntity entity = commonCodeRepository.findByCodeId(id)
|
||||
.orElseThrow(()->new EntityNotFoundException("code를 찾을 수 없습니다. id " + id));
|
||||
|
||||
// 하위 코드 deleted = false 업데이트
|
||||
entity.getChildren().forEach(CommonCodeEntity::deleted);
|
||||
// id 코드 deleted = false 업데이트
|
||||
entity.deleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Basic getOneById(Long aLong) {
|
||||
return null;
|
||||
public Basic getOneById(Long id) {
|
||||
CommonCodeEntity entity = commonCodeRepository.findByCodeId(id)
|
||||
.orElseThrow(()->new EntityNotFoundException("code를 찾을 수 없습니다. id " + id));
|
||||
return entity.toDto();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.kamco.cd.kamcoback.postgres.entity;
|
||||
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
|
||||
import com.kamco.cd.kamcoback.postgres.CommonDateEntity;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
@@ -14,9 +15,8 @@ import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -29,23 +29,23 @@ public class CommonCodeEntity extends CommonDateEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "code_id", nullable = false)
|
||||
@Column(name = "code_id", nullable = false, updatable = false)
|
||||
private Long id;
|
||||
|
||||
@Size(max = 255)
|
||||
@Column(name = "code_cd")
|
||||
private String codeCd;
|
||||
@Column(name = "code_cd", updatable = false)
|
||||
private String code;
|
||||
|
||||
@Size(max = 255)
|
||||
@Column(name = "cd_ct")
|
||||
private String cdCt;
|
||||
private String description;
|
||||
|
||||
@Size(max = 255)
|
||||
@Column(name = "cd_nm")
|
||||
private String cdNm;
|
||||
private String name;
|
||||
|
||||
@Column(name = "cd_odr")
|
||||
private Integer cdOdr;
|
||||
private Integer order;
|
||||
|
||||
@Column(name = "used")
|
||||
private Boolean used;
|
||||
@@ -55,35 +55,53 @@ public class CommonCodeEntity extends CommonDateEntity {
|
||||
private Boolean deleted = false;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "parent_id")
|
||||
@JoinColumn(name = "parent_id", updatable = false)
|
||||
private CommonCodeEntity parent;
|
||||
|
||||
@OneToMany(mappedBy = "parent")
|
||||
private Set<CommonCodeEntity> tbCmCds = new LinkedHashSet<>();
|
||||
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private List<CommonCodeEntity> children = new ArrayList<>();
|
||||
|
||||
public CommonCodeEntity(String codeCd, String cdNm, String cdCt, Integer cdOdr, Boolean used) {
|
||||
this.codeCd = codeCd;
|
||||
this.cdNm = cdNm;
|
||||
this.cdCt = cdCt;
|
||||
this.cdOdr = cdOdr;
|
||||
public CommonCodeEntity(String code, String name, String description, Integer order, Boolean used) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.order = order;
|
||||
this.used = used;
|
||||
}
|
||||
|
||||
public CommonCodeEntity(Long id, String name, String description, Integer order, Boolean used, Boolean deleted) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.order = order;
|
||||
this.used = used;
|
||||
this.deleted = deleted;
|
||||
}
|
||||
|
||||
public CommonCodeDto.Basic toDto() {
|
||||
return new CommonCodeDto.Basic(
|
||||
this.id,
|
||||
this.codeCd,
|
||||
this.cdCt,
|
||||
this.cdNm,
|
||||
this.cdOdr,
|
||||
this.code,
|
||||
this.description,
|
||||
this.name,
|
||||
this.order,
|
||||
this.used,
|
||||
this.deleted,
|
||||
this.parent,
|
||||
this.children.stream().map(CommonCodeEntity::toDto).toList(),
|
||||
super.getCreatedDate(),
|
||||
super.getModifiedDate());
|
||||
super.getModifiedDate());
|
||||
}
|
||||
|
||||
|
||||
public void addParent(CommonCodeEntity parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public boolean isDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public void deleted() {
|
||||
this.deleted = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.kamco.cd.kamcoback.postgres.repository;
|
||||
|
||||
public interface CommonCodeRepositoryCustom {
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CommonCodeRepositoryCustom {
|
||||
Optional<CommonCodeEntity> findByCodeId(Long id);
|
||||
List<CommonCodeEntity> findByAll();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,52 @@
|
||||
package com.kamco.cd.kamcoback.postgres.repository;
|
||||
|
||||
public class CommonCodeRepositoryImpl {
|
||||
import static com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity.commonCodeEntity;
|
||||
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
|
||||
|
||||
public class CommonCodeRepositoryImpl extends QuerydslRepositorySupport implements CommonCodeRepositoryCustom {
|
||||
|
||||
private final JPAQueryFactory queryFactory;
|
||||
|
||||
public CommonCodeRepositoryImpl(JPAQueryFactory queryFactory) {
|
||||
super(CommonCodeEntity.class);
|
||||
this.queryFactory = queryFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<CommonCodeEntity> findByCodeId(Long id) {
|
||||
QCommonCodeEntity child = new QCommonCodeEntity("child");
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(commonCodeEntity)
|
||||
.leftJoin(commonCodeEntity.children, child)
|
||||
.fetchJoin()
|
||||
.where(
|
||||
commonCodeEntity.id.eq(id),
|
||||
commonCodeEntity.deleted.isFalse()
|
||||
)
|
||||
.orderBy(commonCodeEntity.order.asc(), child.order.asc())
|
||||
.fetchOne()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CommonCodeEntity> findByAll() {
|
||||
QCommonCodeEntity child = new QCommonCodeEntity("child");
|
||||
return queryFactory
|
||||
.selectFrom(commonCodeEntity)
|
||||
.leftJoin(commonCodeEntity.children, child)
|
||||
.fetchJoin()
|
||||
.where(
|
||||
commonCodeEntity.parent.isNull(),
|
||||
commonCodeEntity.deleted.isFalse()
|
||||
)
|
||||
.orderBy(commonCodeEntity.order.asc(), child.order.asc())
|
||||
.fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user