Skip to content

Commit

Permalink
Merge pull request #85 from SWM-Flash/integration
Browse files Browse the repository at this point in the history
Integration
  • Loading branch information
ChoiWonYu authored Oct 11, 2024
2 parents f26fbd7 + aee78c4 commit 4379eb5
Show file tree
Hide file tree
Showing 29 changed files with 593 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.first.flash.climbing.solution.application;

import com.first.flash.account.member.application.MemberService;
import com.first.flash.account.member.domain.Member;
import com.first.flash.climbing.solution.application.dto.SolutionCommentCreateRequestDto;
import com.first.flash.climbing.solution.application.dto.SolutionCommentCreateResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionCommentResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionCommentUpdateRequestDto;
import com.first.flash.climbing.solution.application.dto.SolutionCommentsResponseDto;
import com.first.flash.climbing.solution.domain.Solution;
import com.first.flash.climbing.solution.domain.SolutionComment;
import com.first.flash.climbing.solution.exception.exceptions.SolutionCommentAccessDeniedException;
import com.first.flash.climbing.solution.exception.exceptions.SolutionCommentNotFoundException;
import com.first.flash.climbing.solution.infrastructure.SolutionCommentRepository;
import com.first.flash.global.util.AuthUtil;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SolutionCommentService {

private final MemberService memberService;
private final SolutionCommentRepository solutionCommentRepository;
private final SolutionService solutionService;

@Transactional
public SolutionCommentCreateResponseDto createComment(final Long solutionId,
final SolutionCommentCreateRequestDto request) {
UUID id = AuthUtil.getId();
Member member = memberService.findById(id);

Solution solution = solutionService.findSolutionById(solutionId);
SolutionComment solutionComment = SolutionComment.of(request.content(),
member.getNickName(), member.getProfileImageUrl(), member.getId(), solution);
SolutionComment savedSolutionComment = solutionCommentRepository.save(solutionComment);

return SolutionCommentCreateResponseDto.toDto(savedSolutionComment);
}

public SolutionComment findById(final Long id) {
return solutionCommentRepository.findById(id)
.orElseThrow(
() -> new SolutionCommentNotFoundException(id));
}

public SolutionCommentsResponseDto findBySolutionId(final Long solutionId) {
List<SolutionComment> comments = solutionService.findSolutionById(solutionId)
.getComments();
List<SolutionCommentResponseDto> commentsResponse = comments.stream()
.map(
SolutionCommentResponseDto::toDto)
.toList();
return SolutionCommentsResponseDto.from(commentsResponse);
}

@Transactional
public SolutionCommentResponseDto updateComment(final Long commentId,
final SolutionCommentUpdateRequestDto request) {
SolutionComment comment = findById(commentId);
if (!AuthUtil.isSameId(comment.getCommenterDetail().getCommenterId())) {
throw new SolutionCommentAccessDeniedException();
}
comment.updateContent(request.content());
return SolutionCommentResponseDto.toDto(comment);
}

@Transactional
public void deleteComment(final Long commentId) {
SolutionComment comment = findById(commentId);
if (!AuthUtil.isSameId(comment.getCommenterDetail().getCommenterId())) {
throw new SolutionCommentAccessDeniedException();
}
solutionCommentRepository.delete(comment);
}

@Transactional
public void deleteByCommenterId(final UUID commenterId) {
solutionCommentRepository.deleteByCommenterId(commenterId);
}

@Transactional
public void updateCommenterInfo(final UUID commenterId, final String nickName,
final String profileImageUrl) {
solutionCommentRepository.updateCommenterInfo(commenterId, nickName, profileImageUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ public class SolutionEventHandler {

private final SolutionSaveService solutionSaveService;
private final SolutionService solutionService;
private final SolutionCommentService solutionCommentService;

@EventListener
@Transactional
public void updateSolutionInfo(final MemberInfoUpdatedEvent event) {
solutionSaveService.updateUploaderInfo(event.getMemberId(), event.getNickName(),
event.getInstagramId(), event.getProfileImageUrl());

solutionCommentService.updateCommenterInfo(event.getMemberId(), event.getNickName(),
event.getProfileImageUrl());
}

@EventListener
@Transactional
public void deleteSolution(final MemberDeletedEvent event) {
solutionService.deleteByUploaderId(event.getMemberId());
solutionCommentService.deleteByCommenterId(event.getMemberId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.first.flash.account.member.application.MemberService;
import com.first.flash.account.member.domain.Member;
import com.first.flash.climbing.solution.application.dto.SolutionResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionWriteResponseDto;
import com.first.flash.climbing.solution.application.dto.UnregisteredMemberSolutionCreateRequest;
import com.first.flash.climbing.solution.domain.Solution;
import com.first.flash.climbing.solution.domain.SolutionRepository;
Expand All @@ -24,7 +24,7 @@ public class SolutionSaveService {
private final SolutionRepository solutionRepository;

@Transactional
public SolutionResponseDto saveSolution(final UUID problemId,
public SolutionWriteResponseDto saveSolution(final UUID problemId,
final SolutionCreateRequestDto createRequestDto) {
UUID id = AuthUtil.getId();
Member member = memberService.findById(id);
Expand All @@ -34,7 +34,7 @@ public SolutionResponseDto saveSolution(final UUID problemId,
member.getProfileImageUrl());
Solution savedSolution = solutionRepository.save(solution);
Events.raise(SolutionSavedEvent.of(savedSolution.getProblemId()));
return SolutionResponseDto.toDto(savedSolution);
return SolutionWriteResponseDto.toDto(savedSolution);
}

@Transactional
Expand All @@ -44,7 +44,7 @@ public void updateUploaderInfo(final UUID uploaderId, final String nickName,
}

@Transactional
public SolutionResponseDto saveUnregisteredMemberSolution(final UUID problemId,
public SolutionWriteResponseDto saveUnregisteredMemberSolution(final UUID problemId,
final UnregisteredMemberSolutionCreateRequest requestDto) {
UUID id = AuthUtil.getId();
Member member = memberService.findById(id);
Expand All @@ -54,6 +54,6 @@ public SolutionResponseDto saveUnregisteredMemberSolution(final UUID problemId,
requestDto.profileImageUrl());
Solution savedSolution = solutionRepository.save(solution);
Events.raise(SolutionSavedEvent.of(savedSolution.getProblemId()));
return SolutionResponseDto.toDto(savedSolution);
return SolutionWriteResponseDto.toDto(savedSolution);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.first.flash.account.member.application.BlockService;
import com.first.flash.climbing.gym.domian.ClimbingGymIdConfirmRequestedEvent;
import com.first.flash.climbing.problem.domain.ProblemIdConfirmRequestedEvent;
import com.first.flash.climbing.solution.application.dto.MySolutionsResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionUpdateRequestDto;
import com.first.flash.climbing.solution.application.dto.SolutionWriteResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionsPageResponseDto;
import com.first.flash.climbing.solution.application.dto.SolutionsResponseDto;
import com.first.flash.climbing.solution.domain.Solution;
Expand All @@ -15,6 +14,7 @@
import com.first.flash.climbing.solution.exception.exceptions.SolutionNotFoundException;
import com.first.flash.climbing.solution.infrastructure.dto.DetailSolutionDto;
import com.first.flash.climbing.solution.infrastructure.dto.MySolutionDto;
import com.first.flash.climbing.solution.infrastructure.dto.SolutionResponseDto;
import com.first.flash.climbing.solution.infrastructure.paging.SolutionCursor;
import com.first.flash.global.event.Events;
import com.first.flash.global.util.AuthUtil;
Expand Down Expand Up @@ -46,10 +46,7 @@ public SolutionsResponseDto findAllSolutionsByProblemId(final UUID problemId) {
Events.raise(ProblemIdConfirmRequestedEvent.of(problemId));
List<UUID> blockedMembers = blockService.findBlockedMembers();
List<SolutionResponseDto> solutions = solutionRepository.findAllByProblemId(problemId,
blockedMembers)
.stream()
.map(SolutionResponseDto::toDto)
.toList();
AuthUtil.getId(), blockedMembers);

return SolutionsResponseDto.of(solutions);
}
Expand All @@ -68,7 +65,7 @@ public SolutionsPageResponseDto findMySolutions(final String cursor, final int s
}

@Transactional
public SolutionResponseDto updateContent(final Long id,
public SolutionWriteResponseDto updateContent(final Long id,
final SolutionUpdateRequestDto requestDto) {

Solution solution = solutionRepository.findById(id)
Expand All @@ -79,7 +76,7 @@ public SolutionResponseDto updateContent(final Long id,

solution.updateContentInfo(requestDto.review(), requestDto.videoUrl());

return SolutionResponseDto.toDto(solution);
return SolutionWriteResponseDto.toDto(solution);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.first.flash.climbing.solution.application.dto;

import jakarta.validation.constraints.NotEmpty;

public record SolutionCommentCreateRequestDto(@NotEmpty String content) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.first.flash.climbing.solution.application.dto;

import com.first.flash.climbing.solution.domain.SolutionComment;
import java.util.UUID;

public record SolutionCommentCreateResponseDto(Long id, String content, UUID commenterId,
String nickName, String profileImageUrl) {

public static SolutionCommentCreateResponseDto toDto(
final SolutionComment comment) {
return new SolutionCommentCreateResponseDto(comment.getId(), comment.getContent(),
comment.getCommenterDetail().getCommenterId(),
comment.getCommenterDetail().getCommenter(),
comment.getCommenterDetail().getProfileImageUrl());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.first.flash.climbing.solution.application.dto;

import com.first.flash.climbing.solution.domain.SolutionComment;
import com.first.flash.global.util.AuthUtil;
import java.util.UUID;

public record SolutionCommentResponseDto(Long id, String content, UUID commenterId, String nickName,
String profileImageUrl, boolean isMine) {

public static SolutionCommentResponseDto toDto(final SolutionComment solutionComment) {
return new SolutionCommentResponseDto(solutionComment.getId(), solutionComment.getContent(),
solutionComment.getCommenterDetail().getCommenterId(),
solutionComment.getCommenterDetail().getCommenter(),
solutionComment.getCommenterDetail().getProfileImageUrl(),
solutionComment.getCommenterDetail().getCommenterId().equals(AuthUtil.getId())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.first.flash.climbing.solution.application.dto;

import jakarta.validation.constraints.NotNull;

public record SolutionCommentUpdateRequestDto(@NotNull String content) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.first.flash.climbing.solution.application.dto;

import java.util.List;

public record SolutionCommentsResponseDto(List<SolutionCommentResponseDto> comments) {

public static SolutionCommentsResponseDto from(
final List<SolutionCommentResponseDto> commentsResponse) {
return new SolutionCommentsResponseDto(commentsResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
import com.first.flash.global.util.AuthUtil;
import java.util.UUID;

public record SolutionResponseDto(Long id, String uploader, String review, String instagramId,
String videoUrl, UUID uploaderId, Boolean isUploader,
String profileImageUrl) {
public record SolutionWriteResponseDto(Long id, String uploader, String review, String instagramId,
String videoUrl, UUID uploaderId, Boolean isUploader,
String profileImageUrl) {

public static SolutionResponseDto toDto(final Solution solution) {
public static SolutionWriteResponseDto toDto(final Solution solution) {
SolutionDetail solutionDetail = solution.getSolutionDetail();
UploaderDetail uploaderDetail = solution.getUploaderDetail();

UUID uploaderId = uploaderDetail.getUploaderId();
Boolean isUploader = AuthUtil.isSameId(uploaderId);

return new SolutionResponseDto(solution.getId(), uploaderDetail.getUploader(),
return new SolutionWriteResponseDto(solution.getId(), uploaderDetail.getUploader(),
solutionDetail.getReview(), uploaderDetail.getInstagramId(),
solutionDetail.getVideoUrl(), uploaderDetail.getUploaderId(), isUploader,
uploaderDetail.getProfileImageUrl());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.first.flash.climbing.solution.application.dto;

import com.first.flash.climbing.solution.infrastructure.dto.SolutionResponseDto;
import java.util.List;

public record SolutionsResponseDto(List<SolutionResponseDto> solutions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.first.flash.climbing.solution.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.util.UUID;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class CommenterDetail {

@Column(columnDefinition = "BINARY(16)")
private UUID commenterId;
private String commenter;
private String profileImageUrl;

protected CommenterDetail(final UUID commenterId, final String commenter, final String profileImageUrl) {
this.commenterId = commenterId;
this.commenter = commenter;
this.profileImageUrl = profileImageUrl;
}

public static CommenterDetail of(final UUID commenterId, final String commenter, final String profileImageUrl) {
return new CommenterDetail(commenterId, commenter, profileImageUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import com.first.flash.climbing.solution.domain.vo.SolutionDetail;
import com.first.flash.climbing.solution.domain.vo.UploaderDetail;
import com.first.flash.global.domain.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -31,6 +35,9 @@ public class Solution extends BaseEntity {
private SolutionDetail solutionDetail;
private UploaderDetail uploaderDetail;
private Long optionalWeight;
@OneToMany(mappedBy = "solution", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private List<SolutionComment> comments = new ArrayList<>();

protected Solution(final String uploader, final String review, final String instagramId,
final String videoUrl, final UUID problemId, final UUID uploaderId,
Expand Down
Loading

0 comments on commit 4379eb5

Please sign in to comment.