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: modify home/receiver #39

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/zero/eight/donut/domain/enums/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public enum Store {
CU("CU"),
GS25("GS25"),
SEVENELEVEN("SEVENELEVEN"),
SEVENELEVEN("7-ELEVEN"),
EMART24("EMART24"),
MINISTOP("MINISTOP");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DonateGiftRequestDto {
private LocalDateTime dueDate;
private Store store;

public Gift toEntity(Giver giver, Giftbox giftbox, String imageUrl){
public Gift toEntity(Giver giver, Giftbox giftbox, String imageUrl, String store){
return Gift.builder()
.giver(giver)
.product(product)
Expand All @@ -29,7 +29,7 @@ public Gift toEntity(Giver giver, Giftbox giftbox, String imageUrl){
.status(Status.UNUSED)
.price(price)
.imageUrl(imageUrl)
.store(store)
.store( Store.valueOf((store)))
.dueDate(dueDate)
.giftbox(giftbox)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import zero.eight.donut.domain.Giftbox;
import zero.eight.donut.domain.enums.Store;

import java.util.List;
import java.util.Optional;
Expand All @@ -18,4 +19,7 @@ public interface GiftboxRepository extends JpaRepository<Giftbox, Long> {

@Query("SELECT gb FROM Giftbox gb WHERE gb.receiver.id = ?1 and gb.isAvailable = true")
List<Giftbox> findAllByReceiverIdAndIsAvailable(Long receiver_id);

@Query("SELECT SUM(gb.amount) FROM Giftbox gb WHERE gb.store = ?1")
Integer getSumByStore(Store store);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public ApiResponse<?> donateGift(DonateGiftRequestDto requestDto) throws IOExcep
String imgUrl = uploadImageToGCS(requestDto);

//CREATE Gift
Gift newGift = requestDto.toEntity(giver, defaultGiftbox, imgUrl);
Gift newGift = requestDto.toEntity(giver, defaultGiftbox, imgUrl, requestDto.getStore().toString());
giftRepository.save(newGift);

//기부자별 정보 Donation 업데이트
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/zero/eight/donut/service/HomeReceiverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public ApiResponse receiverHome(){
List<Giftbox> giftboxList = Optional.ofNullable(giftboxRepository.findAllByReceiverIdAndIsAvailable(receiver.getId()))
.orElse(Collections.emptyList());

//사용처별 꾸러미 개수 구하기
Map<String, Integer> storeCountMap = new HashMap<>();
giftboxList.stream().forEach(box -> {
Store store = box.getStore();
storeCountMap.put(store.toString(), storeCountMap.getOrDefault(store, 0) + 1);
});
//사용처별 꾸러미 잔액
Optional<Integer> cuGiftBox = Optional.ofNullable(giftboxRepository.getSumByStore(Store.CU));
Optional<Integer> gs25GiftBox = Optional.ofNullable(giftboxRepository.getSumByStore(Store.GS25));
Optional<Integer> sevenelevenGiftBox = Optional.ofNullable(giftboxRepository.getSumByStore(Store.SEVENELEVEN));
Integer cu = cuGiftBox.orElse(0);
Integer gs25 = gs25GiftBox.orElse(0);
Integer seveneleven = sevenelevenGiftBox.orElse(0);

//꾸러미 정보 가져오기
List<BoxInfo> boxInfoList = giftboxList.stream()
Expand Down Expand Up @@ -78,9 +79,9 @@ public ApiResponse receiverHome(){
ReceiverHomeResponseDto responseDto = ReceiverHomeResponseDto.builder()
.availability(availability)
.amount(amount)
.cu(storeCountMap.getOrDefault("CU", 0))
.gs25(storeCountMap.getOrDefault("GS25", 0))
.sevenEleven(storeCountMap.getOrDefault("SEVENELEVEN", 0))
.cu(cu)
.gs25(gs25)
.sevenEleven(seveneleven)
.boxList(boxInfoList)
.build();
return ApiResponse.success(Success.HOME_RECEIVER_SUCCESS, responseDto);
Expand Down
Loading