fix: 공지사항 저장 반영

dev
Lim Jonguk 3 years ago
parent ee1aff0a35
commit b9fa2d0fc7

@ -10,6 +10,7 @@ import com.xit.core.util.Checks;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -48,7 +49,7 @@ public class CmmFileMgtController {
} }
@Operation(summary = "파일 저장" , description = "파일 저장") @Operation(summary = "파일 저장" , description = "파일 저장")
@PostMapping(consumes = "multipart/form-data") @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<? extends IRestResponse> saveFiles(@ModelAttribute @Nonnull CmmFileDto cmmFileDto) { public ResponseEntity<? extends IRestResponse> saveFiles(@ModelAttribute @Nonnull CmmFileDto cmmFileDto) {
AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto), "파일 정보가 존재하지 않습니다."); AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto), "파일 정보가 존재하지 않습니다.");
AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFileCtgCd()), "파일 구분 코드[fileCtgCd] 정보가 존재하지 않습니다."); AssertUtils.isTrue(!Checks.isEmpty(cmmFileDto.getFileCtgCd()), "파일 구분 코드[fileCtgCd] 정보가 존재하지 않습니다.");

@ -29,7 +29,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@Tag(name = "CtgyFileMgtController", description = "파일 관리") @Tag(name = "CtgyFileMgtController", description = "공지사항 / 게시판 관리")
@RestController @RestController
@RequiredArgsConstructor @RequiredArgsConstructor
@RequestMapping("/api/v1/ctgy/file") @RequestMapping("/api/v1/ctgy/file")

@ -1,13 +1,16 @@
package com.xit.biz.ctgy.entity; package com.xit.biz.ctgy.entity;
import com.xit.core.oauth2.utils.HeaderUtil;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Schema(name = "MinInfoBoard680", description = "공지사항") @Schema(name = "MinInfoBoard680", description = "공지사항")
@Table(name = "min_info_board680", schema = "", catalog = "") @Table(name = "min_info_board680")
@Entity @Entity
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@ -17,6 +20,7 @@ import java.io.Serializable;
public class MinInfoBoard680 implements Serializable { public class MinInfoBoard680 implements Serializable {
private static final long SerialVersionUID = 1L; private static final long SerialVersionUID = 1L;
//@Query(value = "SELECT max(mib.inCode) + 1 FROM MinInfoBoard680 mib WHERE mib.inBgubun = '1'")
@Id @Id
@Schema(required = true, title = "공지사항코드", example = " ", description = "Input Description...") @Schema(required = true, title = "공지사항코드", example = " ", description = "Input Description...")
@Column(name = "in_code", nullable = false) @Column(name = "in_code", nullable = false)
@ -73,4 +77,22 @@ public class MinInfoBoard680 implements Serializable {
@Schema(required = false, title = "기타", example = " ", description = "Input Description...") @Schema(required = false, title = "기타", example = " ", description = "Input Description...")
@Column(name = "in_etc", nullable = true) @Column(name = "in_etc", nullable = true)
private String inEtc; 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;
}
} }

@ -2,7 +2,12 @@ package com.xit.biz.ctgy.repository;
import com.xit.biz.ctgy.entity.MinInfoBoard680; import com.xit.biz.ctgy.entity.MinInfoBoard680;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface IPublicBoardRepository extends JpaRepository<MinInfoBoard680, Long> { public interface IPublicBoardRepository extends JpaRepository<MinInfoBoard680, Long> {
@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); MinInfoBoard680 findByInCode(final Long inCode);
} }

@ -84,23 +84,23 @@ public class CtgyFileService implements ICtgyFileService {
String orgFileName = ""; String orgFileName = "";
try { try {
orgFileName = StringUtils.cleanPath(Objects.requireNonNull(mf.getOriginalFilename())); 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.setInFilename(orgFileName);
savedEntity.setInFilesize(mf.getSize()); savedEntity.setInFilesize(mf.getSize());
savedEntity.setInFileurl(serviceUrl + urlPath); savedEntity.setInFileurl(serviceUrl + urlPath);
savedEntity.setInBgubun(CtgyConstants.PublicBoard.GUBUN.getCode()); setEntity(savedEntity, entity);
savedEntity.setInDept(entity.getInDept()); JpaUtil.saveIfNullId(entity.getInCode(), repository, savedEntity);
savedEntity.setInTitle(entity.getInTitle());
savedEntity.setInContents(entity.getInContents());
JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity);
entityList.add(savedEntity); entityList.add(savedEntity);
mf.transferTo(new File(fileUploadPath + File.separator + orgFileName)); mf.transferTo(new File(fileUploadPath + File.separator + orgFileName));
// inputStream을 가져와 // inputStream을 가져와
// copyOfLocation (저장위치)로 파일을 쓴다. // copyOfLocation (저장위치)로 파일을 쓴다.
// copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다 // copy의 옵션은 기존에 존재하면 REPLACE(대체한다), 오버라이딩 한다
//Files.copy(multipartFile.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING); //Files.copy(multipartFile.getInputStream(), copyOfLocation, StandardCopyOption.REPLACE_EXISTING);
@ -114,12 +114,14 @@ public class CtgyFileService implements ICtgyFileService {
} }
} }
}else{ }else{
MinInfoBoard680 savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new); MinInfoBoard680 savedEntity = null;
savedEntity.setInBgubun(CtgyConstants.PublicBoard.GUBUN.getCode()); if(Checks.isEmpty(entity.getInCode()))
savedEntity.setInDept(entity.getInDept()); savedEntity = new MinInfoBoard680();
savedEntity.setInTitle(entity.getInTitle()); else
savedEntity.setInContents(entity.getInContents()); savedEntity = repository.findById(entity.getInCode()).orElseGet(MinInfoBoard680::new);
JpaUtil.saveIfNullId(savedEntity.getInCode(), repository, savedEntity);
setEntity(savedEntity, entity);
JpaUtil.saveIfNullId(entity.getInCode(), repository, savedEntity);
} }
return entityList; return entityList;
} }
@ -139,8 +141,22 @@ public class CtgyFileService implements ICtgyFileService {
repository.deleteById(inCode); repository.deleteById(inCode);
if(file.exists()) file.delete(); 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종속적이다. // File.seperator 는 OS종속적이다.
// Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다 // Spring에서 제공하는 cleanPath()를 통해서 ../ 내부 점들에 대해서 사용을 억제한다

@ -26,11 +26,23 @@ public class JpaUtil {
// else return PageRequest.of(limit*(page-1), limit, Objects.requireNonNull(sort)); // else return PageRequest.of(limit*(page-1), limit, Objects.requireNonNull(sort));
} }
/**
*
* @param id Long
* @param repository JpaRepository
* @param entity Object
*/
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static void saveIfNullId(Long id, JpaRepository repository, Object entity) { public static void saveIfNullId(Long id, JpaRepository repository, Object entity) {
if(id == null) repository.save(entity); if(id == null) repository.save(entity);
} }
/**
*
* @param id Long
* @param repository JpaRepository
* @param entity Object
*/
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static void saveIfNullId(String id, JpaRepository repository, Object entity) { public static void saveIfNullId(String id, JpaRepository repository, Object entity) {
if(id == null) repository.save(entity); if(id == null) repository.save(entity);

Loading…
Cancel
Save