Skip to content

Commit

Permalink
[DDING-3] ScoreHistory 도메인 리팩토링 적용 (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
5uhwann authored Oct 10, 2024
1 parent c07cae3 commit 9d9f8f5
Show file tree
Hide file tree
Showing 21 changed files with 377 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -75,14 +75,14 @@ public interface AdminScoreHistoryApi {
"message": "존재하지 않는 동아리입니다.",
"timestamp": "2024-08-22T00:08:46.990585"
}
"""
"""
)
})
)
})
@GetMapping
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "AccessToken")
ScoreHistoryFilterByClubResponse findAllScoreHistories(@PathVariable Long clubId);
ScoreHistoryListResponse findClubScoreHistories(@PathVariable Long clubId);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +19,6 @@ public interface ClubScoreHistoryApi {
@GetMapping
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "AccessToken")
ScoreHistoryFilterByClubResponse findMyScoreHistories(@AuthenticationPrincipal PrincipalDetails principalDetails);
ScoreHistoryListResponse findMyScoreHistories(@AuthenticationPrincipal PrincipalDetails principalDetails);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ScoreHistoryResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ScoreHistoryResponse> scoreHistoryResponses = scoreHistoryService.findAllByClubId(club.getId())
.stream()
public ScoreHistoryListResponse findMyScoreHistories(PrincipalDetails principalDetails) {
User user = principalDetails.getUser();
ClubScoreHistoryListQuery query = facadeClubScoreHistoryService.findMyScoreHistories(user.getId());
List<ScoreHistoryResponse> scoreHistoryResponses = query.scoreHistories().stream()
.map(ScoreHistoryResponse::from)
.toList();
return ScoreHistoryFilterByClubResponse.of(club, scoreHistoryResponses);
return ScoreHistoryListResponse.of(query.club(), scoreHistoryResponses);
}
}
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,18 +16,22 @@
description = "어드민 - 동아리 점수 변동 내역 목록 응답"
)
@Builder
public record ScoreHistoryFilterByClubResponse(
public record ScoreHistoryListResponse(

@Schema(description = "동아리 총 점수", example = "50")
BigDecimal totalScore,
@ArraySchema(schema = @Schema(description = "점수내역 목록", implementation = ScoreHistoryResponse.class))
List<ScoreHistoryResponse> scoreHistories
) {

public static ScoreHistoryFilterByClubResponse of(Club club, List<ScoreHistoryResponse> 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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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<ScoreHistory> scoreHistories = scoreHistoryService.findAllByClubId(clubId);
return AdminClubScoreHistoryListQuery.of(club, scoreHistories);
}

private BigDecimal roundToThirdPoint(BigDecimal value) {
return value.setScale(3, RoundingMode.DOWN);
}

}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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<ScoreHistory> scoreHistories = scoreHistoryService.findAllByClubId(club.getId());
return ClubScoreHistoryListQuery.of(club, scoreHistories);
}
}
Original file line number Diff line number Diff line change
@@ -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<ScoreHistory> findAllByClubId(Long clubId) {
return scoreHistoryRepository.findByClubId(clubId);
}
}
Original file line number Diff line number Diff line change
@@ -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<ScoreHistory> findAllByClubId(Long clubId);

BigDecimal score = roundToThirdPoint(createScoreHistoryRequest.amount());
clubService.updateClubScore(clubId, score);
scoreHistoryRepository.save(createScoreHistoryRequest.toEntity(club));
}

@Transactional(readOnly = true)
public List<ScoreHistory> findAllByClubId(final Long clubId) {
return scoreHistoryRepository.findByClubId(clubId);
}

@Transactional(readOnly = true)
public List<ScoreHistory> 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);
}
}
Loading

0 comments on commit 9d9f8f5

Please sign in to comment.