공통코드 코드기반 조회 추가
This commit is contained in:
@@ -19,6 +19,7 @@ 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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Tag(name = "공통코드 관리", description = "공통코드 관리 API")
|
||||
@@ -77,6 +78,7 @@ public class CommonCodeApiController {
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = Long.class))),
|
||||
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
|
||||
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
|
||||
})
|
||||
@PostMapping
|
||||
@@ -96,15 +98,16 @@ public class CommonCodeApiController {
|
||||
@Operation(summary = "수정", description = "공통코드를 수정 합니다.")
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(responseCode = "201", description = "공통코드 수정 성공", content =
|
||||
@ApiResponse(responseCode = "204", description = "공통코드 수정 성공", content =
|
||||
@Content(
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = Long.class))),
|
||||
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
|
||||
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
|
||||
})
|
||||
@PutMapping("/{id}")
|
||||
public ApiResponseDto<Long> update(
|
||||
public ApiResponseDto<Void> update(
|
||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
||||
description = "공통코드 수정 요청 정보",
|
||||
required = true,
|
||||
@@ -113,17 +116,19 @@ public class CommonCodeApiController {
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = CommonCodeDto.ModifyReq.class)))
|
||||
@PathVariable Long id, @RequestBody @Valid CommonCodeDto.ModifyReq req) {
|
||||
return ApiResponseDto.createOK(commonCodeService.update(id, req));
|
||||
commonCodeService.update(id, req);
|
||||
return ApiResponseDto.deleteOk(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "삭제", description = "공통코드를 삭제 합니다.")
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(responseCode = "201", description = "공통코드 삭제 성공", content =
|
||||
@ApiResponse(responseCode = "204", description = "공통코드 삭제 성공", content =
|
||||
@Content(
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = Long.class))),
|
||||
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
|
||||
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
|
||||
})
|
||||
@DeleteMapping("/{id}")
|
||||
@@ -132,7 +137,52 @@ public class CommonCodeApiController {
|
||||
description = "공통코드 삭제 요청 정보",
|
||||
required = true)
|
||||
@PathVariable Long id) {
|
||||
commonCodeService.remove(id);
|
||||
commonCodeService.remove(id);
|
||||
return ApiResponseDto.deleteOk(id);
|
||||
}
|
||||
|
||||
@Operation(summary = "순서 변경", description = "공통코드 순서를 변경 합니다.")
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(responseCode = "204", description = "공통코드 순서 변경 성공", content =
|
||||
@Content(
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = Long.class))),
|
||||
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
|
||||
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
|
||||
})
|
||||
@PutMapping("/order")
|
||||
public ApiResponseDto<Void> updateOrder(
|
||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
||||
description = "공통코드 순서변경 요청 정보",
|
||||
required = true,
|
||||
content =
|
||||
@Content(
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = CommonCodeDto.OrderReq.class)))
|
||||
@RequestBody @Valid CommonCodeDto.OrderReq req) {
|
||||
commonCodeService.updateOrder(req);
|
||||
return ApiResponseDto.deleteOk(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "code 기반 조회", description = "code 기반 조회")
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(responseCode = "200", description = "공통코드 순서 변경 성공", content =
|
||||
@Content(
|
||||
mediaType = "application/json",
|
||||
schema = @Schema(implementation = Long.class))),
|
||||
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
|
||||
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
|
||||
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
|
||||
})
|
||||
@GetMapping("/used")
|
||||
public ApiResponseDto<List<CommonCodeDto.Basic>> getByCode(
|
||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(
|
||||
description = "공통코드 순서변경 요청 정보",
|
||||
required = true)
|
||||
@RequestParam String code) {
|
||||
return ApiResponseDto.ok(commonCodeService.findByCode(code));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.kamco.cd.kamcoback.common.utils.interfaces.JsonFormatDttm;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.PositiveOrZero;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -45,6 +47,27 @@ public class CommonCodeDto {
|
||||
private boolean used;
|
||||
}
|
||||
|
||||
@Schema(name = "CodeOrderReq", description = "공통코드 순서 변경 정보")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class OrderReq {
|
||||
List<OrderReqDetail> orders;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class OrderReqDetail {
|
||||
@NotNull
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private Integer order;
|
||||
}
|
||||
|
||||
@Schema(name = "CommonCode Basic", description = "공통코드 기본 정보")
|
||||
@Getter
|
||||
public static class Basic {
|
||||
|
||||
@@ -53,9 +53,30 @@ public class CommonCodeService {
|
||||
return commonCodeCoreService.update(id, req).getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 공통코드 삭제 처리
|
||||
* @param id 코드 아이디
|
||||
*/
|
||||
@Transactional
|
||||
public void remove(Long id) {
|
||||
commonCodeCoreService.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 공통코드 순서 변경
|
||||
* @param req
|
||||
*/
|
||||
@Transactional
|
||||
public void updateOrder(CommonCodeDto.OrderReq req) {
|
||||
commonCodeCoreService.updateOrder(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드기반 조회
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public List<CommonCodeDto.Basic> findByCode(String code) {
|
||||
return commonCodeCoreService.findByCode(code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ 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 java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -40,13 +40,21 @@ public class CommonCodeCoreService implements BaseCoreService<CommonCodeDto.Basi
|
||||
|
||||
public CommonCodeDto.Basic update(Long id, CommonCodeDto.ModifyReq req) {
|
||||
CommonCodeEntity found = commonCodeRepository.findByCodeId(id)
|
||||
.orElseThrow(()->new NoSuchElementException("common code 를 찾을 수 없습니다. id : " + id));
|
||||
.orElseThrow(()->new EntityNotFoundException("common code 를 찾을 수 없습니다. id : " + id));
|
||||
|
||||
CommonCodeEntity entity = new CommonCodeEntity( id, req.getName(), req.getDescription(), req.getOrder(), req.isUsed(), found.getDeleted());
|
||||
|
||||
return commonCodeRepository.save(entity).toDto();
|
||||
}
|
||||
|
||||
public void updateOrder(CommonCodeDto.OrderReq req) {
|
||||
commonCodeRepository.updateOrder(req);
|
||||
}
|
||||
|
||||
public List<CommonCodeDto.Basic> findByCode(String code) {
|
||||
return commonCodeRepository.findByCode(code).stream().map(CommonCodeEntity::toDto).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Long id) {
|
||||
CommonCodeEntity entity = commonCodeRepository.findByCodeId(id)
|
||||
|
||||
@@ -104,4 +104,8 @@ public class CommonCodeEntity extends CommonDateEntity {
|
||||
public void deleted() {
|
||||
this.deleted = true;
|
||||
}
|
||||
|
||||
public void updateOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.kamco.cd.kamcoback.postgres.repository;
|
||||
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CommonCodeRepositoryCustom {
|
||||
Optional<CommonCodeEntity> findByCodeId(Long id);
|
||||
Optional<CommonCodeEntity> findByCode(String code);
|
||||
List<CommonCodeEntity> findByAll();
|
||||
void updateOrder(CommonCodeDto.OrderReq req);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,19 @@ package com.kamco.cd.kamcoback.postgres.repository;
|
||||
|
||||
import static com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity.commonCodeEntity;
|
||||
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
|
||||
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto.OrderReqDetail;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity;
|
||||
import com.querydsl.core.types.dsl.Expressions;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;
|
||||
|
||||
public class CommonCodeRepositoryImpl extends QuerydslRepositorySupport implements CommonCodeRepositoryCustom {
|
||||
@@ -35,6 +43,25 @@ public class CommonCodeRepositoryImpl extends QuerydslRepositorySupport implemen
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<CommonCodeEntity> findByCode(String code) {
|
||||
QCommonCodeEntity child = new QCommonCodeEntity("child");
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(commonCodeEntity)
|
||||
.leftJoin(commonCodeEntity.children, child)
|
||||
.fetchJoin()
|
||||
.where(
|
||||
commonCodeEntity.parent.isNull(),
|
||||
commonCodeEntity.code.eq(code),
|
||||
commonCodeEntity.used.isTrue(),
|
||||
commonCodeEntity.deleted.isFalse()
|
||||
)
|
||||
.orderBy(child.order.asc())
|
||||
.fetchOne()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CommonCodeEntity> findByAll() {
|
||||
QCommonCodeEntity child = new QCommonCodeEntity("child");
|
||||
@@ -49,4 +76,29 @@ public class CommonCodeRepositoryImpl extends QuerydslRepositorySupport implemen
|
||||
.orderBy(commonCodeEntity.order.asc(), child.order.asc())
|
||||
.fetch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOrder(CommonCodeDto.OrderReq req) {
|
||||
Map<Long, Integer> orderMap = req.getOrders().stream().
|
||||
collect(Collectors.toMap(OrderReqDetail::getId, OrderReqDetail::getOrder));
|
||||
|
||||
List<CommonCodeEntity> entity = findAllByIds(orderMap.keySet());
|
||||
|
||||
entity.forEach(commonCodeEntity -> {
|
||||
Integer order = orderMap.get(commonCodeEntity.getId());
|
||||
if(order != null) {
|
||||
commonCodeEntity.updateOrder(order);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<CommonCodeEntity> findAllByIds(Set<Long> ids) {
|
||||
return queryFactory
|
||||
.selectFrom(commonCodeEntity)
|
||||
.where(
|
||||
commonCodeEntity.id.in(ids),
|
||||
commonCodeEntity.deleted.isFalse()
|
||||
)
|
||||
.fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user