Skip to content

Commit

Permalink
[#19] (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehongg authored Jan 27, 2023
1 parent 7119b26 commit 905129f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -76,4 +81,7 @@ public static Customer from (String nickname , String contact) {
}


public void upgrade(Level level) {
this.level = level;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

Expand All @@ -34,5 +45,4 @@ public List<HistoryResponse> findAllById(Long customerId) {
List<History> histories = historyRepository.findAllById(customerId);
return histories.stream().map(HistoryResponse::from).collect(Collectors.toList());
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/greenpoint/server/level/model/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@NoArgsConstructor
@AllArgsConstructor
public class LevelRequest {
private int grade;
private int lpoint;
public String name;
public String image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Level, Long> {
@Query("select l from Level l where l.grade = :newgrade")
Level findByGrade(int newgrade);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public Level create(Level level){
return ret;
}

public Level findByGrade(int newgrade) {
Level ret = levelRepository.findByGrade(newgrade);
return ret;
}
}

0 comments on commit 905129f

Please sign in to comment.