저번에 줄기차게 vo, dto에만 시간을 날려버렸다. 후다닥 신청 endpoint 완성해보자.
저번에 만들었던 upload 메서드에 아래와 같은 이슈가 있어서 수정했다.
public String saveFile(MultipartFile multipartFile, String folderPath) throws IOException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(multipartFile.getSize());
metadata.setContentType(multipartFile.getContentType());
String tempFileName = multipartFile.getOriginalFilename();
if (tempFileName.length() > 110) {
tempFileName = "..." + tempFileName.substring(tempFileName.length() - 110);
}
//동일파일명 삭제 방지
String imgUrl = folderPath + "/" + System.currentTimeMillis() + "_" + tempFileName;
amazonS3.putObject(bucket, imgUrl, multipartFile.getInputStream(), metadata);
return amazonS3.getUrl(bucket, imgUrl).toString();
}
포인트
System.currentTimeMillis()
결론 : JpaRepository는 CrudRepository의 기능도 포함하기 때문에 더 낫다.
CmsApplyRepository.java
@Repository
public interface CmsApplyRepository extends JpaRepository<CmsApplyDto, String> {
}
CmsApplyImgRepository.java
@Repository
public interface CmsApplyImgRepository extends JpaRepository<CmsApplyImgDto, String> {
}
CmsApplyService.java
import com.cms.world.domain.dto.CmsApplyDto;
import com.cms.world.domain.dto.CmsApplyImgDto;
import com.cms.world.domain.vo.CmsApplyVo;
import com.cms.world.repository.CmsApplyImgRepository;
import com.cms.world.repository.CmsApplyRepository;
import com.cms.world.utils.GlobalStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Service
public class CmsApplyService {
private final CmsApplyRepository repository;
private final CmsApplyImgRepository imgRepository;
private final S3UploadService uploadService;
public CmsApplyService(CmsApplyRepository repository, CmsApplyImgRepository imgRepository, S3UploadService uploadService) {
this.repository = repository;
this.imgRepository = imgRepository;
this.uploadService = uploadService;
}
@Transactional
public int insert(CmsApplyVo vo) {
try {
CmsApplyDto dto = CmsApplyDto.builder()
.cms_tp(vo.getCmsType())
.content(vo.getContent())
.user_name(vo.getUserName())
.bank_owner(vo.getBnkOwner())
.build();
repository.save(dto);
for (MultipartFile img : vo.getImgList()) {
String awsUrl = uploadService.saveFile(img, "apply");
CmsApplyImgDto imgDto = CmsApplyImgDto.builder().applyDto(dto).img_url(awsUrl).build();
imgRepository.save(imgDto);
}
return GlobalStatus.EXECUTE_SUCCESS.getStatus();
} catch (IOException e) {
return GlobalStatus.EXECUTE_FAILED.getStatus();
}
}
}
CmsApplyController.java
import com.cms.world.domain.vo.CmsApplyVo;
import com.cms.world.service.CmsApplyService;
import com.cms.world.utils.GlobalStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/apply")
public class CmsApplyController {
private CmsApplyService service;
public CmsApplyController (CmsApplyService service){
this.service = service;
}
@PostMapping("/form")
public Map<Integer, String> submit (CmsApplyVo vo) {
Map<Integer, String> map = new HashMap<>();
if (service.insert(vo) == GlobalStatus.EXECUTE_SUCCESS.getStatus()) {
map.put(GlobalStatus.SUCCESS.getStatus(), GlobalStatus.SUCCESS.getMsg());
} else {
map.put(GlobalStatus.INTERNAL_SERVER_ERR.getStatus(), GlobalStatus.INTERNAL_SERVER_ERR.getMsg());
}
return map;
}
}
ResponseEntity를 쓰려다가 프론트 단에 상태 코드랑 메세지를 같이 던지는 게 처리가 나아서 Map으로 던졌다.
성공, 실패 여부는 아래 추가하고
GlobalStatus.java
...
EXECUTE_SUCCESS(1, "실행 성공"),
EXECUTE_FAILED(0, "실행 실패");
CmsApplyVo
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Getter
@Setter
@ToString
public class CmsApplyVo {
private String cmsType;
private String content;
private String userName;
private String bnkOwner;
private List<MultipartFile> imgList;
}
/%2F%2Fapply
식으로 저장되었음.위와 같이 테스트했고, 정상 동작을 확인했다.