Skip to content

Commit

Permalink
Merge pull request #52 from AR-TTUBEOG/chore/37
Browse files Browse the repository at this point in the history
[Chore] Benefit-Game 관계 변경
  • Loading branch information
choeun7 authored Feb 10, 2024
2 parents ace4268 + c1e3825 commit 1a164c8
Show file tree
Hide file tree
Showing 31 changed files with 306 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@
import com.ttubeog.domain.benefit.domain.MemberBenefit;
import com.ttubeog.domain.benefit.domain.repository.BenefitRepository;
import com.ttubeog.domain.benefit.domain.repository.MemberBenefitRepository;
import com.ttubeog.domain.benefit.dto.request.CreateBenefitReq;
import com.ttubeog.domain.benefit.dto.request.UpdateBenefitReq;
import com.ttubeog.domain.benefit.dto.response.CreateBenefitRes;
import com.ttubeog.domain.benefit.dto.response.SaveBenefitRes;
import com.ttubeog.domain.benefit.dto.response.UpdateBenefitRes;
import com.ttubeog.domain.benefit.exception.AlreadyUsedBenefitException;
import com.ttubeog.domain.benefit.exception.InvalidMemberBenefitException;
import com.ttubeog.domain.benefit.exception.NonExistentBenefitException;
import com.ttubeog.domain.benefit.exception.OverlappingBenefitException;
import com.ttubeog.domain.game.domain.Game;
import com.ttubeog.domain.game.domain.GameType;
import com.ttubeog.domain.game.domain.repository.GameRepository;
import com.ttubeog.domain.game.dto.response.FindGameRes;
import com.ttubeog.domain.member.domain.Member;
import com.ttubeog.domain.member.domain.repository.MemberRepository;
import com.ttubeog.domain.member.exception.InvalidMemberException;
Expand All @@ -29,7 +22,6 @@
import com.ttubeog.global.payload.Message;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.hibernate.Hibernate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
Expand All @@ -38,7 +30,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
Expand All @@ -53,84 +44,6 @@ public class BenefitService {
private final GameRepository gameRepository;
private final JwtTokenProvider jwtTokenProvider;

// 혜택 생성
@Transactional
public ResponseEntity<?> createBenefit(HttpServletRequest request, CreateBenefitReq createBenefitReq) throws JsonProcessingException {
Long memberId = jwtTokenProvider.getMemberId(request);
memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);

// Store store = storeRepository.findById(createBenefitReq.getStoreId()).orElseThrow(에러::new);

//TODO Store의 등록유저가 현재 멤버와 일치하는지 확인

Benefit benefit = Benefit.builder()
.content(createBenefitReq.getContent())
.type(createBenefitReq.getType())
// .store(store)
.build();

benefitRepository.save(benefit);

CreateBenefitRes createBenefitRes = CreateBenefitRes.builder()
.benefitId(benefit.getId())
// .storeId(benefit.getStore().getId())
.content(benefit.getContent())
.type(benefit.getType())
.build();

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(createBenefitRes)
.build();

return ResponseEntity.ok(apiResponse);
}

// 혜택 삭제
@Transactional
public ResponseEntity<?> deleteBenefit(HttpServletRequest request, Long benefitId) throws JsonProcessingException {
Long memberId = jwtTokenProvider.getMemberId(request);
Member member = memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);
Benefit benefit = benefitRepository.findById(benefitId).orElseThrow(NonExistentBenefitException::new);

//TODO Store의 등록유저가 현재 멤버와 일치하는지 확인

benefitRepository.delete(benefit);

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(Message.builder().message("혜택을 삭제했습니다.").build())
.build();

return ResponseEntity.ok(apiResponse);
}

//혜택 수정
@Transactional
public ResponseEntity<?> updateBenefit(HttpServletRequest request, UpdateBenefitReq updateBenefitReq) throws JsonProcessingException {
Long memberId = jwtTokenProvider.getMemberId(request);
memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);
Benefit benefit = benefitRepository.findById(updateBenefitReq.getBenefitId()).orElseThrow(NonExistentBenefitException::new);

//TODO Store의 등록유저가 현재 멤버와 일치하는지 확인

benefit.updateContent(updateBenefitReq.getContent());

UpdateBenefitRes updateBenefitRes = UpdateBenefitRes.builder()
.benefitId(benefit.getId())
//.storeId(benefit.getStore().getId())
.content(benefit.getContent())
.type(benefit.getType())
.build();

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(updateBenefitRes)
.build();

return ResponseEntity.ok(apiResponse);
}

