From 905129fb921f076cb92fc69da75e0fcf1248dd5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=99=8D=EC=84=B1=ED=97=8C?= <79990740+lukehongg@users.noreply.github.com> Date: Fri, 27 Jan 2023 22:53:34 +0900 Subject: [PATCH] [#19] (#20) --- .../server/customer/model/Customer.java | 10 +++++++++- .../greenpoint/server/history/model/History.java | 1 + .../server/history/service/HistoryService.java | 16 +++++++++++++--- .../com/greenpoint/server/level/model/Level.java | 4 ++++ .../server/level/model/LevelRequest.java | 2 ++ .../server/level/repository/LevelRepository.java | 3 +++ .../server/level/service/LevelService.java | 4 ++++ 7 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/greenpoint/server/customer/model/Customer.java b/src/main/java/com/greenpoint/server/customer/model/Customer.java index a5e07f5..90db7fc 100644 --- a/src/main/java/com/greenpoint/server/customer/model/Customer.java +++ b/src/main/java/com/greenpoint/server/customer/model/Customer.java @@ -43,9 +43,14 @@ public void insertUserToken(String token){ } - public void addpoint(int savedPoint) { + public int addpoint(int savedPoint) { this.point = this.point + savedPoint; this.totalPoint = this.totalPoint + savedPoint; + int grade; + if(this.totalPoint >= 1000000) grade = 3; + else if(this.totalPoint >= 100000) grade = 2; + else grade = 1; + return grade; } public void usepoint(int usedPoint) { @@ -76,4 +81,7 @@ public static Customer from (String nickname , String contact) { } + public void upgrade(Level level) { + this.level = level; + } } diff --git a/src/main/java/com/greenpoint/server/history/model/History.java b/src/main/java/com/greenpoint/server/history/model/History.java index 0b5486b..01104c6 100644 --- a/src/main/java/com/greenpoint/server/history/model/History.java +++ b/src/main/java/com/greenpoint/server/history/model/History.java @@ -27,6 +27,7 @@ public class History extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) private Store store; + private int cost; private int savedPoint; private int usedPoint; diff --git a/src/main/java/com/greenpoint/server/history/service/HistoryService.java b/src/main/java/com/greenpoint/server/history/service/HistoryService.java index 509e1a1..81d2c4a 100644 --- a/src/main/java/com/greenpoint/server/history/service/HistoryService.java +++ b/src/main/java/com/greenpoint/server/history/service/HistoryService.java @@ -1,10 +1,11 @@ package com.greenpoint.server.history.service; import com.greenpoint.server.customer.model.Customer; -import com.greenpoint.server.customer.service.CustomerService; import com.greenpoint.server.history.model.History; import com.greenpoint.server.history.model.HistoryResponse; import com.greenpoint.server.history.repository.HistoryRepository; +import com.greenpoint.server.level.model.Level; +import com.greenpoint.server.level.service.LevelService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -17,14 +18,24 @@ public class HistoryService { @Autowired private HistoryRepository historyRepository; + @Autowired + private LevelService levelService; + @Transactional public Long create(History history, Customer customer){ History res = historyRepository.save(history); - customer.addpoint(history.getSavedPoint()); + + int grade = customer.getLevel().getGrade(); customer.usepoint(history.getUsedPoint()); + int newgrade = customer.addpoint(history.getSavedPoint()); + if(newgrade > grade){ + Level level = levelService.findByGrade(newgrade); + customer.upgrade(level); + } + return res.getId(); } @@ -34,5 +45,4 @@ public List findAllById(Long customerId) { List histories = historyRepository.findAllById(customerId); return histories.stream().map(HistoryResponse::from).collect(Collectors.toList()); } - } diff --git a/src/main/java/com/greenpoint/server/level/model/Level.java b/src/main/java/com/greenpoint/server/level/model/Level.java index 49fe403..fa11a0a 100644 --- a/src/main/java/com/greenpoint/server/level/model/Level.java +++ b/src/main/java/com/greenpoint/server/level/model/Level.java @@ -21,11 +21,15 @@ public class Level extends BaseEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + private int grade; + private int lpoint; private String name; private String image; public static Level from(LevelRequest request) { return Level.builder() + .grade(request.getGrade()) + .lpoint(request.getLpoint()) .name(request.getName()) .image(request.getImage()) .build(); diff --git a/src/main/java/com/greenpoint/server/level/model/LevelRequest.java b/src/main/java/com/greenpoint/server/level/model/LevelRequest.java index e5abfc2..398b230 100644 --- a/src/main/java/com/greenpoint/server/level/model/LevelRequest.java +++ b/src/main/java/com/greenpoint/server/level/model/LevelRequest.java @@ -9,6 +9,8 @@ @NoArgsConstructor @AllArgsConstructor public class LevelRequest { + private int grade; + private int lpoint; public String name; public String image; } diff --git a/src/main/java/com/greenpoint/server/level/repository/LevelRepository.java b/src/main/java/com/greenpoint/server/level/repository/LevelRepository.java index 42c766e..578379d 100644 --- a/src/main/java/com/greenpoint/server/level/repository/LevelRepository.java +++ b/src/main/java/com/greenpoint/server/level/repository/LevelRepository.java @@ -3,6 +3,9 @@ import com.greenpoint.server.level.model.Level; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface LevelRepository extends JpaRepository { + @Query("select l from Level l where l.grade = :newgrade") + Level findByGrade(int newgrade); } diff --git a/src/main/java/com/greenpoint/server/level/service/LevelService.java b/src/main/java/com/greenpoint/server/level/service/LevelService.java index 019f290..7811cfd 100644 --- a/src/main/java/com/greenpoint/server/level/service/LevelService.java +++ b/src/main/java/com/greenpoint/server/level/service/LevelService.java @@ -18,4 +18,8 @@ public Level create(Level level){ return ret; } + public Level findByGrade(int newgrade) { + Level ret = levelRepository.findByGrade(newgrade); + return ret; + } }