Skip to content

Commit

Permalink
✨ feat: Respond Delete 구현(#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
600gramSik committed Feb 29, 2024
1 parent b81c2a0 commit f755ce9
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.smunity.petition.domain.petition.controller;

import com.smunity.petition.domain.petition.dto.AgreementRequest;
import com.smunity.petition.domain.petition.dto.AgreementResponse;
import com.smunity.petition.domain.petition.dto.PetitionResponse;
import com.smunity.petition.domain.petition.service.AgreementService;
import com.smunity.petition.global.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/agreements")
public class AgreementController {
private final AgreementService agreementService;

// @GetMapping
// public ApiResponse<List<AgreementResponse.petitionList>> getPetitionList() {
// List<PetitionResponse.petitionList> petitionLists = petitionService.getPetitions();
// return ApiResponse.onSuccess(petitionLists);
// }

// @PostMapping
// public ApiResponse<AgreementResponse.agreementDTO> createAgreement(@RequestBody AgreementRequest request) {
//
// }


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.smunity.petition.domain.petition.controller;

import com.smunity.petition.domain.petition.dto.RespondRequest;
import com.smunity.petition.domain.petition.dto.RespondResponse;
import com.smunity.petition.domain.petition.service.RespondService;
import com.smunity.petition.global.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/responds")
public class RespondController {
private final RespondService respondService;

@GetMapping("/{respondId}")
public ApiResponse<RespondResponse.respondDetail> getRespondDetail(@PathVariable Long respondId) {
RespondResponse.respondDetail respondDetail = respondService.findRespondByPetitionId(respondId);
return ApiResponse.onSuccess(respondDetail);
}

@GetMapping
public ApiResponse<List<RespondResponse.respondList>> getRespondList() {
List<RespondResponse.respondList> respondLists = respondService.getResponds();
return ApiResponse.onSuccess(respondLists);
}

@PostMapping
public ApiResponse<RespondResponse.respondDetail> createRespond(@RequestBody RespondRequest.CreateRespondDTO request) {
return ApiResponse.created(respondService.createRespond(request));
}

@PatchMapping("/{respondId}")
public ApiResponse<RespondResponse.respondDetail> updateRespond(@PathVariable Long respondId, @RequestBody RespondRequest.UpdateDTO updateDTO) {
RespondResponse.respondDetail respondDetail = respondService.updateRespond(updateDTO, respondId);
return ApiResponse.created(respondDetail);
}

@DeleteMapping("/{respondId}")
public ApiResponse<Object> deleteRespond(@PathVariable Long respondId) {
respondService.deleteRespond(respondId);
return ApiResponse.noContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.smunity.petition.domain.petition.dto;

import com.smunity.petition.domain.petition.entity.Agreement;
import com.smunity.petition.domain.petition.entity.Petition;
import lombok.Getter;

public class AgreementRequest {
@Getter
public static class CreateDTO {

private Petition petition;

public Agreement toEntity() {
return Agreement.builder()
.petition(petition)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.smunity.petition.domain.petition.dto;

import com.smunity.petition.domain.account.entity.User;
import com.smunity.petition.domain.petition.entity.Agreement;
import com.smunity.petition.domain.petition.entity.Comment;
import com.smunity.petition.domain.petition.entity.Petition;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class AgreementResponse {
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class agreementDTO {
private Petition petition;
private Long id;
private Long userId;
private User user;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;

public static AgreementResponse.agreementDTO from(Agreement agreement) {
return AgreementResponse.agreementDTO.builder()
.petition(agreement.getPetition())
.userId(agreement.getUser().getId())
.user(agreement.getUser())
.createdDate(agreement.getCreateDate())
.modifiedDate(agreement.getModifyDate())
.build();
}

public static List<CommentResponse.commentDTO> from(List<Comment> comments) {
return comments.stream().map(CommentResponse.commentDTO::from).collect(Collectors.toList());
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.smunity.petition.domain.petition.dto;

import com.smunity.petition.domain.petition.entity.Petition;
import com.smunity.petition.domain.petition.entity.Respond;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RespondRequest {
@Getter
public static class CreateRespondDTO {
private Petition petition;
private String content;


public Respond toEntity() {
return Respond.builder()
.petition(petition)
.content(content)
.build();
}
}
@Getter
public static class UpdateDTO {
private String content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smunity.petition.domain.petition.service;

import com.smunity.petition.domain.account.entity.User;
import com.smunity.petition.domain.account.repository.UserRepository;
import com.smunity.petition.domain.petition.dto.AgreementRequest;
import com.smunity.petition.domain.petition.dto.AgreementResponse;
import com.smunity.petition.domain.petition.entity.Agreement;
import com.smunity.petition.domain.petition.repository.AgreementRepository;
import com.smunity.petition.global.common.code.status.ErrorCode;
import com.smunity.petition.global.common.exception.GeneralException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AgreementService {
private final AgreementRepository agreementRepository;
private final UserRepository userRepository;

// @Transactional
// public AgreementResponse.agreementDTO createAgreement(AgreementRequest.CreateDTO request) {
// User user = userRepository.findByUserName("201911019").orElseThrow(() -> new GeneralException(ErrorCode._INTERNAL_SERVER_ERROR));
// Agreement agreement = request.toEntity();
// }


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.smunity.petition.domain.petition.service;

import com.smunity.petition.domain.account.entity.User;
import com.smunity.petition.domain.account.repository.UserRepository;
import com.smunity.petition.domain.petition.dto.RespondRequest;
import com.smunity.petition.domain.petition.dto.RespondResponse;

import com.smunity.petition.domain.petition.entity.Respond;

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;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class RespondService {
private final RespondRepository respondRepository;
private final UserRepository userRepository;

public RespondResponse.respondDetail findRespondByPetitionId(Long respondId) {
Respond respond = respondRepository.findByPetitionId(respondId);
if (respondId == null) {
throw new GeneralException(ErrorCode.PETITION_NOT_FOUND);
}
//==.orElseThrow(() -> new GeneralException(ErrorCode.PETITION_NOT_FOUND));
return RespondResponse.respondDetail.from(respond);
}

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));
Respond respond = request.toEntity();
respond.setUser(user);
respondRepository.save(respond);
return RespondResponse.respondDetail.from(respond);
}

@Transactional
public RespondResponse.respondDetail updateRespond(RespondRequest.UpdateDTO updateDTO, Long respondId) {
//respondRepo에서 해당 respond를 찾는다.
Respond respond = respondRepository.findById(respondId).orElseThrow(() -> new GeneralException(ErrorCode.RESPOND_NOT_FOUND));
//respond 엔티티에 있는 updateRespond를 사용해서 해당 respond에 대한 내용을 수정한다.
respond.updateRespond(updateDTO);
Respond updated = respondRepository.save(respond);
return RespondResponse.respondDetail.from(updated);
}

@Transactional
public void deleteRespond(Long respondId) {
respondRepository.deleteById(respondId);
}

}

0 comments on commit f755ce9

Please sign in to comment.