Commit c363330c authored by keita.onoguchi's avatar keita.onoguchi

ウォレット機能途中

parent 0617b3f2
......@@ -23,11 +23,13 @@ import com.example.domain.ItemData;
import com.example.domain.Items;
import com.example.domain.Shoes;
import com.example.domain.User;
import com.example.domain.Wallets;
import com.example.service.ItemsService;
import com.example.service.LoginUser;
import com.example.service.LoginUserDetailsService;
import com.example.service.SalesLogService;
import com.example.service.ShoesService;
import com.example.service.WalletsService;
@RestController
@RequestMapping("limited")
......@@ -43,6 +45,9 @@ public class EcsiteRestController {
SalesLogService salesLogService;
@Autowired
ShoesService shoesService;
@Autowired
WalletsService walletsService;
int totalPrice;
String sameId;
......@@ -56,6 +61,10 @@ public class EcsiteRestController {
user.setUpdatedAt(nowDate);
user.setPassword(new Pbkdf2PasswordEncoder().encode(user.getPassword()));
loginUserDetailsService.create(user);
Wallets wallet = new Wallets();
wallet.setUserId(user.getId());
wallet.setAmount(0);
walletsService.create(wallet);
return user;
}
......@@ -107,13 +116,16 @@ public class EcsiteRestController {
}
@GetMapping("buy")
public void buy(@AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart){
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();
User user = userDetails.getUser();
Integer userId = user.getId();
salesLogService.update(1, cart);
session.removeAttribute("cart");
session.removeAttribute("cartValue");
......
......@@ -5,14 +5,18 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.domain.BadRequestException;
import com.example.domain.Wallets;
import com.example.repository.WalletsRepository;
import com.example.web.EcsiteController;
//ユーザウォレットのサービス
@Service
public class WalletsService {
@Autowired
WalletsRepository walletsRepository;
@Autowired
Wallets wallet;
public List<Wallets> findAll(){
return walletsRepository.findAll();
......@@ -29,4 +33,12 @@ public class WalletsService {
public Wallets update(Wallets wallet){
return walletsRepository.save(wallet);
}
//支払い処理
public Wallets pay(Integer id, Integer amount){
//対象の財布情報を取得
wallet = this.findOne(id);
wallet.setAmount(wallet.getAmount() - amount);
return walletsRepository.save(wallet);
}
}
......@@ -13,28 +13,34 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.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;
import com.example.domain.User;
import com.example.domain.Wallets;
import com.example.service.LoginUser;
import com.example.service.SalesLogService;
import com.example.service.ShoesService;
import com.example.service.WalletsService;
@Controller
@RequestMapping("limited")
public class EcsiteController {
@Autowired
ShoesService shoesService;
@Autowired
WalletsService walletsService;
@Autowired
SalesLogService salesLogService;
@Autowired
HttpSession session;
@Autowired
HttpSession session;
int totalPrice = 0;
//写真の表示用メソッド
......@@ -132,11 +138,15 @@ HttpSession session;
public String historyDetails (@PathVariable Date created, Model model, @AuthenticationPrincipal LoginUser userDetails) {
User user = userDetails.getUser();
model.addAttribute("user", user);
if(salesLogService.historyDetails(created, user.getId()) == null){
return "logDetails";
}
List<SalesLog> list = salesLogService.historyDetails(created, user.getId());
model.addAttribute("logDetails", list);
return "logDetails";
}
//カート画面
@GetMapping("cart")
public String Cart(Model model, @AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart){
User user = userDetails.getUser();
......@@ -156,6 +166,7 @@ HttpSession session;
totalPrice += item.getPrice();
});
model.addAttribute("totalPrice", totalPrice);
session.setAttribute("totalprice", totalPrice);
totalPrice = 0;
if(session.getAttribute("cartValue") == null) {
model.addAttribute("cartValue", 0);
......@@ -167,9 +178,18 @@ HttpSession session;
//財布画面
@GetMapping("wallets/{id}")
public String wallets(Model model, @AuthenticationPrincipal LoginUser userDetails) {
public String wallets(@PathVariable Integer id, Model model, @AuthenticationPrincipal LoginUser userDetails, Wallets wallet) {
User user = userDetails.getUser();
model.addAttribute("user", user);
wallet = walletsService.findOne(id);
model.addAttribute("wallet", wallet);
return "wallets";
}
//財布チャージ処理
@PostMapping("wallets/charge/{id}")
public String walletsCharge(@PathVariable Integer id, @Validated WalletsForm form){
walletsService.pay(id, form.getAmount());
return "redirect:/top";
}
}
package com.example.web;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class WalletsForm {
private Integer amount;
}
......@@ -21,4 +21,7 @@ $('#ok-btn').on('click',function(){
alert("購入が完了しました。");
window.location.href = "/limited/top";
})
.faild(function(){
alert("ウォレットの残高が足りません。チャージしてください");
})
});
\ No newline at end of file
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