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

[refactor] private-post 리팩토링 #39

Merged
merged 2 commits into from
Nov 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.netty:netty-all:4.1.75.Final' // netty 버전에 맞게 설정
implementation 'io.netty:netty-all:4.1.75.Final'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import aimo.backend.domains.auth.security.loginFilter.LoginFilter;
import aimo.backend.domains.member.service.MemberService;
import lombok.RequiredArgsConstructor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
Expand Down Expand Up @@ -39,7 +40,6 @@ public class SecurityConfig {
private final PasswordEncoder passwordEncoder;
private final AntPathMatcher pathMatcher = new AntPathMatcher();


@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// 비활성확 목록
Expand Down
10 changes: 8 additions & 2 deletions backend/src/main/java/aimo/backend/common/dto/DataResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static <T> DataResponse<T> from(T data) {
public static <T> DataResponse<Void> ok() {
return new DataResponse<>(HttpStatus.OK, null);
}
public static <T> DataResponse<Void> created() { return new DataResponse<>(HttpStatus.CREATED, null); }
public static <T> DataResponse<Void> noContent() { return new DataResponse<>(HttpStatus.NO_CONTENT, null); }

public static <T> DataResponse<Void> created() {
return new DataResponse<>(HttpStatus.CREATED, null);
}

public static <T> DataResponse<Void> noContent() {
return new DataResponse<>(HttpStatus.NO_CONTENT, null);
}
}
11 changes: 6 additions & 5 deletions backend/src/main/java/aimo/backend/common/entity/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.NoArgsConstructor;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -17,11 +18,11 @@
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updatedAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}

3 changes: 2 additions & 1 deletion backend/src/main/java/aimo/backend/common/entity/Like.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
@EntityListeners(AuditingEntityListener.class)
public abstract class Like extends BaseEntity {

@Id @GeneratedValue
@Id
@GeneratedValue
@Column(nullable = false)
private Long like_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public enum ErrorCode {
INVALID_EMAIL(HttpStatus.BAD_REQUEST, "이메일 형식이 잘못 되었습니다.", "MEMBER-009"),
EMAIL_NOT_MATCH(HttpStatus.BAD_REQUEST, "이메일이 일치하지 않습니다.", "MEMBER-010"),


//PrivatePost
PRIVATE_POST_NOT_FOUND(HttpStatus.NOT_FOUND, "해당하는 대화록을 찾을 수 없습니다.", "DISPUTE-001"),
PRIVATE_POST_CREATE_FAIL(HttpStatus.BAD_REQUEST, "대화록 생성에 실패하였습니다.", "DISPUTE-002"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

import org.apache.coyote.Response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -13,37 +14,47 @@
public class GlobalExceptionHandler {
@ExceptionHandler(ApiException.class)
public ResponseEntity<ErrorResponse> handleApiException(ApiException e) {
return ResponseEntity
.status(e.getHttpStatus())
.body(ErrorResponse.of(e.getHttpStatus(), e.getMessage(), e.getCode()));
return makeResponseEntity(e);
}


@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.of(HttpStatus.BAD_REQUEST, e.getMessage(), ErrorCode.ILLEGAL_ARGUMENT.getCode()));
return makeResponseEntity400(e);
}

@ExceptionHandler(IOException.class)
public ResponseEntity<ErrorResponse> handleIOException(IOException e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), ErrorCode.IO_EXCEPTION.getCode()));
return makeResponseEntity5xx(e);
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), ErrorCode.INTERNAL_SERVER_ERROR.getCode()));
return makeResponseEntity5xx(e);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
return makeResponseEntity5xx(e);
}

public ResponseEntity<ErrorResponse> makeResponseEntity(ApiException e) {
return ResponseEntity
.status(e.getHttpStatus())
.body(ErrorResponse.of(e.getHttpStatus(), e.getMessage(), e.getCode()));
}

public ResponseEntity<ErrorResponse> makeResponseEntity400(Exception e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.of(HttpStatus.BAD_REQUEST, e.getMessage(), ErrorCode.ILLEGAL_ARGUMENT.getCode()));
}

public ResponseEntity<ErrorResponse> makeResponseEntity5xx(Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), ErrorCode.INTERNAL_SERVER_ERROR.getCode()));
.status(ErrorCode.INTERNAL_SERVER_ERROR.getHttpStatus())
.body(ErrorResponse.of(
ErrorCode.INTERNAL_SERVER_ERROR.getHttpStatus(),
e.getMessage(),
ErrorCode.INTERNAL_SERVER_ERROR.getCode()));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package aimo.backend.common.mapper;

import aimo.backend.domains.privatePost.dto.UploadAudioSuccessRequest;
import aimo.backend.domains.privatePost.dto.SaveAudioSuccessRequest;
import aimo.backend.domains.privatePost.entity.AudioRecord;

