diff --git a/src/main/java/com/smunity/petition/domain/petition/dto/RespondRequest.java b/src/main/java/com/smunity/petition/domain/petition/dto/RespondRequest.java index b73aa84..eebd72b 100644 --- a/src/main/java/com/smunity/petition/domain/petition/dto/RespondRequest.java +++ b/src/main/java/com/smunity/petition/domain/petition/dto/RespondRequest.java @@ -16,7 +16,8 @@ public static class CreateRespondDTO { public Respond toEntity() { return Respond.builder() - .petition(new Petition(petitionId)) + .petition(null) + //여기선 null값을 넣어주고 Service에서 실제 값을 넣어주자 .content(content) .build(); } diff --git a/src/main/java/com/smunity/petition/domain/petition/entity/Respond.java b/src/main/java/com/smunity/petition/domain/petition/entity/Respond.java index 4182329..1468705 100644 --- a/src/main/java/com/smunity/petition/domain/petition/entity/Respond.java +++ b/src/main/java/com/smunity/petition/domain/petition/entity/Respond.java @@ -33,6 +33,10 @@ public void setUser(User user) { this.user.getResponds().add(this); } + public void setPetition(Petition petition) { + this.petition = petition; + } + public void updateRespond(RespondRequest.UpdateDTO updateDTO) { this.content = updateDTO.getContent(); } diff --git a/src/main/java/com/smunity/petition/domain/petition/service/RespondService.java b/src/main/java/com/smunity/petition/domain/petition/service/RespondService.java index 651812b..68b4ac6 100644 --- a/src/main/java/com/smunity/petition/domain/petition/service/RespondService.java +++ b/src/main/java/com/smunity/petition/domain/petition/service/RespondService.java @@ -5,8 +5,10 @@ import com.smunity.petition.domain.petition.dto.RespondRequest; import com.smunity.petition.domain.petition.dto.RespondResponse; +import com.smunity.petition.domain.petition.entity.Petition; import com.smunity.petition.domain.petition.entity.Respond; +import com.smunity.petition.domain.petition.repository.PetitionRepository; import com.smunity.petition.domain.petition.repository.RespondRepository; import com.smunity.petition.global.common.code.status.ErrorCode; import com.smunity.petition.global.common.exception.GeneralException; @@ -23,6 +25,7 @@ public class RespondService { private final RespondRepository respondRepository; private final UserRepository userRepository; + private final PetitionRepository petitionRepository; public RespondResponse.respondDetail findRespondByPetitionId(Long respondId) { Respond respond = respondRepository.findByPetitionId(respondId); @@ -37,16 +40,19 @@ public List getResponds() { List responds = respondRepository.findAll(); return RespondResponse.respondList.from(responds); } - + //청원글 생성 @Transactional public RespondResponse.respondDetail createRespond(RespondRequest.CreateRespondDTO request) { User user = userRepository.findByUserName("201910925").orElseThrow(() -> new GeneralException(ErrorCode._INTERNAL_SERVER_ERROR)); + Petition petition = petitionRepository.findById(request.getPetitionId()).orElseThrow(() -> new GeneralException(ErrorCode.PETITION_NOT_FOUND)); Respond respond = request.toEntity(); + //DTO에 Petition 객체를 넣는 대신 여기서 Petition을 주입해주고 id를 받자 + respond.setPetition(petition); respond.setUser(user); respondRepository.save(respond); return RespondResponse.respondDetail.from(respond); } - + //청원글 업데이트 @Transactional public RespondResponse.respondDetail updateRespond(RespondRequest.UpdateDTO updateDTO, Long respondId) { //respondRepo에서 해당 respond를 찾는다. @@ -61,5 +67,4 @@ public RespondResponse.respondDetail updateRespond(RespondRequest.UpdateDTO upda public void deleteRespond(Long respondId) { respondRepository.deleteById(respondId); } - }