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

feat: 장소 등록 시 장소 통계와 함께 등록되는 기능 추가 #479

Merged
merged 12 commits into from
Oct 18, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
import com.now.naaga.like.application.dto.CancelLikeCommand;
import com.now.naaga.like.domain.PlaceLike;
import com.now.naaga.like.domain.PlaceLikeType;
import com.now.naaga.like.exception.PlaceLikeException;
import com.now.naaga.like.exception.PlaceLikeExceptionType;
import com.now.naaga.like.repository.PlaceLikeRepository;
import com.now.naaga.player.application.PlayerService;
import com.now.naaga.player.domain.Player;
import com.now.naaga.placestatistics.application.PlaceStatisticsService;
import com.now.naaga.placestatistics.application.dto.SubtractLikeCommand;
import com.now.naaga.place.application.PlaceStatisticsService;
import com.now.naaga.place.application.dto.SubtractLikeCommand;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Transactional
@Service
public class PlaceLikeService {
Expand Down Expand Up @@ -45,7 +40,7 @@ public void cancelLike(final CancelLikeCommand cancelLikeCommand) {
}

private void subtractPlaceLikeCount(final Long placeId, final PlaceLike placeLike) {
if(placeLike.getType() == PlaceLikeType.LIKE) {
if (placeLike.getType() == PlaceLikeType.LIKE) {
final SubtractLikeCommand subtractLikeCommand = new SubtractLikeCommand(placeId);
placeStatisticsService.subtractLike(subtractLikeCommand);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.now.naaga.place.application;

import com.now.naaga.place.application.dto.CreatePlaceStatisticsCommand;
import com.now.naaga.place.domain.PlaceCreateEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class CreatePlaceStatisticsWithPlaceCreateEventHandler {

private final PlaceStatisticsService placeStatisticsService;

public CreatePlaceStatisticsWithPlaceCreateEventHandler(final PlaceStatisticsService placeStatisticsService) {
this.placeStatisticsService = placeStatisticsService;
}

@Transactional
@EventListener
public void handle(final PlaceCreateEvent placeCreateEvent) {
final Long placeId = placeCreateEvent.placeId();
final CreatePlaceStatisticsCommand command = new CreatePlaceStatisticsCommand(placeId);
placeStatisticsService.createPlaceStatistics(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import com.now.naaga.place.application.dto.FindPlaceByIdCommand;
import com.now.naaga.place.application.dto.RecommendPlaceCommand;
import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.PlaceCreateEvent;
import com.now.naaga.place.domain.PlaceRecommendService;
import com.now.naaga.place.domain.Position;
import com.now.naaga.place.domain.SortType;
import com.now.naaga.place.exception.PlaceException;
import com.now.naaga.place.persistence.repository.PlaceRepository;
import com.now.naaga.place.repository.PlaceRepository;
import com.now.naaga.player.application.PlayerService;
import com.now.naaga.player.domain.Player;
import com.now.naaga.temporaryplace.application.TemporaryPlaceService;
import java.util.List;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,18 +29,18 @@ public class PlaceService {

private final PlayerService playerService;

private final TemporaryPlaceService temporaryPlaceService;

private final PlaceRecommendService placeRecommendService;

private final ApplicationEventPublisher applicationEventPublisher;

public PlaceService(final PlaceRepository placeRepository,
final PlayerService playerService,
final TemporaryPlaceService temporaryPlaceService,
final PlaceRecommendService placeRecommendService) {
final PlaceRecommendService placeRecommendService,
final ApplicationEventPublisher applicationEventPublisher) {
this.placeRepository = placeRepository;
this.playerService = playerService;
this.temporaryPlaceService = temporaryPlaceService;
this.placeRecommendService = placeRecommendService;
this.applicationEventPublisher = applicationEventPublisher;
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -73,7 +74,7 @@ public Place createPlace(final CreatePlaceCommand createPlaceCommand) {
registeredPlayer);

placeRepository.save(place);
temporaryPlaceService.deleteById(createPlaceCommand.temporaryPlaceId());
applicationEventPublisher.publishEvent(new PlaceCreateEvent(createPlaceCommand.temporaryPlaceId(), place.getId()));
return place;
Comment on lines +77 to 78
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정리 겸 for 채채

  1. PlaceCreateEvent(Long temporaryPlaceId, // 임시 장소 삭제를 위함
    Long placeId) // 장소 통계를 만들기 위함
    객체를 생성하고, 해당 객체로 이벤트를 발생시킨다
  2. CreatePlaceStatisticsWithPlaceCreateEventHandler의 @eventlistener가 붙고 매개변수 PlaceCreateEvent를 가진 메서드를 실행시킨다
    = PlaceCreateEvent()객체가 publish되었을 때, @eventlistener가 붙고 PlaceCreateEvent 매개변수를 갖는 메서드가 실행 되는 듯 함.
    장소통계 서비스를 이용한 장소 통계가 생성된다.
  3. DeleteTemporaryPlaceWithPlaceCreateEventHandler도 마찬가지로 동시에 메서드가 실행된다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한가지 정정하겠습니다!

DeleteTemporaryPlaceWithPlaceCreateEventHandler도 마찬가지로 동시에 메서드가 실행된다

동시에 실행되지 않습니다. 비동기 이벤트가 아닌, 동기 이벤트이기 때문에 순서대로 실행됩니다 - !

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.now.naaga.place.application;

import static com.now.naaga.place.domain.PlaceStatistics.LIKE_COUNT_DEFAULT_VALUE;

import com.now.naaga.place.application.dto.CreatePlaceStatisticsCommand;
import com.now.naaga.place.application.dto.FindPlaceByIdCommand;
import com.now.naaga.place.application.dto.SubtractLikeCommand;
import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.PlaceStatistics;
import com.now.naaga.place.exception.PlaceStatisticsException;
import com.now.naaga.place.exception.PlaceStatisticsExceptionType;
import com.now.naaga.place.repository.PlaceStatisticsRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class PlaceStatisticsService {

private final PlaceStatisticsRepository placeStatisticsRepository;

private final PlaceService placeService;

public PlaceStatisticsService(final PlaceStatisticsRepository placeStatisticsRepository,
final PlaceService placeService) {
this.placeStatisticsRepository = placeStatisticsRepository;
this.placeService = placeService;
}

public PlaceStatistics createPlaceStatistics(final CreatePlaceStatisticsCommand createPlaceStatisticsCommand) {
final Long placeId = createPlaceStatisticsCommand.placeId();
final Place place = placeService.findPlaceById(new FindPlaceByIdCommand(placeId));

final PlaceStatistics placeStatistics = new PlaceStatistics(place, LIKE_COUNT_DEFAULT_VALUE);
return placeStatisticsRepository.save(placeStatistics);
}

public void subtractLike(final SubtractLikeCommand subtractLikeCommand) {
final Long placeId = subtractLikeCommand.placeId();

final PlaceStatistics placeStatistics = placeStatisticsRepository.findByPlaceId(placeId)
.orElseThrow(() -> new PlaceStatisticsException(PlaceStatisticsExceptionType.NOT_FOUND));

placeStatistics.subtractLike();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.now.naaga.place.application.dto;

public record CreatePlaceStatisticsCommand(Long placeId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.now.naaga.place.application.dto;

public record SubtractLikeCommand(Long placeId) {
}
10 changes: 8 additions & 2 deletions backend/src/main/java/com/now/naaga/place/domain/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import com.now.naaga.place.exception.PlaceException;
import com.now.naaga.place.exception.PlaceExceptionType;
import com.now.naaga.player.domain.Player;
import jakarta.persistence.*;

import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.util.Objects;

@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.now.naaga.place.domain;

public record PlaceCreateEvent(Long temporaryPlaceId,
Long placeId) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.now.naaga.place.domain;

import com.now.naaga.place.exception.PlaceException;
import com.now.naaga.place.persistence.repository.PlaceRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.now.naaga.place.exception.PlaceExceptionType.NO_EXIST;

import com.now.naaga.place.exception.PlaceException;
import com.now.naaga.place.repository.PlaceRepository;
import java.util.List;
import java.util.Random;

import static com.now.naaga.place.exception.PlaceExceptionType.NO_EXIST;
import org.springframework.stereotype.Service;

@Service
public class PlaceRecommendService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.now.naaga.placestatistics.domain;
package com.now.naaga.place.domain;

import com.now.naaga.common.domain.BaseEntity;
import com.now.naaga.place.domain.Place;
import jakarta.persistence.*;
import org.hibernate.annotations.DialectOverride;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import java.util.Objects;

@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.now.naaga.placestatistics.exception;
package com.now.naaga.place.exception;

import com.now.naaga.common.exception.BaseException;
import com.now.naaga.common.exception.BaseExceptionType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.now.naaga.placestatistics.exception;
package com.now.naaga.place.exception;

import com.now.naaga.common.exception.BaseExceptionType;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.now.naaga.place.persistence.repository;
package com.now.naaga.place.repository;

import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.Position;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.now.naaga.placestatistics.repository;

import com.now.naaga.placestatistics.domain.PlaceStatistics;
import org.springframework.data.jpa.repository.JpaRepository;
package com.now.naaga.place.repository;

import com.now.naaga.place.domain.PlaceStatistics;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PlaceStatisticsRepository extends JpaRepository<PlaceStatistics, Long> {

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.now.naaga.temporaryplace.application;

import com.now.naaga.place.domain.PlaceCreateEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class DeleteTemporaryPlaceWithPlaceCreateEventHandler {

private final TemporaryPlaceService temporaryPlaceService;

public DeleteTemporaryPlaceWithPlaceCreateEventHandler(final TemporaryPlaceService temporaryPlaceService) {
this.temporaryPlaceService = temporaryPlaceService;
}

@Transactional
@EventListener
public void handle(final PlaceCreateEvent placeCreateEvent) {
final Long temporaryPlaceId = placeCreateEvent.temporaryPlaceId();
temporaryPlaceService.deleteById(temporaryPlaceId);
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에 AWS S3에 저장된 사진 파일의 위치를 이동시키는 부분도 필요하겠습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S3 가 어떻게 진행되고 있는지 몰라서 어떤 코멘트인지 잘 모르겠습니다 🥲

수정이 필요하다는 의미이신가요?

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

import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.Position;
import com.now.naaga.place.persistence.repository.PlaceRepository;
import com.now.naaga.place.repository.PlaceRepository;
import com.now.naaga.player.domain.Player;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.now.naaga.common.builder;

import com.now.naaga.place.domain.Place;
import com.now.naaga.placestatistics.domain.PlaceStatistics;
import com.now.naaga.placestatistics.repository.PlaceStatisticsRepository;
import com.now.naaga.place.domain.PlaceStatistics;
import com.now.naaga.place.repository.PlaceStatisticsRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.now.naaga.common.fixture;

import com.now.naaga.placestatistics.domain.PlaceStatistics;
import com.now.naaga.place.domain.PlaceStatistics;

public class PlaceStatisticsFixture {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.now.naaga.like.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.now.naaga.common.builder.PlaceBuilder;
import com.now.naaga.common.builder.PlaceLikeBuilder;
import com.now.naaga.common.builder.PlaceStatisticsBuilder;
import com.now.naaga.common.builder.PlayerBuilder;
import com.now.naaga.common.exception.BaseExceptionType;
import com.now.naaga.like.application.dto.CancelLikeCommand;
import com.now.naaga.like.domain.PlaceLike;
import com.now.naaga.like.domain.PlaceLikeType;
import com.now.naaga.like.exception.PlaceLikeException;
import com.now.naaga.like.repository.PlaceLikeRepository;
import com.now.naaga.place.domain.Place;
import com.now.naaga.placestatistics.domain.PlaceStatistics;
import com.now.naaga.placestatistics.repository.PlaceStatisticsRepository;
import com.now.naaga.place.domain.PlaceStatistics;
import com.now.naaga.place.repository.PlaceStatisticsRepository;
import com.now.naaga.player.domain.Player;
import org.junit.jupiter.api.Assertions;
import java.util.Optional;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -23,13 +25,6 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;

import java.util.Optional;

import static com.now.naaga.like.exception.PlaceLikeExceptionType.NOT_EXIST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Sql("/truncate.sql")
@ActiveProfiles("test")
Expand Down Expand Up @@ -169,7 +164,7 @@ public PlaceLikeServiceTest(final PlaceLikeService placeLikeService,
.build();
final CancelLikeCommand cancelLikeCommand = new CancelLikeCommand(player.getId(), place.getId());

// when & then`
// when & then
assertDoesNotThrow(() -> placeLikeService.cancelLike(cancelLikeCommand));
}
}
Loading
Loading