public class AudioRecordMapper {
public static AudioRecord toEntity(UploadAudioSuccessRequest uploadAudioSuccessRequest) {

public static AudioRecord toEntity(SaveAudioSuccessRequest saveAudioSuccessRequest) {
return AudioRecord
.builder()
.url(uploadAudioSuccessRequest.url())
.size(uploadAudioSuccessRequest.size())
.filename(uploadAudioSuccessRequest.filename())
.url(saveAudioSuccessRequest.url())
.size(saveAudioSuccessRequest.size())
.filename(saveAudioSuccessRequest.filename())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
@Component
@RequiredArgsConstructor
public class MemberMapper {

private final PasswordEncoder passwordEncoder;

public Member signUpMemberEntity(SignUpRequest signUpRequest){
public Member signUpMemberEntity(SignUpRequest signUpRequest) {
return Member
.builder()
.username(signUpRequest.username())
Expand Down
40 changes: 40 additions & 0 deletions backend/src/main/java/aimo/backend/common/mapper/PostMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package aimo.backend.common.mapper;

import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.post.dto.PostPreviewResponse;
import aimo.backend.domains.post.dto.SavePostRequest;
import aimo.backend.domains.post.entity.Post;

public class PostMapper {

public static Post toEntity(SavePostRequest request, Member member) {
return Post
.builder()
.member(member)
.title(request.title())
.summaryAi(request.summaryAi())
.stancePlaintiff(request.stancePlaintiff())
.stanceDefendant(request.stanceDefendant())
.privatePostId(request.privatePostId())
.originType(request.originType())
.category(request.category())
.build();
}

public static PostPreviewResponse toPreviewResponse(Post post) {
return new PostPreviewResponse(
post.getId(),
post.getTitle(),
getPreview(post.getSummaryAi(), 90),
post.getPostLikes().size(),
post.getPostViews().size(),
post.getCommentsCount(),
post.getCreatedAt()
);
}

public static String getPreview(String summaryAi, Integer length) {
return summaryAi.length() > length ?
summaryAi.substring(0, length) + "..." : summaryAi;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
public class PrivatePostMapper {

public static PrivatePost toEntity(SummaryAndJudgementResponse summaryAndJudgementResponse) {
return PrivatePost
.builder()
return PrivatePost.builder()
.title(summaryAndJudgementResponse.title())
.stancePlaintiff(summaryAndJudgementResponse.stancePlaintiff())
.stanceDefendant(summaryAndJudgementResponse.stanceDefendant())
Expand All @@ -20,31 +19,19 @@ public static PrivatePost toEntity(SummaryAndJudgementResponse summaryAndJudgeme
.build();
}

public static PrivatePostPreviewResponse toPreviewResponse(PrivatePost privatePost){
return new PrivatePostPreviewResponse(
privatePost.getId(),
privatePost.getTitle(),
getPreview(privatePost.getSummaryAi(), 21),
privatePost.getOriginType(),
privatePost.getCreatedAt(),
privatePost.getPublished()
);
public static PrivatePostPreviewResponse toPreviewResponse(PrivatePost privatePost) {
return new PrivatePostPreviewResponse(privatePost.getId(), privatePost.getTitle(),
getPreview(privatePost.getSummaryAi(), 21), privatePost.getOriginType(), privatePost.getCreatedAt(),
privatePost.getPublished());
}

public static String getPreview(String summaryAi, Integer length){
return summaryAi.length() > length ?
summaryAi.substring(0, length) + "..." : summaryAi;
public static String getPreview(String summaryAi, Integer length) {
return summaryAi.length() > length ? summaryAi.substring(0, length) + "..." : summaryAi;
}

public static PrivatePostResponse toResponse(PrivatePost privatePost){
return new PrivatePostResponse(
privatePost.getTitle(),
privatePost.getSummaryAi(),
privatePost.getStancePlaintiff(),
privatePost.getStanceDefendant(),
privatePost.getJudgement(),
privatePost.getFaultRate(),
privatePost.getPublished()
);
public static PrivatePostResponse toResponse(PrivatePost privatePost) {
return new PrivatePostResponse(privatePost.getTitle(), privatePost.getSummaryAi(),
privatePost.getStancePlaintiff(), privatePost.getStanceDefendant(), privatePost.getJudgement(),
privatePost.getFaultRate(), privatePost.getPublished());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import lombok.Getter;
import lombok.Setter;

@Getter @Setter
@Getter
@Setter
@ConfigurationProperties(prefix = "ai.server")
public class AiServerProperties {

private String domainUrl;
private String judgementApi;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@

import lombok.Getter;
import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@Setter
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {

private String issuer;
private String secret;
private Access access;
private Refresh refresh;

private String ACCESS_TOKEN_SUBJECT = "AccessToken";
private String REFRESH_TOKEN_SUBJECT = "RefreshToken";
private String MEMBER_ID_CLAIM = "member_id";
private String BEARER = "Bearer ";

@Getter
@Setter
public static class Access {
private int expiration;
private String header;
}

@Getter
@Setter
public static class Refresh {
private int expiration;
private String header;
}
private String issuer;
private String secret;
private Access access;
private Refresh refresh;

private String ACCESS_TOKEN_SUBJECT = "AccessToken";
private String REFRESH_TOKEN_SUBJECT = "RefreshToken";
private String MEMBER_ID_CLAIM = "member_id";
private String BEARER = "Bearer ";

@Getter
@Setter
public static class Access {

private int expiration;
private String header;
}

@Getter
@Setter
public static class Refresh {

private int expiration;
private String header;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Getter;
import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@Setter
@ConfigurationProperties(prefix = "security")
public class SecurityProperties {
private String[] permitUrls;
private String password;

private String[] permitUrls;
private String password;
}
Loading