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 @@ -9,15 +9,16 @@
import com.now.naaga.place.application.dto.RecommendPlaceCommand;
import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.PlaceCheckService;
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.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 @@ -29,22 +30,22 @@ public class PlaceService {

private final PlayerService playerService;

private final TemporaryPlaceService temporaryPlaceService;

private final PlaceCheckService placeCheckService;

private final PlaceRecommendService placeRecommendService;

private final ApplicationEventPublisher applicationEventPublisher;

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

@Transactional(readOnly = true)
Expand Down Expand Up @@ -80,7 +81,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,5 @@
package com.now.naaga.place.domain;

public record PlaceCreateEvent(Long temporaryPlaceId,
Long placeId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.now.naaga.placestatistics.application;

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

@Component
public class CreatePlaceStatisticsWithPlaceCreateEventHandler {

private final PlaceStatisticsService placeStatisticsService;

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

@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
@@ -1,10 +1,16 @@
package com.now.naaga.placestatistics.application;

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

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

Expand All @@ -14,15 +20,27 @@ public class PlaceStatisticsService {

private final PlaceStatisticsRepository placeStatisticsRepository;

public PlaceStatisticsService(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));
.orElseThrow(() -> new PlaceStatisticsException(PlaceStatisticsExceptionType.NOT_FOUND));

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

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

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

@Component
public class DeleteTemporaryPlaceWithPlaceCreateEventHandler {

private final TemporaryPlaceService temporaryPlaceService;

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

@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 @@ -13,9 +13,12 @@
import com.now.naaga.place.domain.Position;
import com.now.naaga.place.exception.PlaceException;
import com.now.naaga.place.exception.PlaceExceptionType;
import com.now.naaga.placestatistics.domain.PlaceStatistics;
import com.now.naaga.placestatistics.repository.PlaceStatisticsRepository;
import com.now.naaga.player.domain.Player;
import com.now.naaga.temporaryplace.domain.TemporaryPlace;
import com.now.naaga.temporaryplace.repository.TemporaryPlaceRepository;
import java.util.Optional;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
Expand All @@ -35,6 +38,9 @@ class PlaceServiceTest {
@Autowired
private TemporaryPlaceRepository temporaryPlaceRepository;

@Autowired
private PlaceStatisticsRepository placeStatisticsRepository;

@Autowired
private PlaceService placeService;

Expand Down Expand Up @@ -87,6 +93,32 @@ class PlaceServiceTest {
});
}

@Test
void 장소_등록_시_장소_통계도_함께_등록된다() {
// given
final Player player = playerBuilder.init()
.build();

final TemporaryPlace temporaryPlace = temporaryPlaceBuilder.init()
.build();

final Long temporaryPlaceId = temporaryPlace.getId();

final CreatePlaceCommand createPlaceCommand = new CreatePlaceCommand("루터회관",
"이곳은 루터회관이다 알겠냐",
Position.of(1.23, 4.56),
"image/url",
player.getId(),
temporaryPlaceId);

// when
final Place place = placeService.createPlace(createPlaceCommand);

// then
final Optional<PlaceStatistics> placeStatistics = placeStatisticsRepository.findByPlaceId(place.getId());
assertThat(placeStatistics).isNotEmpty();
}

@Test
void 장소_등록_시_주변_반경_20M_내에_등록된_장소가_존재한다면_예외가_발생한다() {
// given
Expand All @@ -112,4 +144,4 @@ class PlaceServiceTest {
// then
assertThat(baseExceptionType).isEqualTo(PlaceExceptionType.ALREADY_EXIST_NEARBY);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,67 @@
package com.now.naaga.placestatistics.application;

import static org.assertj.core.api.Assertions.assertThat;

import com.now.naaga.common.builder.PlaceBuilder;
import com.now.naaga.common.builder.PlaceStatisticsBuilder;
import com.now.naaga.place.domain.Place;
import com.now.naaga.placestatistics.application.dto.CreatePlaceStatisticsCommand;
import com.now.naaga.placestatistics.application.dto.SubtractLikeCommand;
import com.now.naaga.placestatistics.domain.PlaceStatistics;
import com.now.naaga.placestatistics.repository.PlaceStatisticsRepository;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
@Sql("/truncate.sql")
@ActiveProfiles("test")
@SpringBootTest
class PlaceStatisticsServiceTest {

PlaceStatisticsService placeStatisticsService;
@Autowired
private PlaceStatisticsRepository placeStatisticsRepository;

PlaceStatisticsBuilder placeStatisticsBuilder;
@Autowired
private PlaceStatisticsService placeStatisticsService;

PlaceStatisticsRepository placeStatisticsRepository;
@Autowired
private PlaceStatisticsBuilder placeStatisticsBuilder;

@Autowired
public PlaceStatisticsServiceTest(final PlaceStatisticsService placeStatisticsService,
final PlaceStatisticsBuilder placeStatisticsBuilder,
final PlaceStatisticsRepository placeStatisticsRepository) {
this.placeStatisticsService = placeStatisticsService;
this.placeStatisticsBuilder = placeStatisticsBuilder;
this.placeStatisticsRepository = placeStatisticsRepository;
private PlaceBuilder placeBuilder;

@Transactional
@Test
void 장소통계를_생성한다() {
// given
final Place place = placeBuilder.init()
.build();

// when
final CreatePlaceStatisticsCommand command = new CreatePlaceStatisticsCommand(place.getId());
final PlaceStatistics actual = placeStatisticsService.createPlaceStatistics(command);

// then
final PlaceStatistics expected = new PlaceStatistics(place, PlaceStatistics.LIKE_COUNT_DEFAULT_VALUE);
assertThat(actual).usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expected);
}

@Test
void 장소통계_1개를_줄인다() {
//given
final long beforeLikeCount = 10L;
final PlaceStatistics placeStatistics = placeStatisticsBuilder.init()
.likeCount(beforeLikeCount)
.build();
.likeCount(beforeLikeCount)
.build();
final Place place = placeStatistics.getPlace();

//when
Expand Down