Files
noti-hub/src/main/java/com/kri/notihub/server/controller/TemplateController.java

105 lines
3.7 KiB
Java

package com.kri.notihub.server.controller;
import com.kri.common.feature.web.RequestUserContext;
import com.kri.notihub.server.common.enums.ChannelType;
import com.kri.notihub.server.common.enums.SendType;
import com.kri.notihub.server.dto.template.TemplateBasicDto;
import com.kri.notihub.server.dto.template.TemplateDto;
import com.kri.notihub.server.dto.template.TemplateSummaryDto;
import com.kri.notihub.server.service.FileService;
import com.kri.notihub.server.service.TemplateService;
import io.swagger.v3.oas.annotations.Operation;
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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/api/v1/template")
@RequiredArgsConstructor
@Tag(name = "Template", description = "템플릿 API")
public class TemplateController {
private final TemplateService templateService;
private final FileService fileService;
@Operation(summary = "템플릿 목록 조회")
@GetMapping("/list/{chnnlTy}/{itrntPrjctCd}")
public List<TemplateSummaryDto> getTemplateList(
@PathVariable ChannelType chnnlTy,
@PathVariable String itrntPrjctCd
) {
return templateService.getTemplateList(chnnlTy, itrntPrjctCd);
}
@Operation(summary = "템플릿 단건 조회")
@GetMapping("/{tmplatIdx}")
public TemplateDto getTemplate(@PathVariable long tmplatIdx) {
return templateService.getTemplate(tmplatIdx);
}
@Operation(summary = "템플릿 생성")
@PutMapping
public TemplateDto createTemplate(
@Valid @RequestBody TemplateBasicDto template,
RequestUserContext ctx
) {
TemplateDto newTmplat = templateService.createTemplate(template.getItrntPrjctCd(), template,
ctx.getUserId());
if (template.getTmplatTy() == SendType.EMAIL) {
String newHtml = fileService.syncOnSave(
newTmplat.getItrntPrjctCd(),
newTmplat.getTmplatIdx(),
newTmplat.getTmplatBody()
);
newTmplat.setTmplatBody(newHtml);
templateService.updateTemplateBodyWithCreate(newTmplat);
}
return newTmplat;
}
@Operation(summary = "바디에 사용하는 이미지 업로드")
@PutMapping("/body-image/{itrntPrjctCd}")
public String uploadContactSenarioImage(@PathVariable String itrntPrjctCd,
@RequestParam("file") MultipartFile file) {
return fileService.storeTempFile(itrntPrjctCd, file);
}
@Operation(summary = "템플릿 수정")
@PatchMapping
public TemplateDto updateTemplate(
@Valid @RequestBody TemplateDto template,
RequestUserContext ctx
) {
if (template.getTmplatTy() == SendType.EMAIL) {
String newHtml = fileService.syncOnSave(
template.getItrntPrjctCd(),
template.getTmplatIdx(),
template.getTmplatBody()
);
template.setTmplatBody(newHtml);
}
return templateService.updateTemplate(template, ctx.getUserId());
}
@Operation(summary = "템플릿 삭제(소프트 삭제)")
@DeleteMapping("/{tmplatIdx}")
public void deleteTemplate(@PathVariable long tmplatIdx, RequestUserContext ctx) {
templateService.deleteTemplate(tmplatIdx, ctx.getUserId());
}
}