//게임 성공 후 혜택 저장
@Transactional
public ResponseEntity<?> saveBenefit(HttpServletRequest request, Long benefitId) throws JsonProcessingException {
Expand All @@ -155,7 +68,6 @@ public ResponseEntity<?> saveBenefit(HttpServletRequest request, Long benefitId)
SaveBenefitRes saveBenefitRes = SaveBenefitRes.builder()
.id(memberBenefit.getId())
.benefitId(benefit.getId())
// .storeId(memberBenefit.getBenefit().getStore().getId())
.content(benefit.getContent())
.type(benefit.getType())
.used(memberBenefit.getUsed())
Expand Down Expand Up @@ -193,7 +105,6 @@ public ResponseEntity<?> useBenefit(HttpServletRequest request, Long benefitId)
SaveBenefitRes saveBenefitRes = SaveBenefitRes.builder()
.id(memberBenefit.getId())
.benefitId(benefit.getId())
// .store(memberBenefit.store.getId())
.used(memberBenefit.getUsed())
.expried(memberBenefit.getExpired())
.createdAt(memberBenefit.getCreatedAt())
Expand All @@ -220,7 +131,6 @@ public ResponseEntity<?> findMyBenefit(HttpServletRequest request, Integer page)
memberBenefit -> SaveBenefitRes.builder()
.id(memberBenefit.getId())
.benefitId(memberBenefit.getBenefit().getId())
// .store(memberBenefit.getStore().getId())
.used(memberBenefit.getUsed())
.expried(memberBenefit.getExpired())
.createdAt(memberBenefit.getCreatedAt())
Expand All @@ -237,42 +147,6 @@ public ResponseEntity<?> findMyBenefit(HttpServletRequest request, Integer page)
return ResponseEntity.ok(apiResponse);
}

//혜택ID로 게임 조회
public ResponseEntity<?> findGames(HttpServletRequest request, Long benefitId) throws JsonProcessingException {
Long memberId = jwtTokenProvider.getMemberId(request);
memberRepository.findById(memberId).orElseThrow(InvalidMemberException::new);
Benefit benefit = benefitRepository.findById(benefitId).orElseThrow(NonExistentBenefitException::new);
List<Game> games = gameRepository.findAllByBenefit(benefit);

List<FindGameRes> findGameResList = new ArrayList<>();
for (Game game : games) {
FindGameRes.FindGameResBuilder builder = FindGameRes.builder()
.gameId(game.getId())
.type(game.getType());

if (game.getType() == GameType.BASKETBALL) {
builder.timeLimit(game.getBasketballGame().getTimeLimit())
.ballCount(game.getBasketballGame().getBallCount())
.successCount(game.getBasketballGame().getSuccessCount());
} else if (game.getType() == GameType.GIFT) {
builder.timeLimit(game.getGiftGame().getTimeLimit())
.giftCount(game.getGiftGame().getGiftCount());
} else if (game.getType() == GameType.ROULETTE) {
Hibernate.initialize(game.getRouletteGame().getOptions()); // 명시적 초기화
builder.options(game.getRouletteGame().getOptions());
}

findGameResList.add(builder.build());
}

ApiResponse apiResponse = ApiResponse.builder()
.check(true)
.information(findGameResList)
.build();

return ResponseEntity.ok(apiResponse);
}

//한달지나면 expired true로 만들기
@Transactional
@Scheduled(fixedRate = 60*60000) //1시간마다 검사
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/ttubeog/domain/benefit/domain/Benefit.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import jakarta.persistence.*;
import lombok.*;

import java.util.List;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
Expand All @@ -25,21 +23,23 @@ public class Benefit extends BaseEntity {
@Enumerated(EnumType.STRING)
private BenefitType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "game_id")
private Game game;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id")
private Store store;

@OneToMany(mappedBy = "benefit", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Game> game;

@Builder
public Benefit(String content, BenefitType type, Store store) {
public Benefit(String content, BenefitType type, Game game, Store store) {
this.content = content;
this.type = type;
this.game = game;
this.store = store;
}

public void updateContent(String content) {
this.content = content;
public void deleteGame() {
this.game = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.ttubeog.domain.benefit.domain;

public enum BenefitType {
sale, plus, gift
SALE, PLUS, GIFT
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.ttubeog.domain.benefit.domain.repository;

import com.ttubeog.domain.benefit.domain.Benefit;
import com.ttubeog.domain.game.domain.Game;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface BenefitRepository extends JpaRepository<Benefit, Long> {

Optional<Benefit> findByGame(Game game);

List<Benefit> findAllByGame(Game game);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface MemberBenefitRepository extends JpaRepository<MemberBenefit, Lo
List<MemberBenefit> findAllByExpiredFalse();

Page<MemberBenefit> findAllByMember(Member member, PageRequest pageRequest);
}

List<MemberBenefit> findAllByMemberAndBenefitIn(Member member, List<Benefit> benefits);
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ public class SaveBenefitRes {
@Schema(description = "혜택 ID", example = "1")
private Long benefitId;

// @Schema(description = "매장 ID", example = "1")
// private Long storeId;

@Schema(description = "내용", example = "아메리카노 20% 할인")
private String content;

@Schema(description = "혜택타입", example = "sale")
@Schema(description = "혜택타입", example = "SALE")
private BenefitType type;

@Schema(description = "사용 여부", example = "false")
Expand All @@ -35,11 +32,9 @@ public class SaveBenefitRes {
private LocalDateTime createdAt;

@Builder
// public SaveBenefitRes(Long id, Long benefitId, Long storeId, String content, BenefitType type, Boolean isUsed, Boolean expried, LocalDateTime createdAt) {
public SaveBenefitRes(Long id, Long benefitId, String content, BenefitType type, Boolean used, Boolean expried, LocalDateTime createdAt) {
this.id = id;
this.benefitId = benefitId;
// this.storeId = storeId;
this.content = content;
this.type = type;
this.used = used;
Expand Down

This file was deleted.

Loading

0 comments on commit 1a164c8

Please sign in to comment.