Skip to content

Commit

Permalink
♻️ refactor: Respond CRUD 최종 검토(#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
600gramSik committed Mar 13, 2024
1 parent 378a1bb commit 1b1b9d0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -37,16 +40,19 @@ public List<RespondResponse.respondList> getResponds() {
List<Respond> 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를 찾는다.
Expand All @@ -61,5 +67,4 @@ public RespondResponse.respondDetail updateRespond(RespondRequest.UpdateDTO upda
public void deleteRespond(Long respondId) {
respondRepository.deleteById(respondId);
}

}

0 comments on commit 1b1b9d0

Please sign in to comment.