-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b81c2a0
commit f755ce9
Showing
7 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/smunity/petition/domain/petition/controller/AgreementController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// | ||
// } | ||
|
||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/smunity/petition/domain/petition/controller/RespondController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/smunity/petition/domain/petition/dto/AgreementRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/smunity/petition/domain/petition/dto/AgreementResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
src/main/java/com/smunity/petition/domain/petition/dto/RespondRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/smunity/petition/domain/petition/service/AgreementService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// } | ||
|
||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/smunity/petition/domain/petition/service/RespondService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |