From 9d9f8f530e1bdd99c9b78915da565bf576a0ceb6 Mon Sep 17 00:00:00 2001 From: 5uhwann <106325839+5uhwann@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:42:12 +0900 Subject: [PATCH] =?UTF-8?q?[DDING-3]=20ScoreHistory=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20(#138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/AdminScoreHistoryApi.java | 10 +-- .../scorehistory/api/ClubScoreHistoryApi.java | 4 +- .../AdminScoreHistoryController.java | 25 +++--- .../ClubScoreHistoryController.java | 23 +++-- .../request/CreateScoreHistoryRequest.java | 11 ++- ...nse.java => ScoreHistoryListResponse.java} | 15 ++-- .../FacadeAdminScoreHistoryService.java | 12 +++ .../FacadeAdminScoreHistoryServiceImpl.java | 43 ++++++++++ .../FacadeClubScoreHistoryService.java | 9 ++ .../FacadeClubScoreHistoryServiceImpl.java | 27 ++++++ .../service/GeneralScoreHistoryService.java | 28 +++++++ .../service/ScoreHistoryService.java | 39 +-------- .../command/CreateScoreHistoryCommand.java | 25 ++++++ .../query/AdminClubScoreHistoryListQuery.java | 20 +++++ .../dto/query/ClubScoreHistoryListQuery.java | 22 +++++ src/main/resources/logback-prod.xml | 2 +- .../common/support/WebApiUnitTestSupport.java | 5 -- .../AdminScoreHistoryControllerUnitTest.java | 58 ------------- .../ClubScoreHistoryControllerUnitTest.java | 59 ------------- ...acadeAdminScoreHistoryServiceImplTest.java | 84 +++++++++++++++++++ ...FacadeClubScoreHistoryServiceImplTest.java | 60 +++++++++++++ 21 files changed, 377 insertions(+), 204 deletions(-) rename src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/{ScoreHistoryFilterByClubResponse.java => ScoreHistoryListResponse.java} (78%) create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryService.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImpl.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryService.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImpl.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/GeneralScoreHistoryService.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/command/CreateScoreHistoryCommand.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/AdminClubScoreHistoryListQuery.java create mode 100644 src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/ClubScoreHistoryListQuery.java delete mode 100644 src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryControllerUnitTest.java delete mode 100644 src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryControllerUnitTest.java create mode 100644 src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImplTest.java create mode 100644 src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImplTest.java diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/AdminScoreHistoryApi.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/AdminScoreHistoryApi.java index 41474be2..8db0ce59 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/AdminScoreHistoryApi.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/AdminScoreHistoryApi.java @@ -2,7 +2,7 @@ import ddingdong.ddingdongBE.common.exception.ErrorResponse; import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.CreateScoreHistoryRequest; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.ExampleObject; @@ -55,14 +55,14 @@ public interface AdminScoreHistoryApi { @PostMapping @ResponseStatus(HttpStatus.CREATED) @SecurityRequirement(name = "AccessToken") - void register(@PathVariable Long clubId, @Valid @RequestBody CreateScoreHistoryRequest createScoreHistoryRequest); + void createScoreHistory(@PathVariable Long clubId, @Valid @RequestBody CreateScoreHistoryRequest createScoreHistoryRequest); @Operation(summary = "어드민 동아리 점수 내역 목록 조회 API") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "점수 변동 내역 목록 조회 성공", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, - schema = @Schema(implementation = ScoreHistoryFilterByClubResponse.class))), + schema = @Schema(implementation = ScoreHistoryListResponse.class))), @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, @@ -75,7 +75,7 @@ public interface AdminScoreHistoryApi { "message": "존재하지 않는 동아리입니다.", "timestamp": "2024-08-22T00:08:46.990585" } - """ + """ ) }) ) @@ -83,6 +83,6 @@ public interface AdminScoreHistoryApi { @GetMapping @ResponseStatus(HttpStatus.OK) @SecurityRequirement(name = "AccessToken") - ScoreHistoryFilterByClubResponse findAllScoreHistories(@PathVariable Long clubId); + ScoreHistoryListResponse findClubScoreHistories(@PathVariable Long clubId); } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/ClubScoreHistoryApi.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/ClubScoreHistoryApi.java index fb8c9c7f..21ac2e36 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/ClubScoreHistoryApi.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/api/ClubScoreHistoryApi.java @@ -1,7 +1,7 @@ package ddingdong.ddingdongBE.domain.scorehistory.api; import ddingdong.ddingdongBE.auth.PrincipalDetails; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; @@ -19,6 +19,6 @@ public interface ClubScoreHistoryApi { @GetMapping @ResponseStatus(HttpStatus.OK) @SecurityRequirement(name = "AccessToken") - ScoreHistoryFilterByClubResponse findMyScoreHistories(@AuthenticationPrincipal PrincipalDetails principalDetails); + ScoreHistoryListResponse findMyScoreHistories(@AuthenticationPrincipal PrincipalDetails principalDetails); } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryController.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryController.java index 3f479d39..f47c2dc6 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryController.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryController.java @@ -1,12 +1,11 @@ package ddingdong.ddingdongBE.domain.scorehistory.controller; -import ddingdong.ddingdongBE.domain.club.entity.Club; -import ddingdong.ddingdongBE.domain.club.service.ClubService; import ddingdong.ddingdongBE.domain.scorehistory.api.AdminScoreHistoryApi; import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.CreateScoreHistoryRequest; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse.ScoreHistoryResponse; -import ddingdong.ddingdongBE.domain.scorehistory.service.ScoreHistoryService; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse.ScoreHistoryResponse; +import ddingdong.ddingdongBE.domain.scorehistory.service.FacadeAdminScoreHistoryService; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.RestController; @@ -15,18 +14,14 @@ @RequiredArgsConstructor public class AdminScoreHistoryController implements AdminScoreHistoryApi { - private final ClubService clubService; - private final ScoreHistoryService scoreHistoryService; + private final FacadeAdminScoreHistoryService facadeAdminScoreHistoryService; - public void register(Long clubId, CreateScoreHistoryRequest createScoreHistoryRequest) { - scoreHistoryService.create(clubId, createScoreHistoryRequest); + public void createScoreHistory(Long clubId, CreateScoreHistoryRequest createScoreHistoryRequest) { + facadeAdminScoreHistoryService.create(createScoreHistoryRequest.toCommand(clubId)); } - public ScoreHistoryFilterByClubResponse findAllScoreHistories(Long clubId) { - Club club = clubService.getByClubId(clubId); - List scoreHistoryResponses = scoreHistoryService.findAllByClubId(clubId).stream() - .map(ScoreHistoryResponse::from) - .toList(); - return ScoreHistoryFilterByClubResponse.of(club, scoreHistoryResponses); + public ScoreHistoryListResponse findClubScoreHistories(Long clubId) { + AdminClubScoreHistoryListQuery query = facadeAdminScoreHistoryService.findAllByClubId(clubId); + return ScoreHistoryListResponse.of(query); } } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryController.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryController.java index 38f36fda..e6cdc39c 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryController.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryController.java @@ -1,12 +1,12 @@ package ddingdong.ddingdongBE.domain.scorehistory.controller; import ddingdong.ddingdongBE.auth.PrincipalDetails; -import ddingdong.ddingdongBE.domain.club.entity.Club; -import ddingdong.ddingdongBE.domain.club.service.ClubService; import ddingdong.ddingdongBE.domain.scorehistory.api.ClubScoreHistoryApi; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryFilterByClubResponse.ScoreHistoryResponse; -import ddingdong.ddingdongBE.domain.scorehistory.service.ScoreHistoryService; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse; +import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.response.ScoreHistoryListResponse.ScoreHistoryResponse; +import ddingdong.ddingdongBE.domain.scorehistory.service.FacadeClubScoreHistoryService; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.ClubScoreHistoryListQuery; +import ddingdong.ddingdongBE.domain.user.entity.User; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.RestController; @@ -15,15 +15,14 @@ @RequiredArgsConstructor public class ClubScoreHistoryController implements ClubScoreHistoryApi { - private final ClubService clubService; - private final ScoreHistoryService scoreHistoryService; + private final FacadeClubScoreHistoryService facadeClubScoreHistoryService; - public ScoreHistoryFilterByClubResponse findMyScoreHistories(PrincipalDetails principalDetails) { - Club club = clubService.getByUserId(principalDetails.getUser().getId()); - List scoreHistoryResponses = scoreHistoryService.findAllByClubId(club.getId()) - .stream() + public ScoreHistoryListResponse findMyScoreHistories(PrincipalDetails principalDetails) { + User user = principalDetails.getUser(); + ClubScoreHistoryListQuery query = facadeClubScoreHistoryService.findMyScoreHistories(user.getId()); + List scoreHistoryResponses = query.scoreHistories().stream() .map(ScoreHistoryResponse::from) .toList(); - return ScoreHistoryFilterByClubResponse.of(club, scoreHistoryResponses); + return ScoreHistoryListResponse.of(query.club(), scoreHistoryResponses); } } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/request/CreateScoreHistoryRequest.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/request/CreateScoreHistoryRequest.java index e3f0e29e..c7ea85b6 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/request/CreateScoreHistoryRequest.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/request/CreateScoreHistoryRequest.java @@ -1,11 +1,10 @@ package ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request; -import ddingdong.ddingdongBE.domain.club.entity.Club; import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory; -import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.command.CreateScoreHistoryCommand; import io.swagger.v3.oas.annotations.media.Schema; -import java.math.BigDecimal; import jakarta.validation.constraints.NotNull; +import java.math.BigDecimal; import lombok.Builder; @Schema( @@ -30,9 +29,9 @@ public record CreateScoreHistoryRequest( BigDecimal amount ) { - public ScoreHistory toEntity(Club club) { - return ScoreHistory.builder() - .club(club) + public CreateScoreHistoryCommand toCommand(Long clubId) { + return CreateScoreHistoryCommand.builder() + .clubId(clubId) .amount(amount) .scoreCategory(ScoreCategory.from(scoreCategory)) .reason(reason) diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryFilterByClubResponse.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryListResponse.java similarity index 78% rename from src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryFilterByClubResponse.java rename to src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryListResponse.java index 9950e94d..6753c5fc 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryFilterByClubResponse.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/controller/dto/response/ScoreHistoryListResponse.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import ddingdong.ddingdongBE.domain.club.entity.Club; import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Schema; import java.math.BigDecimal; @@ -15,7 +16,7 @@ description = "어드민 - 동아리 점수 변동 내역 목록 응답" ) @Builder -public record ScoreHistoryFilterByClubResponse( +public record ScoreHistoryListResponse( @Schema(description = "동아리 총 점수", example = "50") BigDecimal totalScore, @@ -23,10 +24,14 @@ public record ScoreHistoryFilterByClubResponse( List scoreHistories ) { - public static ScoreHistoryFilterByClubResponse of(Club club, List scoreHistories) { - return ScoreHistoryFilterByClubResponse.builder() - .totalScore(club.getScore().getValue()) - .scoreHistories(scoreHistories) + public static ScoreHistoryListResponse of(AdminClubScoreHistoryListQuery query) { + return ScoreHistoryListResponse.builder() + .totalScore(query.club().getScore().getValue()) + .scoreHistories( + query.scoreHistories().stream() + .map(ScoreHistoryResponse::from) + .toList() + ) .build(); } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryService.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryService.java new file mode 100644 index 00000000..d5a5c355 --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryService.java @@ -0,0 +1,12 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.command.CreateScoreHistoryCommand; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; + +public interface FacadeAdminScoreHistoryService { + + Long create(CreateScoreHistoryCommand command); + + AdminClubScoreHistoryListQuery findAllByClubId(Long clubId); + +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImpl.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImpl.java new file mode 100644 index 00000000..a50a99de --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImpl.java @@ -0,0 +1,43 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.club.service.ClubService; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.command.CreateScoreHistoryCommand; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class FacadeAdminScoreHistoryServiceImpl implements FacadeAdminScoreHistoryService { + + private final ScoreHistoryService scoreHistoryService; + private final ClubService clubService; + + @Override + @Transactional + public Long create(CreateScoreHistoryCommand command) { + BigDecimal score = roundToThirdPoint(command.amount()); + Club club = clubService.getByClubId(command.clubId()); + clubService.updateClubScore(club.getId(), score); + return scoreHistoryService.create(command.toEntity(club)); + } + + @Override + public AdminClubScoreHistoryListQuery findAllByClubId(Long clubId) { + Club club = clubService.getByClubId(clubId); + List scoreHistories = scoreHistoryService.findAllByClubId(clubId); + return AdminClubScoreHistoryListQuery.of(club, scoreHistories); + } + + private BigDecimal roundToThirdPoint(BigDecimal value) { + return value.setScale(3, RoundingMode.DOWN); + } + +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryService.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryService.java new file mode 100644 index 00000000..fc42f4e6 --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryService.java @@ -0,0 +1,9 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.ClubScoreHistoryListQuery; + +public interface FacadeClubScoreHistoryService { + + ClubScoreHistoryListQuery findMyScoreHistories(Long userId); + +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImpl.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImpl.java new file mode 100644 index 00000000..d7267455 --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImpl.java @@ -0,0 +1,27 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.club.service.ClubService; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.ClubScoreHistoryListQuery; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class FacadeClubScoreHistoryServiceImpl implements FacadeClubScoreHistoryService { + + private final ScoreHistoryService scoreHistoryService; + private final ClubService clubService; + + @Override + public ClubScoreHistoryListQuery findMyScoreHistories(Long userId) { + Club club = clubService.getByUserId(userId); + List scoreHistories = scoreHistoryService.findAllByClubId(club.getId()); + return ClubScoreHistoryListQuery.of(club, scoreHistories); + } +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/GeneralScoreHistoryService.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/GeneralScoreHistoryService.java new file mode 100644 index 00000000..6623794d --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/GeneralScoreHistoryService.java @@ -0,0 +1,28 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.repository.ScoreHistoryRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class GeneralScoreHistoryService implements ScoreHistoryService { + + private final ScoreHistoryRepository scoreHistoryRepository; + + @Override + @Transactional + public Long create(ScoreHistory scoreHistory) { + ScoreHistory savedScoreHistory = scoreHistoryRepository.save(scoreHistory); + return savedScoreHistory.getId(); + } + + @Override + public List findAllByClubId(Long clubId) { + return scoreHistoryRepository.findByClubId(clubId); + } +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/ScoreHistoryService.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/ScoreHistoryService.java index d7472271..4d1b9758 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/ScoreHistoryService.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/ScoreHistoryService.java @@ -1,45 +1,12 @@ package ddingdong.ddingdongBE.domain.scorehistory.service; -import ddingdong.ddingdongBE.domain.club.entity.Club; -import ddingdong.ddingdongBE.domain.club.service.ClubService; -import ddingdong.ddingdongBE.domain.scorehistory.controller.dto.request.CreateScoreHistoryRequest; import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; -import ddingdong.ddingdongBE.domain.scorehistory.repository.ScoreHistoryRepository; -import java.math.BigDecimal; -import java.math.RoundingMode; import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -@Service -@RequiredArgsConstructor -@Transactional -public class ScoreHistoryService { +public interface ScoreHistoryService { - private final ScoreHistoryRepository scoreHistoryRepository; - private final ClubService clubService; + Long create(ScoreHistory scoreHistory); - public void create(final Long clubId, CreateScoreHistoryRequest createScoreHistoryRequest) { - Club club = clubService.getByClubId(clubId); + List findAllByClubId(Long clubId); - BigDecimal score = roundToThirdPoint(createScoreHistoryRequest.amount()); - clubService.updateClubScore(clubId, score); - scoreHistoryRepository.save(createScoreHistoryRequest.toEntity(club)); - } - - @Transactional(readOnly = true) - public List findAllByClubId(final Long clubId) { - return scoreHistoryRepository.findByClubId(clubId); - } - - @Transactional(readOnly = true) - public List findAllByUserId(final Long userId) { - Club club = clubService.getByUserId(userId); - return scoreHistoryRepository.findByClubId(club.getId()); - } - - private BigDecimal roundToThirdPoint(BigDecimal value) { - return value.setScale(3, RoundingMode.DOWN); - } } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/command/CreateScoreHistoryCommand.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/command/CreateScoreHistoryCommand.java new file mode 100644 index 00000000..fa554c72 --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/command/CreateScoreHistoryCommand.java @@ -0,0 +1,25 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service.dto.command; + +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import java.math.BigDecimal; +import lombok.Builder; + +@Builder +public record CreateScoreHistoryCommand( + Long clubId, + ScoreCategory scoreCategory, + String reason, + BigDecimal amount +) { + + public ScoreHistory toEntity(Club club) { + return ScoreHistory.builder() + .club(club) + .amount(amount) + .scoreCategory(scoreCategory) + .reason(reason) + .build(); + } +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/AdminClubScoreHistoryListQuery.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/AdminClubScoreHistoryListQuery.java new file mode 100644 index 00000000..565c6c6f --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/AdminClubScoreHistoryListQuery.java @@ -0,0 +1,20 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service.dto.query; + +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import java.util.List; +import lombok.Builder; + +@Builder +public record AdminClubScoreHistoryListQuery( + Club club, + List scoreHistories +) { + + public static AdminClubScoreHistoryListQuery of(Club club, List scoreHistories) { + return AdminClubScoreHistoryListQuery.builder() + .club(club) + .scoreHistories(scoreHistories) + .build(); + } +} diff --git a/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/ClubScoreHistoryListQuery.java b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/ClubScoreHistoryListQuery.java new file mode 100644 index 00000000..93b85baf --- /dev/null +++ b/src/main/java/ddingdong/ddingdongBE/domain/scorehistory/service/dto/query/ClubScoreHistoryListQuery.java @@ -0,0 +1,22 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service.dto.query; + +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import java.util.List; +import lombok.Builder; + +@Builder +public record ClubScoreHistoryListQuery( + Club club, + List scoreHistories +) { + + public static ClubScoreHistoryListQuery of( + Club club, + List scoreHistories) { + return ClubScoreHistoryListQuery.builder() + .club(club) + .scoreHistories(scoreHistories) + .build(); + } +} diff --git a/src/main/resources/logback-prod.xml b/src/main/resources/logback-prod.xml index a146529d..36d811f1 100644 --- a/src/main/resources/logback-prod.xml +++ b/src/main/resources/logback-prod.xml @@ -3,7 +3,7 @@ logs/all/logFile.log - logs/all/logFile.%d{yyyy-MM-dd_HH}.log + logs/all/logFile.%d{yyyy-MM-dd}.log 30 100MB diff --git a/src/test/java/ddingdong/ddingdongBE/common/support/WebApiUnitTestSupport.java b/src/test/java/ddingdong/ddingdongBE/common/support/WebApiUnitTestSupport.java index b1124821..b5db0383 100644 --- a/src/test/java/ddingdong/ddingdongBE/common/support/WebApiUnitTestSupport.java +++ b/src/test/java/ddingdong/ddingdongBE/common/support/WebApiUnitTestSupport.java @@ -13,8 +13,6 @@ import ddingdong.ddingdongBE.domain.fileinformation.service.FileInformationService; import ddingdong.ddingdongBE.domain.question.service.GeneralQuestionService; import ddingdong.ddingdongBE.domain.scorehistory.controller.AdminScoreHistoryController; -import ddingdong.ddingdongBE.domain.scorehistory.controller.ClubScoreHistoryController; -import ddingdong.ddingdongBE.domain.scorehistory.service.ScoreHistoryService; import ddingdong.ddingdongBE.file.service.FileService; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; @@ -30,7 +28,6 @@ AdminDocumentController.class, DocumentController.class, AdminScoreHistoryController.class, - ClubScoreHistoryController.class }) public abstract class WebApiUnitTestSupport { @@ -52,8 +49,6 @@ public abstract class WebApiUnitTestSupport { protected GeneralQuestionService generalQuestionService; @MockBean protected ClubService clubService; - @MockBean - protected ScoreHistoryService scoreHistoryService; @Autowired protected ObjectMapper objectMapper; diff --git a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryControllerUnitTest.java b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryControllerUnitTest.java deleted file mode 100644 index 31f90fab..00000000 --- a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/AdminScoreHistoryControllerUnitTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package ddingdong.ddingdongBE.domain.scorehistory.controller; - -import static ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory.ACTIVITY_REPORT; -import static ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory.CARRYOVER_SCORE; -import static org.hamcrest.Matchers.hasSize; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.when; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import ddingdong.ddingdongBE.domain.club.entity.Club; -import ddingdong.ddingdongBE.domain.scorehistory.entity.Score; -import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; -import ddingdong.ddingdongBE.common.support.WebApiUnitTestSupport; -import ddingdong.ddingdongBE.common.support.WithMockAuthenticatedUser; -import java.math.BigDecimal; -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -class AdminScoreHistoryControllerUnitTest extends WebApiUnitTestSupport { - - @WithMockAuthenticatedUser(role = "ADMIN") - @DisplayName("동아리 점수 내역 조회 요청을 수행한다.") - @Test - void findAllScoreHistories() throws Exception { - //given - Club club = Club.builder() - .id(1L) - .score(Score.from(new BigDecimal(55))).build(); - List scoreHistories = List.of(ScoreHistory.builder() - .club(club) - .scoreCategory(CARRYOVER_SCORE) - .amount(new BigDecimal(5)) - .reason("reasonA").build(), - ScoreHistory.builder() - .club(club) - .scoreCategory(ACTIVITY_REPORT) - .amount(new BigDecimal(5)) - .reason("reasonB").build()); - when(clubService.getByClubId(anyLong())).thenReturn(club); - when(scoreHistoryService.findAllByClubId(club.getId())).thenReturn(scoreHistories); - - //when //then - mockMvc.perform(get("/server/admin/{clubId}/score", 1L) - .with(csrf())) - .andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.totalScore").value(55)) - .andExpect(jsonPath("$.scoreHistories", hasSize(scoreHistories.size()))) - .andExpect(jsonPath("$.scoreHistories[0].scoreCategory").value(CARRYOVER_SCORE.getCategory())) - .andExpect(jsonPath("$.scoreHistories[0].reason").value("reasonA")) - .andExpect(jsonPath("$.scoreHistories[0].amount").value(5)); - } -} diff --git a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryControllerUnitTest.java b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryControllerUnitTest.java deleted file mode 100644 index 74ee0d5d..00000000 --- a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/controller/ClubScoreHistoryControllerUnitTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package ddingdong.ddingdongBE.domain.scorehistory.controller; - -import static ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory.ACTIVITY_REPORT; -import static ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory.CARRYOVER_SCORE; -import static org.hamcrest.Matchers.hasSize; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.when; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import ddingdong.ddingdongBE.domain.club.entity.Club; -import ddingdong.ddingdongBE.domain.scorehistory.entity.Score; -import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; -import ddingdong.ddingdongBE.common.support.WebApiUnitTestSupport; -import ddingdong.ddingdongBE.common.support.WithMockAuthenticatedUser; -import java.math.BigDecimal; -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -class ClubScoreHistoryControllerUnitTest extends WebApiUnitTestSupport { - - @WithMockAuthenticatedUser(role = "CLUB") - @DisplayName("동아리 - 내 점수 내역 조회 요청을 수행한다.") - @Test - void getScoreHistories() throws Exception { - //given - Club club = Club.builder() - .id(1L) - .score(Score.from(new BigDecimal(55))).build(); - List scoreHistories = List.of(ScoreHistory.builder() - .club(club) - .scoreCategory(CARRYOVER_SCORE) - .amount(new BigDecimal(5)) - .reason("reasonA").build(), - ScoreHistory.builder() - .club(club) - .scoreCategory(ACTIVITY_REPORT) - .amount(new BigDecimal(5)) - .reason("reasonB").build()); - when(clubService.getByUserId(anyLong())).thenReturn(club); - when(scoreHistoryService.findAllByClubId(club.getId())).thenReturn(scoreHistories); - - //when //then - mockMvc.perform(get("/server/club/my/score") - .with(csrf())) - .andDo(print()) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.totalScore").value(55)) - .andExpect(jsonPath("$.scoreHistories", hasSize(scoreHistories.size()))) - .andExpect(jsonPath("$.scoreHistories[0].scoreCategory").value(CARRYOVER_SCORE.getCategory())) - .andExpect(jsonPath("$.scoreHistories[0].reason").value("reasonA")) - .andExpect(jsonPath("$.scoreHistories[0].amount").value(5)); - } - -} diff --git a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImplTest.java b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImplTest.java new file mode 100644 index 00000000..88c562d0 --- /dev/null +++ b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeAdminScoreHistoryServiceImplTest.java @@ -0,0 +1,84 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + + +import static org.assertj.core.api.Assertions.assertThat; + +import com.navercorp.fixturemonkey.FixtureMonkey; +import ddingdong.ddingdongBE.common.support.FixtureMonkeyFactory; +import ddingdong.ddingdongBE.common.support.TestContainerSupport; +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.club.repository.ClubRepository; +import ddingdong.ddingdongBE.domain.scorehistory.entity.Score; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreCategory; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.repository.ScoreHistoryRepository; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.command.CreateScoreHistoryCommand; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; +import java.math.BigDecimal; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class FacadeAdminScoreHistoryServiceImplTest extends TestContainerSupport { + + @Autowired + private FacadeAdminScoreHistoryService facadeAdminScoreHistoryService; + @Autowired + private ScoreHistoryRepository scoreHistoryRepository; + @Autowired + private ClubRepository clubRepository; + private final FixtureMonkey fixtureMonkey = FixtureMonkeyFactory.getNotNullBuilderIntrospectorMonkey(); + + @DisplayName("어드민: ScoreHistory 생성") + @Test + void findMyScoreHistories() { + //given + Club club = fixtureMonkey.giveMeBuilder(Club.class) + .setNull("user") + .set("score", Score.from(BigDecimal.ZERO)) + .sample(); + Club savedClub = clubRepository.save(club); + + CreateScoreHistoryCommand command = CreateScoreHistoryCommand.builder() + .clubId(savedClub.getId()) + .scoreCategory(ScoreCategory.BUSINESS_PARTICIPATION) + .reason("test") + .amount(BigDecimal.ONE) + .build(); + + // When + Long createdScoreHistoryId = facadeAdminScoreHistoryService.create(command); + + // Then + ScoreHistory scoreHistory = scoreHistoryRepository.findById(createdScoreHistoryId).orElseThrow(); + assertThat(scoreHistory).isNotNull(); + assertThat(scoreHistory.getReason()).isEqualTo("test"); + assertThat(scoreHistory.getScoreCategory()).isEqualTo(ScoreCategory.BUSINESS_PARTICIPATION); + } + + @DisplayName("어드민: ScoreHistory 리스트 조회") + @Test + void findAllByClubId() { + //given + Club club = fixtureMonkey.giveMeBuilder(Club.class) + .setNull("user") + .set("score", Score.from(BigDecimal.ZERO)) + .sample(); + Club savedClub = clubRepository.save(club); + List questions = fixtureMonkey.giveMeBuilder(ScoreHistory.class) + .set("club", savedClub) + .set("amount", BigDecimal.ONE) + .sampleList(5); + scoreHistoryRepository.saveAll(questions); + + // When + AdminClubScoreHistoryListQuery result = facadeAdminScoreHistoryService.findAllByClubId(savedClub.getId()); + + // Then + assertThat(result.scoreHistories()).hasSize(5); + } + +} diff --git a/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImplTest.java b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImplTest.java new file mode 100644 index 00000000..4bc9cb37 --- /dev/null +++ b/src/test/java/ddingdong/ddingdongBE/domain/scorehistory/service/FacadeClubScoreHistoryServiceImplTest.java @@ -0,0 +1,60 @@ +package ddingdong.ddingdongBE.domain.scorehistory.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.navercorp.fixturemonkey.FixtureMonkey; +import ddingdong.ddingdongBE.common.support.FixtureMonkeyFactory; +import ddingdong.ddingdongBE.common.support.TestContainerSupport; +import ddingdong.ddingdongBE.domain.club.entity.Club; +import ddingdong.ddingdongBE.domain.club.repository.ClubRepository; +import ddingdong.ddingdongBE.domain.scorehistory.entity.Score; +import ddingdong.ddingdongBE.domain.scorehistory.entity.ScoreHistory; +import ddingdong.ddingdongBE.domain.scorehistory.repository.ScoreHistoryRepository; +import ddingdong.ddingdongBE.domain.scorehistory.service.dto.query.AdminClubScoreHistoryListQuery; +import ddingdong.ddingdongBE.domain.user.entity.User; +import ddingdong.ddingdongBE.domain.user.repository.UserRepository; +import java.math.BigDecimal; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class FacadeClubScoreHistoryServiceImplTest extends TestContainerSupport { + + @Autowired + private FacadeAdminScoreHistoryService facadeAdminScoreHistoryService; + @Autowired + private ScoreHistoryRepository scoreHistoryRepository; + @Autowired + private UserRepository userRepository; + @Autowired + private ClubRepository clubRepository; + private final FixtureMonkey fixtureMonkey = FixtureMonkeyFactory.getNotNullBuilderIntrospectorMonkey(); + + @Test + @DisplayName("동아리: ScoreHistory 리스트 조회") + void getAllQuestions() { + //given + User user = fixtureMonkey.giveMeOne(User.class); + User savedUser = userRepository.save(user); + Club club = fixtureMonkey.giveMeBuilder(Club.class) + .set("user", savedUser) + .set("score", Score.from(BigDecimal.ZERO)) + .sample(); + Club savedClub = clubRepository.save(club); + List questions = fixtureMonkey.giveMeBuilder(ScoreHistory.class) + .set("club", savedClub) + .set("amount", BigDecimal.ONE) + .sampleList(5); + scoreHistoryRepository.saveAll(questions); + + // When + AdminClubScoreHistoryListQuery result = facadeAdminScoreHistoryService.findAllByClubId(savedUser.getId()); + + // Then + assertThat(result.scoreHistories()).hasSize(5); + } + +}