Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/52 Respond CRUD 구현 #55

Open
wants to merge 4 commits into
base: release/52
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.smunity.petition.domain.petition.entity.Petition;
import com.smunity.petition.domain.petition.entity.Respond;
import com.smunity.petition.domain.question.entity.Answer;
import com.smunity.petition.domain.question.entity.Question;
import jakarta.persistence.*;
Expand Down Expand Up @@ -70,6 +71,9 @@ public class User {
@OneToMany(mappedBy = "user")
private List<Petition> petitions;

@OneToMany(mappedBy = "user")
private List<Respond> responds;

public void setProfile(Profile profile) {
this.profile = profile;
}
Expand Down
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
Expand Up @@ -21,7 +21,7 @@ public static class petitionDetail {
private Integer category;
private Boolean anonymous;
private List<CommentResponse.commentDTO> comments;
private RespondResponse.respondDTO respond;
private RespondResponse.respondDetail respond;
private Integer agreementCount;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;
Expand All @@ -38,7 +38,7 @@ public static petitionDetail from(Petition petition) {
.anonymous(petition.isAnonymous())
.comments(petition.getComments() != null ? CommentResponse.commentDTO.from(petition.getComments()) : null)
.agreementCount(petition.getAgreements() != null ? petition.getAgreements().size() : 0)
.respond(petition.getRespond() != null ? RespondResponse.respondDTO.from(petition.getRespond()) : null)
.respond(petition.getRespond() != null ? RespondResponse.respondDetail.from(petition.getRespond()) : null)
.createdDate(petition.getCreateDate())
.modifiedDate(petition.getModifyDate())
.endDate(petition.getEndDate())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 Long petitionId;
private String content;
//절대 엔티티 자체를 넣지 말자

public Respond toEntity() {
return Respond.builder()
.petition(null)
//여기선 null값을 넣어주고 Service에서 실제 값을 넣어주자
.content(content)
.build();
}
}
@Getter
public static class UpdateDTO {
private String content;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
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.*;

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

public class RespondResponse {
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class respondDTO {
private String content;
public static class respondDetail {
private Long id;
private Long petitionId;
private Long userId;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;

public static respondDTO from(Respond respond) {
return respondDTO.builder()
.content(respond.getContent())
public static respondDetail from(Respond respond) {
return respondDetail.builder()
.petitionId(respond.getPetition().getId())
.userId(respond.getUser().getId())
.createdDate(respond.getCreateDate())
.modifiedDate(respond.getModifyDate())
.build();
}
}

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class respondList {
private Long id;
private Long userId;
private Long petitionId;

public static respondList from(Respond respond) {
return respondList.builder()
.id(respond.getId())
.userId(respond.getUser().getId())
.petitionId(respond.getPetition().getId())
.build();
}
public static List<respondList> from(List<Respond> responds) {
return responds.stream().map(respondList::from).collect(Collectors.toList());
}
}


}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.smunity.petition.domain.petition.entity;

import com.smunity.petition.domain.account.entity.User;
import com.smunity.petition.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Table(name = "petitions_petition_voter")
public class Agreement {
public class Agreement extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void setUser(User user) {
this.user.getPetitions().add(this);
}

public Petition(Long id) {
this.id = id;
}

public void updatePetition(PetitionRequest.UpdateDTO updateDTO){
this.subject = updateDTO.getSubject();
this.content = updateDTO.getContent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
package com.smunity.petition.domain.petition.entity;

import com.smunity.petition.domain.account.entity.User;
import com.smunity.petition.domain.petition.dto.RespondRequest;
import com.smunity.petition.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Table(name = "petitions_answer")
public class Respond extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

//학생회 측
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private User user;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "petition_id")
private Petition petition;
Petition petition;


private String content;

public void setUser(User user) {
this.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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ public interface PetitionRepository extends JpaRepository<Petition, Long> {

@Query("select p from Petition p")
List<PetitionResponse.petitionList> findAllPetition();

List<Petition> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.smunity.petition.domain.petition.repository;

import com.smunity.petition.domain.petition.dto.RespondResponse;
import com.smunity.petition.domain.petition.entity.Respond;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface RespondRepository extends JpaRepository<Respond, Long> {

@Query( "select r " +
"from Respond r " +
"where r.petition.id = :petitionId")
RespondResponse.respondDTO findByPetitionId(@Param("petitionId") Long petitionId);
//RespondResponse.respondDetail -> Respond
Respond findByPetitionId(@Param("petitionId") Long petitionId);

}
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();
// }


}
Loading