Skip to content

Commit

Permalink
🔗 :: (#422) BugReport 에그리거트 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
tedsoftj1123 authored Sep 22, 2023
2 parents 8bc0f0a + 9492ba0 commit 0d81ad9
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package team.retum.jobis.domain.bug.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import team.retum.jobis.common.annotation.Aggregate;

@Getter
@Builder
@Aggregate
@AllArgsConstructor
public class BugAttachment {

private final Long bugReportId;

private final String attachmentUrl;

public static BugAttachment of(String attachmentUrl) {
return new BugAttachment(attachmentUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public class BugReport {
private final String content;
private final DevelopmentArea developmentArea;
private final Long studentId;
private final List<BugAttachment> bugAttachments;
private final List<BugAttachment> attachments;
private final LocalDateTime createdAt;

public BugReport addAllBugAttachments(List<BugAttachment> bugAttachments) {
return this.toBuilder()
.bugAttachments(bugAttachments)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package team.retum.jobis.domain.bug.spi;

import team.retum.jobis.domain.bug.model.BugAttachment;
import team.retum.jobis.domain.bug.model.BugReport;

import java.util.List;

public interface CommandBugReportPort {

BugReport saveBugReport(BugReport bugReport);

List<BugAttachment> saveAllBugAttachment(List<BugAttachment> bugAttachments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,24 @@ public class CreateBugReportUseCase {
private final SecurityPort securityPort;

public void execute(CreateBugReportRequest request) {
BugReport savedBugReport = saveBugReport(request);
String writer = securityPort.getCurrentStudent().getName();

publishBugReportEventPort.publishBugReport(savedBugReport, writer);
}
List<BugAttachment> attachments = List.of();
if (request.getAttachmentUrls() != null) {
attachments = request.getAttachmentUrls().stream()
.map(BugAttachment::of)
.toList();
}

private BugReport saveBugReport(CreateBugReportRequest request) {
BugReport savedBugReport = commandBugReportPort.saveBugReport(
BugReport.builder()
.title(request.getTitle())
.content(request.getContent())
.developmentArea(request.getDevelopmentArea())
.studentId(securityPort.getCurrentUserId())
.attachments(attachments)
.build()
);

if (request.getAttachmentUrls() != null) {
List<BugAttachment> savedBugAttachments = commandBugReportPort.saveAllBugAttachment(
request.getAttachmentUrls().stream()
.map(attachmentUrl ->
BugAttachment.builder()
.bugReportId(savedBugReport.getId())
.attachmentUrl(attachmentUrl)
.build()
).toList()
);

return savedBugReport.addAllBugAttachments(savedBugAttachments);
}

return savedBugReport;
String writer = securityPort.getCurrentStudent().getName();
publishBugReportEventPort.publishBugReport(savedBugReport, writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public QueryBugReportDetailsResponse execute(Long bugReportId) {
.content(bugReport.getContent())
.developmentArea(bugReport.getDevelopmentArea())
.attachments(
bugReport.getBugAttachments().stream()
bugReport.getAttachments().stream()
.map(BugAttachment::getAttachmentUrl)
.toList())
.createdAt(bugReport.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import team.retum.jobis.domain.bug.model.BugAttachment;
import team.retum.jobis.domain.bug.model.BugReport;
import team.retum.jobis.domain.bug.model.DevelopmentArea;
import team.retum.jobis.domain.bug.persistence.mapper.BugAttachmentMapper;
import team.retum.jobis.domain.bug.persistence.mapper.BugReportMapper;
import team.retum.jobis.domain.bug.persistence.repository.BugAttachmentJpaRepository;
import team.retum.jobis.domain.bug.persistence.repository.BugReportJpaRepository;
import team.retum.jobis.domain.bug.persistence.repository.vo.QQueryBugReportsVO;
import team.retum.jobis.domain.bug.spi.BugReportPort;
Expand All @@ -28,8 +25,6 @@ public class BugReportPersistenceAdapter implements BugReportPort {

private final BugReportJpaRepository bugReportJpaRepository;
private final BugReportMapper bugReportMapper;
private final BugAttachmentJpaRepository bugAttachmentJpaRepository;
private final BugAttachmentMapper bugAttachmentMapper;
private final JPAQueryFactory queryFactory;

@Override
Expand All @@ -41,17 +36,6 @@ public BugReport saveBugReport(BugReport bugReport) {
);
}

@Override
public List<BugAttachment> saveAllBugAttachment(List<BugAttachment> bugAttachments) {
return bugAttachmentJpaRepository.saveAll(
bugAttachments.stream()
.map(bugAttachmentMapper::toEntity)
.toList()
).stream()
.map(bugAttachmentMapper::toDomain)
.toList();
}

@Override
public Optional<BugReport> queryBugReportById(Long id) {
return bugReportJpaRepository.findById(id)
Expand All @@ -62,7 +46,7 @@ public Optional<BugReport> queryBugReportById(Long id) {
public List<BugReportsVO> queryBugReportsByDevelopmentArea(DevelopmentArea developmentArea) {
return queryFactory
.selectFrom(bugReportEntity)
.leftJoin(bugReportEntity.bugAttachments, bugAttachmentEntity)
.leftJoin(bugReportEntity.attachments, bugAttachmentEntity)
.where(eqDevelopmentArea(developmentArea))
.orderBy(bugReportEntity.createdAt.desc())
.transform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@IdClass(BugAttachmentId.class)
@Table(name = "tbl_bug_attachment")
@Entity
@Embeddable
public class BugAttachmentEntity {

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bug_report_id", nullable = false)
private BugReportEntity bugReport;

@Id
@NotNull
@Column(columnDefinition = "VARCHAR(300)")
private String attachmentUrl;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;
import team.retum.jobis.global.entity.BaseTimeEntity;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand All @@ -18,7 +20,6 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
Expand Down Expand Up @@ -51,15 +52,18 @@ public class BugReportEntity extends BaseTimeEntity {
@JoinColumn(name = "student_id", nullable = false)
private StudentEntity student;

@OneToMany(mappedBy = "bugReport", orphanRemoval = true)
private List<BugAttachmentEntity> bugAttachments = new ArrayList<>();
@ElementCollection
@CollectionTable(name = "tbl_bug_attachment", joinColumns = @JoinColumn(name = "bug_report_id"))
private List<BugAttachmentEntity> attachments = new ArrayList<>();

@Builder
public BugReportEntity(Long id, String title, String content, DevelopmentArea developmentArea, StudentEntity student) {
public BugReportEntity(Long id, String title, String content, List<BugAttachmentEntity> attachments,
DevelopmentArea developmentArea, StudentEntity student) {
this.id = id;
this.title = title;
this.content = content;
this.developmentArea = developmentArea;
this.attachments = attachments;
this.student = student;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import org.springframework.stereotype.Component;
import team.retum.jobis.domain.bug.model.BugAttachment;
import team.retum.jobis.domain.bug.model.BugReport;
import team.retum.jobis.domain.bug.persistence.entity.BugAttachmentEntity;
import team.retum.jobis.domain.bug.persistence.entity.BugReportEntity;
import team.retum.jobis.domain.student.exception.StudentNotFoundException;
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;
import team.retum.jobis.domain.student.persistence.repository.StudentJpaRepository;

import java.util.List;

@RequiredArgsConstructor
@Component
public class BugReportMapper {
Expand All @@ -16,32 +20,34 @@ public class BugReportMapper {

public BugReportEntity toEntity(BugReport domain) {
StudentEntity student = studentJpaRepository.findById(domain.getStudentId())
.orElse(null);
.orElseThrow(() -> StudentNotFoundException.EXCEPTION);

List<BugAttachmentEntity> attachments = domain.getAttachments().stream()
.map(attachment -> new BugAttachmentEntity(attachment.getAttachmentUrl()))
.toList();

return BugReportEntity.builder()
.id(domain.getId())
.content(domain.getContent())
.title(domain.getTitle())
.developmentArea(domain.getDevelopmentArea())
.student(student)
.attachments(attachments)
.build();
}

public BugReport toDomain(BugReportEntity entity) {
List<BugAttachment> attachments = entity.getAttachments().stream()
.map(attachment -> new BugAttachment(attachment.getAttachmentUrl()))
.toList();

return BugReport.builder()
.id(entity.getId())
.content(entity.getContent())
.title(entity.getTitle())
.developmentArea(entity.getDevelopmentArea())
.studentId(entity.getStudent().getId())
.bugAttachments(
entity.getBugAttachments().stream()
.map(
bugAttachmentEntity -> BugAttachment.builder()
.bugReportId(bugAttachmentEntity.getBugReport().getId())
.attachmentUrl(bugAttachmentEntity.getAttachmentUrl())
.build()
).toList()
)
.attachments(attachments)
.createdAt(entity.getCreatedAt())
.build();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.filter.OncePerRequestFilter;
import team.retum.jobis.common.error.ErrorProperty;
Expand All @@ -10,12 +11,14 @@
import team.retum.jobis.global.error.exception.GlobalErrorCode;
import team.retum.jobis.global.error.response.ErrorResponse;
import team.retum.jobis.global.security.auth.CurrentUserHolder;
import team.retum.jobis.global.util.LogUtil;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
public class GlobalExceptionFilter extends OncePerRequestFilter {

Expand All @@ -32,11 +35,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (e.getCause() instanceof JobisException jobisException) {
writeErrorResponse(response, jobisException.getErrorProperty());
} else {
e.printStackTrace();
eventPublisher.publishEvent(ExceptionEvent.builder()
.request(request)
.exception(e)
.build());
log.error(LogUtil.stackTraceToString(e));
eventPublisher.publishEvent(ExceptionEvent.builder().request(request).exception(e).build());
writeErrorResponse(response, GlobalErrorCode.INTERNAL_SERVER_ERROR);
}
} finally {
Expand All @@ -49,8 +49,6 @@ private void writeErrorResponse(HttpServletResponse response, ErrorProperty erro
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
ErrorResponse errorResponse = ErrorResponse.of(errorProperty);
response.getWriter().write(
objectMapper.writeValueAsString(errorResponse)
);
response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
}
}
Loading

0 comments on commit 0d81ad9

Please sign in to comment.