Skip to content

Commit

Permalink
[~] #64 TemplateService의 메서드가 ServiceResult 반환하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
woody35545 committed Oct 11, 2023
1 parent 56970e4 commit a5e3e63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Response_Deprecated<TemplateDto> apiGetAllTemplates(){
@GetMapping("/templates/{memberId}")
public Response_Deprecated<DtoList<TemplateDto>> apiGetAllMemberTemplates(@PathVariable("memberId")String memberId){
ResponseStatus responseStatus = ResponseStatus.OK;
DtoList<TemplateDto> allMemberTemplates = templateService.getAllMemberTemplates(memberId);
DtoList<TemplateDto> allMemberTemplates = templateService.getAllTemplatesByMember(memberId);
return new ResponseDto<DtoList<TemplateDto>>(responseStatus,allMemberTemplates).toResponse();
}

Expand Down
59 changes: 32 additions & 27 deletions src/main/java/com/kns/tenquest/service/TemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.kns.tenquest.DtoList;
import com.kns.tenquest.RequestWrapper.TemplateWrapper;
import com.kns.tenquest.dto.MemberDto;
import com.kns.tenquest.dto.QuestionDto;
import com.kns.tenquest.dto.TemplateDocDto;
import com.kns.tenquest.dto.TemplateDto;
import com.kns.tenquest.dto.*;
import com.kns.tenquest.entity.Member;
import com.kns.tenquest.entity.Question;
import com.kns.tenquest.entity.Template;
Expand All @@ -16,7 +13,6 @@
import com.kns.tenquest.repository.TemplateRepository;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
Expand All @@ -32,11 +28,11 @@ public class TemplateService {
private final MemberRepository memberRepository;
private final QuestionRepository questionRepository;

public DtoList<TemplateDto> getAllTemplates(){
public ServiceResult getAllTemplates(){
DtoList<TemplateDto> TemplateDtoList = new DtoList<>(templateRepository.findAll());
return TemplateDtoList;
return new ServiceResult().success().data(TemplateDtoList);
}
public DtoList<TemplateDto> getAllMemberTemplates(String templateOwner){
public ServiceResult getAllTemplatesByMember(String templateOwner){
List<Template> templateList = new ArrayList<Template>(templateRepository.findAllByTemplateOwner(templateOwner));
DtoList<TemplateDto> templateDtoList = new DtoList<>();
for(Template ele : templateList){
Expand All @@ -49,19 +45,19 @@ public int compare(TemplateDto template1, TemplateDto template2) {
return template2.getCreatedAt().compareTo(template1.getCreatedAt());
}
});
return templateDtoList;
return new ServiceResult().success().data(templateDtoList);
}

public TemplateWrapper getTemplate(String templateId){
public ServiceResult getTemplate(String templateId){
Optional<Template> optTemplate = templateRepository.findById(templateId);
if(optTemplate.isEmpty()){
return null;
return new ServiceResult().fail().message("Template not found");
}
TemplateDto templateDto = new TemplateDto(optTemplate.get());
String templateOwnerId = templateDto.getTemplateOwner();
Optional<Member> optMem = memberRepository.findById(templateOwnerId);
if(optMem.isEmpty()){
return null;
return new ServiceResult().fail().message("Template owner not found");
}
MemberDto templateOwner = new MemberDto(optMem.get());
String templateOwnerName = templateOwner.userName;
Expand All @@ -71,21 +67,25 @@ public TemplateWrapper getTemplate(String templateId){
for (TemplateDoc doc : templateDocList1) {
Optional<Question> optQuestion = questionRepository.findById(doc.getQuestionId());
if(optQuestion.isEmpty()){
return null;
return new ServiceResult().fail().message("Question not found");
}
QuestionDto questionDto = new QuestionDto(optQuestion.get());
TemplateDocDto templateDocDto = new TemplateDocDto(doc);
templateDocDto.setQuestionContent(questionDto.getQuestionContent());
templateDocList.add(templateDocDto);
}
return new TemplateWrapper(templateDto,templateOwnerName,templateDocList);
return new ServiceResult().success().data( new TemplateWrapper(templateDto,templateOwnerName,templateDocList));
}
public TemplateDto getTemplateByTemplateName(String templateName){
return new TemplateDto(templateRepository.findTemplateByTemplateName(templateName).orElse(new Template()));

public ServiceResult getTemplateByTemplateName(String templateName){
return
new ServiceResult()
.success()
.data(new TemplateDto(templateRepository.findTemplateByTemplateName(templateName).orElse(new Template())));
}

@Transactional
public TemplateWrapper createTemplate(TemplateWrapper requestWrapper, String memberId) {
public ServiceResult createTemplate(TemplateWrapper requestWrapper, String memberId) {
Optional<Member> nullableMember = memberRepository.findById(memberId);
if(nullableMember.isEmpty()){
throw new NoSuchElementException("존재하지 않는 사용자 입니다.");
Expand All @@ -109,19 +109,19 @@ public TemplateWrapper createTemplate(TemplateWrapper requestWrapper, String mem
}
//templateDoc 생성 로직

return requestWrapper;
return new ServiceResult().success().data(requestWrapper);
} catch (RuntimeException e) {
e.printStackTrace();
throw new RuntimeException("template 생성 중 오류가 발생하였습니다.");
}
}
return null;
return new ServiceResult().fail().message("template creation failed");
} //처음 create 시 생성값 주기

public TemplateDto templateUpdate(String memberId,String templateId, TemplateDto templateDto) {
public ServiceResult templateUpdate(String memberId,String templateId, TemplateDto templateDto) {
Optional<Template> optTemplate = templateRepository.findByTemplateIdAndTemplateOwner(templateId,memberId);
if(optTemplate.isEmpty()){
return null;
return new ServiceResult().fail().message("Template not found");
}
else{
TemplateDto updatingTemplateDto = new TemplateDto(optTemplate.get());
Expand All @@ -130,25 +130,30 @@ public TemplateDto templateUpdate(String memberId,String templateId, TemplateDto
if(StringUtils.isNotBlank(templateDto.getIsPublic().toString()))
updatingTemplateDto.setIsPublic(templateDto.getIsPublic());
templateRepository.save(updatingTemplateDto.toEntity());
return updatingTemplateDto;

return new ServiceResult().success()
.data(updatingTemplateDto);
}
} //수정시 변경사항을 controller에서 적용한 후 저장

public Template templateView(String templateId){
return templateRepository.findById(templateId).get();
public ServiceResult templateView(String templateId)
{
return new ServiceResult().success().data(templateRepository.findById(templateId).get());
}

public TemplateDto templateDelete(String memberId, String templateId){
public ServiceResult templateDelete(String memberId, String templateId){
Optional<Template> optTemplate = templateRepository.findByTemplateIdAndTemplateOwner(templateId,memberId);
if(optTemplate.isEmpty()){
return null;
return new ServiceResult().fail().message("Template not found");
}
List deletingTemplateDocList = new DtoList<>(templateDocRepository.findAllByTemplateId(templateId));

TemplateDto deletedTemplateDto = new TemplateDto(optTemplate.get());
templateDocRepository.deleteAllInBatch(deletingTemplateDocList);
templateRepository.deleteById(templateId);
return deletedTemplateDto;

return new ServiceResult().success()
.data(deletedTemplateDto);

}
}
Expand Down

0 comments on commit a5e3e63

Please sign in to comment.