diff --git a/http/account/AccountControllerHttpRequest.http b/http/account/AccountControllerHttpRequest.http index dc7f9da..373df0a 100644 --- a/http/account/AccountControllerHttpRequest.http +++ b/http/account/AccountControllerHttpRequest.http @@ -8,4 +8,17 @@ Authorization: Bearer {{access_token}} ### 3.2 가게 상세 정보 조회 // @no-log GET {{host_url}}/api/v1/stores/{{account.API_3_2.id}}/details -Authorization: Bearer {{access_token}} \ No newline at end of file +Authorization: Bearer {{access_token}} + +### 3.3 가게 히스토리 등록하기 +// @no-log +POST {{host_url}}/api/v1/store-histories +Content-Type: application/json +Authorization: Bearer {{access_token}} + +{ + "store_id": {{account.API_3_3.store_id}}, + "action_date": "{{account.API_3_3.action_date}}", + "content": "{{account.API_3_3.content}}", + "amount": {{account.API_3_3.amount}} +} diff --git a/src/main/java/com/daon/onjung/account/application/controller/command/StoreCommandV1Controller.java b/src/main/java/com/daon/onjung/account/application/controller/command/StoreCommandV1Controller.java new file mode 100644 index 0000000..3627764 --- /dev/null +++ b/src/main/java/com/daon/onjung/account/application/controller/command/StoreCommandV1Controller.java @@ -0,0 +1,28 @@ +package com.daon.onjung.account.application.controller.command; + +import com.daon.onjung.account.application.dto.request.CreateStoreHistoryRequestDto; +import com.daon.onjung.account.application.usecase.CreateStoreHistoryUseCase; +import com.daon.onjung.core.dto.ResponseDto; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class StoreCommandV1Controller { + + private final CreateStoreHistoryUseCase createStoreHistoryUseCase; + + /** + * 3.3 가게 히스토리 등록하기 + */ + @PostMapping(value = "/api/v1/store-histories") + public ResponseDto createStoreHistory( + @RequestBody @Valid CreateStoreHistoryRequestDto requestDto + ) { + createStoreHistoryUseCase.execute(requestDto); + return ResponseDto.ok(null); + } +} diff --git a/src/main/java/com/daon/onjung/account/application/dto/request/CreateStoreHistoryRequestDto.java b/src/main/java/com/daon/onjung/account/application/dto/request/CreateStoreHistoryRequestDto.java new file mode 100644 index 0000000..4a4b5e7 --- /dev/null +++ b/src/main/java/com/daon/onjung/account/application/dto/request/CreateStoreHistoryRequestDto.java @@ -0,0 +1,20 @@ +package com.daon.onjung.account.application.dto.request; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.time.LocalDate; + +public record CreateStoreHistoryRequestDto( + @JsonProperty("store_id") + Long storeId, + + @JsonProperty("action_date") + LocalDate actionDate, + + @JsonProperty("content") + String content, + + @JsonProperty("amount") + Integer amount +) { +} \ No newline at end of file diff --git a/src/main/java/com/daon/onjung/account/application/dto/response/ReadStoreDetailResponseDto.java b/src/main/java/com/daon/onjung/account/application/dto/response/ReadStoreDetailResponseDto.java index 1630625..4a2a159 100644 --- a/src/main/java/com/daon/onjung/account/application/dto/response/ReadStoreDetailResponseDto.java +++ b/src/main/java/com/daon/onjung/account/application/dto/response/ReadStoreDetailResponseDto.java @@ -215,21 +215,25 @@ public static class StoreHistoryDto { @NotNull(message = "info는 null일 수 없습니다.") @JsonProperty("info") - private final StoreHistoryInfo storeHistoryInfo; + private final List storeHistoryInfo; @Builder - public StoreHistoryDto(String date, StoreHistoryInfo storeHistoryInfo) { + public StoreHistoryDto(String date, List storeHistoryInfo) { this.date = date; this.storeHistoryInfo = storeHistoryInfo; } public static StoreHistoryDto fromEntity(StoreHistory storeHistory) { return StoreHistoryDto.builder() - .date(DateTimeUtil.convertLocalDateToKORYearMonthString(storeHistory.getActionDate())) - .storeHistoryInfo(StoreHistoryInfo.fromEntity( - storeHistory.getContent(), - storeHistory.getAmount() + "만원" - )) + .date(DateTimeUtil.convertLocalDateToString(storeHistory.getActionDate())) + .storeHistoryInfo( + List.of( + StoreHistoryInfo.fromEntity( + storeHistory.getContent(), + storeHistory.getAmount() / 10000 + "만원" + ) + ) + ) .build(); } diff --git a/src/main/java/com/daon/onjung/account/application/service/CreateStoreHistoryService.java b/src/main/java/com/daon/onjung/account/application/service/CreateStoreHistoryService.java new file mode 100644 index 0000000..89d138a --- /dev/null +++ b/src/main/java/com/daon/onjung/account/application/service/CreateStoreHistoryService.java @@ -0,0 +1,40 @@ +package com.daon.onjung.account.application.service; + +import com.daon.onjung.account.application.dto.request.CreateStoreHistoryRequestDto; +import com.daon.onjung.account.application.usecase.CreateStoreHistoryUseCase; +import com.daon.onjung.account.domain.Store; +import com.daon.onjung.account.domain.StoreHistory; +import com.daon.onjung.account.domain.service.StoreHistoryService; +import com.daon.onjung.account.repository.mysql.StoreHistoryRepository; +import com.daon.onjung.account.repository.mysql.StoreRepository; +import com.daon.onjung.core.exception.error.ErrorCode; +import com.daon.onjung.core.exception.type.CommonException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class CreateStoreHistoryService implements CreateStoreHistoryUseCase { + + private final StoreRepository storeRepository; + private final StoreHistoryRepository storeHistoryRepository; + + private final StoreHistoryService storeHistoryService; + + @Override + @Transactional + public void execute(CreateStoreHistoryRequestDto requestDto) { + Store store = storeRepository.findById(requestDto.storeId()) + .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_RESOURCE)); + + StoreHistory storeHistory = storeHistoryService.createStoreHistory( + requestDto.actionDate(), + requestDto.content(), + requestDto.amount(), + store + ); + + storeHistoryRepository.save(storeHistory); + } +} diff --git a/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java b/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java index 37cf5a4..38ca3e4 100644 --- a/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java +++ b/src/main/java/com/daon/onjung/account/application/service/ReadStoreDetailService.java @@ -10,6 +10,7 @@ import com.daon.onjung.core.exception.error.ErrorCode; import com.daon.onjung.core.exception.type.CommonException; import com.daon.onjung.core.utility.BankUtil; +import com.daon.onjung.core.utility.DateTimeUtil; import com.daon.onjung.core.utility.RestClientUtil; import com.daon.onjung.event.domain.Event; import com.daon.onjung.event.domain.service.EventService; @@ -21,7 +22,9 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -67,8 +70,26 @@ public ReadStoreDetailResponseDto execute(Long id) { List storeHistories = storeHistoryRepository.findByStoreOrderByActionDateAsc(store); - List storeHistoryDtos = storeHistories.stream() - .map(ReadStoreDetailResponseDto.StoreHistoryDto::fromEntity) + // 그룹화: actionDate를 기준으로 그룹화 + Map> groupedByYearMonth = storeHistories.stream() + .collect(Collectors.groupingBy( + sh -> DateTimeUtil.convertLocalDateToKORYearMonthString(sh.getActionDate()) // "yyyy년 MM월" 기준 그룹화 + )); + + + List storeHistoryDtos = groupedByYearMonth.entrySet().stream() + .map(entry -> { + String yearMonth = entry.getKey(); + List infos = entry.getValue().stream() + .map(ReadStoreDetailResponseDto.StoreHistoryDto::fromEntity) // StoreHistory -> StoreHistoryDto 변환 + .flatMap(dto -> dto.getStoreHistoryInfo().stream()) // StoreHistoryInfo만 추출 + .toList(); + + return ReadStoreDetailResponseDto.StoreHistoryDto.builder() + .date(yearMonth) + .storeHistoryInfo(infos) + .build(); + }) .toList(); return ReadStoreDetailResponseDto.fromEntity(storeInfoDto, eventInfoDto, onjungInfoDto, storeHistoryDtos); diff --git a/src/main/java/com/daon/onjung/account/application/usecase/CreateStoreHistoryUseCase.java b/src/main/java/com/daon/onjung/account/application/usecase/CreateStoreHistoryUseCase.java new file mode 100644 index 0000000..714c19f --- /dev/null +++ b/src/main/java/com/daon/onjung/account/application/usecase/CreateStoreHistoryUseCase.java @@ -0,0 +1,11 @@ +package com.daon.onjung.account.application.usecase; + +import com.daon.onjung.account.application.dto.request.CreateStoreHistoryRequestDto; +import com.daon.onjung.core.annotation.bean.UseCase; + +import java.time.LocalDate; + +@UseCase +public interface CreateStoreHistoryUseCase { + void execute(CreateStoreHistoryRequestDto requestDto); +} diff --git a/src/main/java/com/daon/onjung/account/domain/StoreHistory.java b/src/main/java/com/daon/onjung/account/domain/StoreHistory.java index aebf307..056dddb 100644 --- a/src/main/java/com/daon/onjung/account/domain/StoreHistory.java +++ b/src/main/java/com/daon/onjung/account/domain/StoreHistory.java @@ -40,9 +40,10 @@ public class StoreHistory { /* Methods ------------------------------------ */ /* -------------------------------------------- */ @Builder - public StoreHistory(LocalDate actionDate, String content, Store store) { + public StoreHistory(LocalDate actionDate, String content, Integer amount, Store store) { this.actionDate = actionDate; this.content = content; this.store = store; + this.amount = amount; } } diff --git a/src/main/java/com/daon/onjung/account/domain/service/StoreHistoryService.java b/src/main/java/com/daon/onjung/account/domain/service/StoreHistoryService.java new file mode 100644 index 0000000..16b1a06 --- /dev/null +++ b/src/main/java/com/daon/onjung/account/domain/service/StoreHistoryService.java @@ -0,0 +1,24 @@ +package com.daon.onjung.account.domain.service; + +import com.daon.onjung.account.domain.Store; +import com.daon.onjung.account.domain.StoreHistory; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; + +@Service +public class StoreHistoryService { + public StoreHistory createStoreHistory( + LocalDate actionDate, + String content, + Integer amount, + Store store + ) { + return StoreHistory.builder() + .actionDate(actionDate) + .content(content) + .amount(amount) + .store(store) + .build(); + } +} diff --git a/src/main/java/com/daon/onjung/event/application/dto/response/ReadTicketResponseDto.java b/src/main/java/com/daon/onjung/event/application/dto/response/ReadTicketResponseDto.java index 03bf18f..7d1b44a 100644 --- a/src/main/java/com/daon/onjung/event/application/dto/response/ReadTicketResponseDto.java +++ b/src/main/java/com/daon/onjung/event/application/dto/response/ReadTicketResponseDto.java @@ -46,7 +46,7 @@ public static class TicketDto { @JsonProperty("id") private final Long id; - @JsonProperty("store_ingo") + @JsonProperty("store_info") private final StoreInfoDto storeInfo; @JsonProperty("expiration_date")