diff --git a/src/main/java/com/xit/biz/cmm/controller/CmmFileMgtController.java b/src/main/java/com/xit/biz/cmm/controller/CmmFileMgtController.java index 6ddb7a5..d59018b 100644 --- a/src/main/java/com/xit/biz/cmm/controller/CmmFileMgtController.java +++ b/src/main/java/com/xit/biz/cmm/controller/CmmFileMgtController.java @@ -10,6 +10,7 @@ import com.xit.core.util.Checks; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.lang.NonNull; import org.springframework.web.bind.annotation.*; @@ -48,7 +49,7 @@ public class CmmFileMgtController { } @Operation(summary = "파일 저장" , description = "파일 저장") - @PostMapping(consumes = "multipart/form-data") + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity saveFiles(@ModelAttribute @Nonnull CmmFileDto cmmFileDto) { AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto), "파일 정보가 존재하지 않습니다."); AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFileCtgCd()), "파일 구분 코드[fileCtgCd] 정보가 존재하지 않습니다."); diff --git a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java index 06fda6b..f7f2c44 100644 --- a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java +++ b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java @@ -29,7 +29,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; -@Tag(name = "CtgyFileMgtController", description = "파일 관리") +@Tag(name = "CtgyFileMgtController", description = "공지사항 / 게시판 관리") @RestController @RequiredArgsConstructor @RequestMapping("/api/v1/ctgy/file") diff --git a/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java b/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java index b204cf1..025ec9a 100644 --- a/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java +++ b/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java @@ -1,13 +1,16 @@ package com.xit.biz.ctgy.entity; +import com.xit.core.oauth2.utils.HeaderUtil; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; import javax.persistence.*; import java.io.Serializable; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; @Schema(name = "MinInfoBoard680", description = "공지사항") -@Table(name = "min_info_board680", schema = "", catalog = "") +@Table(name = "min_info_board680") @Entity @NoArgsConstructor @AllArgsConstructor @@ -17,6 +20,7 @@ import java.io.Serializable; public class MinInfoBoard680 implements Serializable { private static final long SerialVersionUID = 1L; + //@Query(value = "SELECT max(mib.inCode) + 1 FROM MinInfoBoard680 mib WHERE mib.inBgubun = '1'") @Id @Schema(required = true, title = "공지사항코드", example = " ", description = "Input Description...") @Column(name = "in_code", nullable = false) @@ -73,4 +77,22 @@ public class MinInfoBoard680 implements Serializable { @Schema(required = false, title = "기타", example = " ", description = "Input Description...") @Column(name = "in_etc", nullable = true) private String inEtc; + + @PrePersist + public void onPrePersist(){ + LocalDateTime localDateTime = LocalDateTime.now(); + this.inNalja = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + this.inTime = localDateTime.format(DateTimeFormatter.ofPattern("HH:mm")); + this.inName = HeaderUtil.getUserId(); + this.inHit = 0L; + } + + @PreUpdate + public void onPreUpdate(){ + LocalDateTime localDateTime = LocalDateTime.now(); + this.inNalja = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + this.inTime = localDateTime.format(DateTimeFormatter.ofPattern("HH:mm")); + this.inName = HeaderUtil.getUserId(); +// this.inHit = this.inHit + 1; + } } diff --git a/src/main/java/com/xit/biz/ctgy/repository/IPublicBoardRepository.java b/src/main/java/com/xit/biz/ctgy/repository/IPublicBoardRepository.java index 52f660a..b8adbda 100644 --- a/src/main/java/com/xit/biz/ctgy/repository/IPublicBoardRepository.java +++ b/src/main/java/com/xit/biz/ctgy/repository/IPublicBoardRepository.java @@ -2,7 +2,12 @@ package com.xit.biz.ctgy.repository; import com.xit.biz.ctgy.entity.MinInfoBoard680; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface IPublicBoardRepository extends JpaRepository { + + @Query(value = "SELECT max(e.in_code) + 1 FROM min_info_board680 e WHERE e.in_bgubun = '1'", nativeQuery = true) + Long getInCodeByInBgubun(); + MinInfoBoard680 findByInCode(final Long inCode); } diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java index c072571..6b19375 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java @@ -84,23 +84,23 @@ public class CtgyFileService implements ICtgyFileService { String orgFileName = ""; try { orgFileName = StringUtils.cleanPath(Objects.requireNonNull(mf.getOriginalFilename())); + MinInfoBoard680 savedEntity = null; // 파일 저장 && 전송 - MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); + if(Checks.isEmpty(entity.getInCode())) + savedEntity = new MinInfoBoard680(); + else + savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); savedEntity.setInFilename(orgFileName); savedEntity.setInFilesize(mf.getSize()); savedEntity.setInFileurl(serviceUrl + urlPath); - savedEntity.setInBgubun(CtgyConstants.PublicBoard.GUBUN.getCode()); - savedEntity.setInDept(entity.getInDept()); - savedEntity.setInTitle(entity.getInTitle()); - savedEntity.setInContents(entity.getInContents()); - - JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity); + setEntity(savedEntity, entity); + JpaUtil.saveIfNullId(entity.getInCode(), repository, savedEntity); entityList.add(savedEntity); mf.transferTo(new File(fileUploadPath + File.separator + orgFileName)); - // inputStream을 가져와서 + // inputStream을 가져와 // copyOfLocation (저장위치)로 파일을 쓴다. // copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다 //Files.copy(multipartFile.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING); @@ -114,12 +114,14 @@ public class CtgyFileService implements ICtgyFileService { } } }else{ - MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); - savedEntity.setInBgubun(CtgyConstants.PublicBoard.GUBUN.getCode()); - savedEntity.setInDept(entity.getInDept()); - savedEntity.setInTitle(entity.getInTitle()); - savedEntity.setInContents(entity.getInContents()); - JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity); + MinInfoBoard680 savedEntity = null; + if(Checks.isEmpty(entity.getInCode())) + savedEntity = new MinInfoBoard680(); + else + savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); + + setEntity(savedEntity, entity); + JpaUtil.saveIfNullId(entity.getInCode(), repository, savedEntity); } return entityList; } @@ -139,8 +141,22 @@ public class CtgyFileService implements ICtgyFileService { repository.deleteById(inCode); if(file.exists()) file.delete(); } + + private void setEntity(MinInfoBoard680 savedEntity, MinInfoBoard680 entity){ + savedEntity.setInBgubun(CtgyConstants.PublicBoard.GUBUN.getCode()); + savedEntity.setInDept(entity.getInDept()); + savedEntity.setInTitle(entity.getInTitle()); + savedEntity.setInContents(entity.getInContents()); + + if(Checks.isEmpty(entity.getInCode())) { + savedEntity.setInCode(repository.getInCodeByInBgubun()); + savedEntity.setInContentno(savedEntity.getInCode()); + } + } } + + /* // File.seperator 는 OS종속적이다. // Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다 diff --git a/src/main/java/com/xit/core/support/jpa/JpaUtil.java b/src/main/java/com/xit/core/support/jpa/JpaUtil.java index 0e2b1dd..2b70a88 100644 --- a/src/main/java/com/xit/core/support/jpa/JpaUtil.java +++ b/src/main/java/com/xit/core/support/jpa/JpaUtil.java @@ -26,11 +26,23 @@ public class JpaUtil { // else return PageRequest.of(limit*(page-1), limit, Objects.requireNonNull(sort)); } + /** + * 신규인 경우 저장 처리 + * @param id Long + * @param repository JpaRepository + * @param entity Object + */ @SuppressWarnings("rawtypes") public static void saveIfNullId(Long id, JpaRepository repository, Object entity) { if(id == null) repository.save(entity); } + /** + * 신규인 경우 저장 처리 + * @param id Long + * @param repository JpaRepository + * @param entity Object + */ @SuppressWarnings("rawtypes") public static void saveIfNullId(String id, JpaRepository repository, Object entity) { if(id == null) repository.save(entity);