Skip to content

Commit

Permalink
✨ feat: Respond CRU 구현(#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
600gramSik committed Feb 29, 2024
1 parent 9c14325 commit b81c2a0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
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
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
@@ -1,28 +1,56 @@
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 Petition petition;
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()
.petition(respond.getPetition())
.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 Petition petition;


public static respondList from(Respond respond) {
return respondList.builder()
.id(respond.getId())
.userId(respond.getUser().getId())
.petition(respond.getPetition())
.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
@@ -1,23 +1,22 @@
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;
Expand All @@ -27,4 +26,14 @@ public class Respond extends BaseEntity {
private Petition petition;

private String content;

public void setUser(User user) {
this.user = user;
this.user.getResponds().add(this);
}

public void updateRespond(RespondRequest.UpdateDTO updateDTO) {
this.content = updateDTO.getContent();
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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);

List<Respond> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public enum ErrorCode implements BaseErrorCode {
QUESTION_NOT_FOUND(HttpStatus.NOT_FOUND,"QUESTION404", "해당 질문이 존재하지 않습니다."),
ANSWER_NOT_FOUND(HttpStatus.NOT_FOUND, "ANSWER404", "해당 답변이 존재하지 않습니다."),
// petition 관련 에러
PETITION_NOT_FOUND(HttpStatus.NOT_FOUND, "PETITION404", "해당 청원이 존재하지 않습니다.");
PETITION_NOT_FOUND(HttpStatus.NOT_FOUND, "PETITION404", "해당 청원이 존재하지 않습니다."),

//respond 관련 에러(for Update)
RESPOND_NOT_FOUND(HttpStatus.NOT_FOUND, "RESPOND404", "해당 청원 응답이 존재하지 않습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down

0 comments on commit b81c2a0

Please sign in to comment.