Commit 2a00476e authored by shoei.kanno's avatar shoei.kanno

財布機能追加

parent b67d62f3
......@@ -48,8 +48,8 @@ public class EcsiteRestController {
@Autowired
WalletsService walletsService;
int totalPrice;
String sameId;
int totalPrice;
//アカウント新規登録
@PostMapping("signUp")
......@@ -117,17 +117,22 @@ public class EcsiteRestController {
@GetMapping("buy")
public void buy(@AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart, Wallets wallet){
if(walletsService.findOne(userDetails.getId()).getAmount() < totalPrice){
throw new BadRequestException();
}
items = (LinkedHashMap<String, Items>) session.getAttribute("cart");
items.forEach((key, value) -> {
cart.add(value);
});
User user = userDetails.getUser();
Integer userId = user.getId();
salesLogService.update(1, cart);
cart.forEach(item ->{
totalPrice += item.getPrice();
});
Integer userId = (Integer) session.getAttribute("userId");
if(walletsService.findOne(userId).getAmount() < totalPrice){
throw new BadRequestException();
}
//支払い
walletsService.pay(userId, totalPrice);
salesLogService.update(userId, cart);
session.removeAttribute("cart");
session.removeAttribute("cartValue");
totalPrice = 0;
}
}
\ No newline at end of file
......@@ -30,6 +30,15 @@ public class WalletsService {
return walletsRepository.save(wallet);
}
//チャージ処理
public Wallets charge(Integer id, Integer amount){
//対象の財布情報を取得
Wallets wallet = new Wallets();
wallet = this.findOne(id);
wallet.setAmount(wallet.getAmount() + amount);
return walletsRepository.save(wallet);
}
//支払い処理
public Wallets pay(Integer id, Integer amount){
//対象の財布情報を取得
......
......@@ -15,11 +15,11 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.domain.BadRequestException;
import com.example.domain.Items;
import com.example.domain.SalesLog;
import com.example.domain.Shoes;
......@@ -42,6 +42,11 @@ public class EcsiteController {
@Autowired
HttpSession session;
@ModelAttribute
WalletsForm setUpForm() {
return new WalletsForm();
}
int totalPrice = 0;
//写真の表示用メソッド
public String photoView(String Photo) {
......@@ -151,6 +156,7 @@ public class EcsiteController {
public String Cart(Model model, @AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart){
User user = userDetails.getUser();
model.addAttribute("user", user);
session.setAttribute("userId", user.getId());
items = (LinkedHashMap<String, Items>) session.getAttribute("cart");
if(items == null) {
if(session.getAttribute("cartValue") == null) {
......@@ -189,7 +195,7 @@ public class EcsiteController {
//財布チャージ処理
@PostMapping("wallets/charge/{id}")
public String walletsCharge(@PathVariable Integer id, @Validated WalletsForm form){
walletsService.pay(id, form.getAmount());
return "redirect:/top";
walletsService.charge(id, form.getAmount());
return "redirect:/limited/top";
}
}
......@@ -21,7 +21,7 @@ $('#ok-btn').on('click',function(){
alert("購入が完了しました。");
window.location.href = "/limited/top";
})
.faild(function(){
.fail(function(){
alert("ウォレットの残高が足りません。チャージしてください");
})
});
\ No newline at end of file
......@@ -98,7 +98,7 @@ $('.cartBtn').on('click', function(event){
contentType: 'application/json',
dataType: "json",
data: JSON.stringify(data)
});
})
//[#modal-content]と[#modal-overlay]をフェードアウトした後に…
$( "#modal-content,#modal-overlay" ).fadeOut( "slow" , function(){
//[#modal-overlay]を削除する
......
......@@ -69,7 +69,7 @@
<div class="container">
<p id="online-shoe-store" class="wallets-p"><span id="online-shoe-store-span">Wallets</span></p>
<p id="balance">Balance<span id="balanceSpan" th:text="'&yen;' + ${wallet.amount}">10000</span></p>
<form id="wallets-form" method="post" th:action="@{/limited/wallets/charge/{id}(id=${user.id})}" th:object="${walletForm}">
<form id="wallets-form" method="post" th:action="@{/limited/wallets/charge/{id}(id=${user.id})}" th:object="${walletsForm}">
<div id="amount-form">
<label for="amount">Charge Money</label>
<span id="en">&yen;</span><input type="number" id="amount" name="amount" required="required" th:field="*{amount}"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment