Skip to content

Commit

Permalink
#398 [fix] conflict 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
parkheeddong committed Jul 2, 2024
2 parents 383d43a + df7b6c0 commit 013d0cd
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mile.common.resolver.user.UserId;
import com.mile.dto.SuccessResponse;
import com.mile.exception.message.SuccessMessage;
import com.mile.writername.service.WriterNameDeleteService;
import com.mile.writername.service.WriterNameService;
import com.mile.writername.service.dto.WriterNameDescriptionResponse;
import com.mile.writername.service.dto.WriterNameDescriptionUpdateRequest;
Expand All @@ -23,7 +22,6 @@
@RequestMapping("/api/writername")
public class WriterNameController implements WriterNameControllerSwagger {

private final WriterNameDeleteService writerNameDeleteService;
private final WriterNameService writerNameService;

@Override
Expand All @@ -32,7 +30,7 @@ public ResponseEntity<SuccessResponse> deleteMember(
@PathVariable("writerNameId") final Long writerNameId,
@UserId final Long userId
) {
writerNameDeleteService.deleteWriterNameById(writerNameId, userId);
writerNameService.deleteWriterNameById(writerNameId, userId);
return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.MOIM_MEMBER_DELETE_SUCCESS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ public void deleteComments(final List<Post> posts) {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.mile.post.service.PostGetService;
import com.mile.post.service.dto.CommentCreateRequest;
import com.mile.writername.domain.WriterName;
import com.mile.writername.service.WriterNameService;
import com.mile.writername.service.WriterNameRetriever;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -26,13 +26,13 @@ public class CommentService {

private final PostAuthenticateService postAuthenticateService;
private final CommentRepository commentRepository;
private final WriterNameService writerNameService;
private final PostGetService postGetService;
private final WriterNameRetriever writerNameRetriever;
private final CommentReplyService commentReplyService;
private final CommentRetriever commentRetriever;
private final CommentRemover commentRemover;
private final CommentCreator commentCreator;
private final CommentReplyRemover commentReplyRemover;
private final PostGetService postGetService;


@Transactional
Expand Down Expand Up @@ -69,7 +69,7 @@ public List<CommentResponse> getCommentResponse(
) {
postAuthenticateService.authenticateUserWithPostId(postId, userId);
List<Comment> commentList = commentRetriever.findByPostId(postId);
Long writerNameId = writerNameService.getWriterNameIdByMoimIdAndUserId(moimId, userId);
Long writerNameId = writerNameRetriever.getWriterNameIdByMoimIdAndUserId(moimId, userId);
return commentList.stream()
.map(comment -> CommentResponse.of(
comment,
Expand All @@ -85,7 +85,7 @@ public String createCommentReply(
) {
Comment comment = commentRetriever.findById(commentId);
return commentReplyService.createCommentReply(
writerNameService.findWriterNameByMoimIdAndUserId(commentRetriever.getMoimIdFromComment(comment), userId),
writerNameRetriever.findWriterNameByMoimIdAndUserId(commentRetriever.getMoimIdFromComment(comment), userId),
comment,
replyCreateRequest);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mile.curious.service;


import com.mile.curious.domain.Curious;
import com.mile.curious.repository.CuriousRepository;
import com.mile.post.domain.Post;
import com.mile.writername.domain.WriterName;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class CuriousRemover {
private final CuriousRepository curiousRepository;
private final CuriousRetriever curiousRetriever;

public void deleteAllByWriterNameId(
final WriterName writerName
) {

List<Curious> curiousList = curiousRetriever.findAllByWriterName(writerName);

curiousList.forEach(curious -> {
Post post = curious.getPost();
post.decreaseCuriousCount();
writerName.decreaseTotalCuriousCount();
});

curiousRepository.deleteAll(curiousList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mile.curious.service;

import com.mile.curious.domain.Curious;
import com.mile.curious.repository.CuriousRepository;
import com.mile.writername.domain.WriterName;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class CuriousRetriever {

private final CuriousRepository curiousRepository;

public List<Curious> findAllByWriterName(final WriterName writerName) {
return curiousRepository.findAllByWriterName(writerName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mile.post.domain.Post;
import com.mile.writername.domain.WriterName;
import com.mile.writername.service.WriterNameService;
import com.mile.writername.service.WriterNameUpdator;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
Expand All @@ -20,13 +21,13 @@
public class CuriousService {

private final CuriousRepository curiousRepository;
private final WriterNameService writerNameService;
private final WriterNameUpdator writerNameUpdator;

public void deleteCurious(final Post post, final WriterName writerName) {
checkCuriousNotExists(post, writerName);
curiousRepository.delete(curiousRepository.findByPostAndWriterName(post, writerName));
post.decreaseCuriousCount();
writerNameService.decreaseTotalCuriousCountByWriterName(writerName);
writerNameUpdator.decreaseTotalCuriousCountByWriterName(writerName);
}

public void checkCuriousNotExists(final Post post, final WriterName writerName) {
Expand All @@ -43,7 +44,7 @@ public void createCurious(final Post post, final WriterName writerName) {
throw new ConflictException(ErrorMessage.CURIOUS_ALREADY_EXISTS_EXCEPTION);
}
post.increaseCuriousCount();
writerNameService.increaseTotalCuriousCountByWriterName(writerName);
writerNameUpdator.increaseTotalCuriousCountByWriterName(writerName);
}

public void checkCuriousExists(final Post post, final WriterName writerName) {
Expand All @@ -68,21 +69,4 @@ public void deleteAllByPosts(
posts.forEach(this::deleteAllByPost);
}

public void deleteAllByWriterNameId(
final Long writerNameId
) {


WriterName writerName = writerNameService.findById(writerNameId);
List<Curious> curiousList = curiousRepository.findAllByWriterName(writerName);

curiousList.forEach(curious -> {
Post post = curious.getPost();

post.decreaseCuriousCount();
writerName.decreaseTotalCuriousCount();
});

curiousRepository.deleteAll(curiousList);
}
}
18 changes: 18 additions & 0 deletions module-domain/src/main/java/com/mile/moim/service/MoimCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mile.moim.service;

import com.mile.moim.domain.Moim;
import com.mile.moim.repository.MoimRepository;
import com.mile.moim.service.dto.MoimCreateRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MoimCreator {

private final MoimRepository moimRepository;

public Moim createMoim(MoimCreateRequest moimCreateRequest) {
return moimRepository.save(Moim.create(moimCreateRequest));
}
}
36 changes: 2 additions & 34 deletions module-domain/src/main/java/com/mile/moim/service/MoimRemover.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
package com.mile.moim.service;

import com.mile.comment.domain.Comment;
import com.mile.comment.service.CommentService;
import com.mile.commentreply.service.CommentReplyService;
import com.mile.curious.service.CuriousService;
import com.mile.moim.domain.Moim;
import com.mile.moim.repository.MoimRepository;
import com.mile.post.domain.Post;
import com.mile.post.service.PostDeleteService;
import com.mile.post.service.PostGetService;
import com.mile.topic.domain.Topic;
import com.mile.topic.service.TopicService;
import com.mile.writername.service.WriterNameDeleteService;
import java.util.List;

import com.mile.writername.service.WriterNameRemover;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand All @@ -25,33 +13,13 @@
@RequiredArgsConstructor
public class MoimRemover {

private final TopicService topicService;
private final PostGetService postGetService;
private final CommentReplyService commentReplyService;
private final PostDeleteService postDeleteService;
private final CommentService commentService;
private final CuriousService curiousService;
private final MoimRepository moimRepository;
private final WriterNameRemover writerNameRemover;
private final MoimRetriever moimRetriever;

@Transactional
public void deleteMoim(
final Long moimId,
final Long userId
final Moim moim
) {
moimRetriever.getAuthenticateOwnerOfMoim(moimId, userId);
Moim moim = moimRetriever.findById(moimId);
List<Topic> topics = topicService.findTopicListByMoimId(moimId);
List<Post> posts = postGetService.findAllByTopics(topics);
List<Comment> comments = commentService.findAllByPosts(posts);
commentReplyService.deleteRepliesByComments(comments);
commentService.deleteComments(posts);
curiousService.deleteAllByPosts(posts);
postDeleteService.deletePostsByTopic(topics);
writerNameRemover.deleteWriterNamesByMoim(moim);
topicService.deleteTopicsByMoim(moim);
writerNameRemover.setWriterNameMoimNull(moim.getOwner());

moimRepository.delete(moim);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
import com.mile.exception.model.NotFoundException;
import com.mile.moim.domain.Moim;
import com.mile.moim.repository.MoimRepository;
import com.mile.moim.service.lock.AtomicValidateUniqueMoimName;
import com.mile.user.domain.User;
import com.mile.user.service.UserService;
import com.mile.writername.service.WriterNameService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
@RequiredArgsConstructor
public class MoimRetriever {

private final WriterNameService writerNameService;
private final MoimRepository moimRepository;
private final UserService userService;

public Moim findById(
final Long moimId
Expand All @@ -29,22 +28,11 @@ public Moim findById(
);
}

public void getAuthenticateOwnerOfMoim(
final Long moimId,
final Long userId
) {
Long writerNameId = writerNameService.getWriterNameIdByMoimIdAndUserId(moimId, userId);
Moim moim = findById(moimId);
if (!moim.getOwner().getId().equals(writerNameId)) {
throw new ForbiddenException(ErrorMessage.MOIM_OWNER_AUTHENTICATION_ERROR);
}
}

public void authenticateOwnerOfMoim(
final Moim moim,
final Long userId
final User user
) {
if (!isMoimOwnerEqualsUser(moim, userService.findById(userId))) {
if (!isMoimOwnerEqualsUser(moim, user)) {
throw new ForbiddenException(ErrorMessage.MOIM_OWNER_AUTHENTICATION_ERROR);
}
}
Expand All @@ -56,4 +44,26 @@ public boolean isMoimOwnerEqualsUser(
return moim.getOwner().getWriter().equals(user);
}

public List<Moim> findBestMoims() {
LocalDateTime endOfWeek = LocalDateTime.now();
LocalDateTime startOfWeek = endOfWeek.minusDays(7);
PageRequest pageRequest = PageRequest.of(0, 2);
return moimRepository.findTop3PublicMoimsWithMostPostsLastWeek(pageRequest, startOfWeek, endOfWeek);
}

public List<Moim> getLatestMoims(int count, List<Moim> excludeMoims) {
PageRequest pageRequest = PageRequest.of(0, count);
if (excludeMoims.isEmpty()) {
return moimRepository.findLatestMoimsWithoutExclusion(pageRequest);
} else {
return moimRepository.findLatestMoimsWithExclusion(pageRequest, excludeMoims);
}
}


@AtomicValidateUniqueMoimName
public boolean checkNormalizeName(final String normalizedName) {
return moimRepository.existsByNormalizedName(normalizedName);
}

}
Loading

0 comments on commit 013d0cd

Please sign in to comment.