공통코드 기능 수정

This commit is contained in:
2025-11-19 10:09:06 +09:00
parent a63b4c33f3
commit 3053b0552e
8 changed files with 288 additions and 64 deletions

View File

@@ -10,8 +10,13 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -24,13 +29,53 @@ public class CommonCodeApiController {
private final CommonCodeService commonCodeService;
@Operation(summary = "목록 조회", description = "모든 공통코드 조회")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "조회 성공",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = CommonCodeDto.Basic.class))),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@GetMapping
public ApiResponseDto<List<CommonCodeDto.Basic>> getFindAll() {
return ApiResponseDto.createOK(commonCodeService.getFindAll());
}
@Operation(summary = "단건 조회", description = "단건 조회")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "조회 성공",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = CommonCodeDto.Basic.class))),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@GetMapping("/{id}")
public ApiResponseDto<CommonCodeDto.Basic> getOneById(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "단건 조회",
required = true)
@PathVariable Long id) {
return ApiResponseDto.ok(commonCodeService.getOneById(id));
}
@Operation(summary = "저장", description = "공통코드를 저장 합니다.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "201", description = "공통코드 저장 성공", content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@@ -45,7 +90,49 @@ public class CommonCodeApiController {
schema = @Schema(implementation = CommonCodeDto.AddReq.class)))
@RequestBody
@Valid CommonCodeDto.AddReq req) {
Long id = commonCodeService.save(req);
return ApiResponseDto.createOK(id);
return ApiResponseDto.createOK(commonCodeService.save(req));
}
@Operation(summary = "수정", description = "공통코드를 수정 합니다.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "201", description = "공통코드 수정 성공", content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@PutMapping("/{id}")
public ApiResponseDto<Long> update(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "공통코드 수정 요청 정보",
required = true,
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = CommonCodeDto.ModifyReq.class)))
@PathVariable Long id, @RequestBody @Valid CommonCodeDto.ModifyReq req) {
return ApiResponseDto.createOK(commonCodeService.update(id, req));
}
@Operation(summary = "삭제", description = "공통코드를 삭제 합니다.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "201", description = "공통코드 삭제 성공", content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@DeleteMapping("/{id}")
public ApiResponseDto<Long> remove(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "공통코드 삭제 요청 정보",
required = true)
@PathVariable Long id) {
commonCodeService.remove(id);
return ApiResponseDto.deleteOk(id);
}
}

View File

@@ -1,15 +1,12 @@
package com.kamco.cd.kamcoback.code.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kamco.cd.kamcoback.common.utils.interfaces.JsonFormatDttm;
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.Category;
import com.kamco.cd.kamcoback.zoo.dto.AnimalDto.Species;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -18,7 +15,7 @@ import lombok.Setter;
public class CommonCodeDto {
@Schema(name = "CodeAddReq", description = "코드저장 요청")
@Schema(name = "CodeAddReq", description = "공통코드 저장 정보")
@Getter
@Setter
@NoArgsConstructor
@@ -35,19 +32,31 @@ public class CommonCodeDto {
private Long parentId;
}
@Schema(name = "AnimalBasic", description = "동물 기본 정보")
@Schema(name = "CodeModifyReq", description = "공통코드 수정 정보")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class ModifyReq {
@NotEmpty
private String name;
private String description;
private int order;
private boolean used;
}
@Schema(name = "CommonCode Basic", description = "공통코드 기본 정보")
@Getter
public static class Basic {
@JsonIgnore
private Long id;
private String codeCd;
private String cdCt;
private String cdNm;
private Integer cdOdr;
private String code;
private String description;
private String name;
private Integer order;
private Boolean used;
private Boolean deleted;
CommonCodeEntity parent;
private List<CommonCodeDto.Basic> children;
@JsonFormatDttm
private ZonedDateTime createdDttm;
@@ -57,23 +66,23 @@ public class CommonCodeDto {
public Basic(
Long id,
String codeCd,
String cdCt,
String cdNm,
Integer cdOdr,
String code,
String description,
String name,
Integer order,
Boolean used,
Boolean deleted,
CommonCodeEntity parent,
List<CommonCodeDto.Basic> children,
ZonedDateTime createdDttm,
ZonedDateTime updatedDttm) {
this.id = id;
this.codeCd = codeCd;
this.cdCt = cdCt;
this.cdNm = cdNm;
this.cdOdr = cdOdr;
this.code = code;
this.description = description;
this.name = name;
this.order = order;
this.used = used;
this.deleted = deleted;
this.parent = parent;
this.children = children;
this.createdDttm = createdDttm;
this.updatedDttm = updatedDttm;
}

View File

@@ -2,6 +2,7 @@ package com.kamco.cd.kamcoback.code.service;
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
import com.kamco.cd.kamcoback.postgres.core.CommonCodeCoreService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -13,6 +14,24 @@ public class CommonCodeService {
private final CommonCodeCoreService commonCodeCoreService;
/**
* 공통코드 목록 조회
* @return
*/
public List<CommonCodeDto.Basic> getFindAll() {
return commonCodeCoreService.findAll();
}
/**
* 공통코드 단건 조회
* @param id
* @return
*/
public CommonCodeDto.Basic getOneById(Long id) {
return commonCodeCoreService.getOneById(id);
}
/**
* 공통코드 생성 요청
* @param req 생성요청 정보
@@ -20,7 +39,23 @@ public class CommonCodeService {
*/
@Transactional
public Long save(CommonCodeDto.AddReq req) {
Long id = commonCodeCoreService.save(req).getId();
return id;
return commonCodeCoreService.save(req).getId();
}
/**
* 공통코드 수정 요청
* @param id 코드 아이디
* @param req 수정요청 정보
* @return
*/
@Transactional
public Long update(Long id, CommonCodeDto.ModifyReq req) {
return commonCodeCoreService.update(id, req).getId();
}
@Transactional
public void remove(Long id) {
commonCodeCoreService.remove(id);
}
}

View File

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

View File

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

View File

@@ -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();
}

View File

@@ -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();
}
}