Commit 3b1db15a authored by keita.onoguchi's avatar keita.onoguchi

購入数分、靴のストックを減らす処理を追加

parent 97502a8e
package com.example.api;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.example.domain.ErrorResponse;
@RestControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler{
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
if (!(body instanceof ErrorResponse)) {
if(status.getReasonPhrase().equalsIgnoreCase("Method Not Allowed")){
body = new Error("URLの誤り");
status = HttpStatus.NOT_FOUND;
}
}
return new ResponseEntity<>(body, headers, status);
}
@ExceptionHandler({Exception.class})
public ResponseEntity<Object> handle400(Exception ex, WebRequest request) {
HttpHeaders headers = new HttpHeaders();
ErrorResponse body = new ErrorResponse("これ以上購入できません");
HttpStatus status = HttpStatus.BAD_REQUEST;
return this.handleExceptionInternal(ex, body, headers, status, request);
}
}
\ No newline at end of file
......@@ -18,13 +18,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.example.domain.BadRequestException;
import com.example.domain.ItemData;
import com.example.domain.Items;
import com.example.domain.Shoes;
import com.example.domain.User;
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;
@RestController
@RequestMapping("limited")
......@@ -38,8 +41,11 @@ public class EcsiteRestController {
HttpSession session;
@Autowired
SalesLogService salesLogService;
@Autowired
ShoesService shoesService;
int totalPrice;
//アカウント新規登録
@PostMapping("signUp")
public User createUser(@RequestBody User user) {
user.setRoles("USER");
......@@ -52,28 +58,45 @@ public class EcsiteRestController {
return user;
}
//カート追加処理
@PostMapping("inputCart")
public void inputCart(@RequestBody ItemData data, Model model) {
public void inputCart(@RequestBody ItemData data, Model model, Shoes shoes) {
//靴の在庫数を購入予定数が超えていないか確認
shoes = shoesService.findOne(data.getShoesId());
if(shoes.getStock() < data.getQuantity()){
throw new BadRequestException();
}
//セッションがnullの場合
if(session.getAttribute("cart") == null){
//HashMapを生成
LinkedHashMap<String, Items> cart = new LinkedHashMap<String, Items>();
String id = data.getShoesId().toString();
//HashMapに靴のIdをKeyとして、Valueに靴の情報を入れる。
cart.put(id , itemsService.findOne(data.getShoesId(),data.getQuantity()));
System.out.println(cart);
//HashMapをセッションに追加
session.setAttribute("cart", cart);
//カートのアイテム数をセッションに追加
session.setAttribute("cartValue", data.getQuantity());
//セッションがnull出ない場合
}else{
//HashMapを生成
LinkedHashMap<String, Items> cart = new LinkedHashMap<String, Items>();
String id = data.getShoesId().toString();
//生成したHashMapにセッションにもともとある情報を照会させる。
cart = (LinkedHashMap<String, Items>) session.getAttribute("cart");
//HashMapに靴のIdをKeyとして、Valueに靴の情報を入れる。
cart.put(id, itemsService.findOne(data.getShoesId(),data.getQuantity()));
//HashMapをセッションに追加
session.setAttribute("cart", cart);
int nowQuantity = (int) session.getAttribute("cartValue");
//カートのアイテム数をセッションに追加
session.setAttribute("cartValue", nowQuantity + data.getQuantity());
}
}
@GetMapping("buy")
public void buy(@AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart){
public String buy(@AuthenticationPrincipal LoginUser userDetails, LinkedHashMap<String, Items> items, ArrayList <Items> cart){
items = (LinkedHashMap<String, Items>) session.getAttribute("cart");
items.forEach((key, value) -> {
cart.add(value);
......@@ -82,5 +105,6 @@ public class EcsiteRestController {
//Integer userId = user.getId();
salesLogService.update(1, cart);
session.removeAttribute("cart");
return "index";
}
}
\ No newline at end of file
package com.example.domain;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
private static final long serialVersionUID = 1L;
public BadRequestException() {
super();
}
}
\ No newline at end of file
package com.example.domain;
import lombok.Value;
@Value
public class ErrorResponse {
//@JsonProperty("Error")
//private Error error;
private final String message;
/*public ErrorResponse(String message) {
this.error = new Error(message);
}
@Value
private class Error {
}*/
}
\ No newline at end of file
......@@ -5,17 +5,19 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import com.example.domain.Items;
import com.example.domain.SalesLog;
import com.example.domain.Shoes;
import com.example.repository.SalesLogRepository;
@Service
public class SalesLogService {
@Autowired
SalesLogRepository salesLogRepository;
@Autowired
ShoesService shoesService;
public List<SalesLog> findAll(){
return salesLogRepository.findAll();
......@@ -30,21 +32,24 @@ public class SalesLogService {
}
public void update(Integer userId, ArrayList <Items> cart){
int count = 0;
java.util.Date d1 = new java.util.Date();
//d1をSQL.DATE型に変換してtodayに代入
java.sql.Date today = new java.sql.Date(d1.getTime());
System.out.println(cart.size());
//カートに入っている靴の種類文処理
for(int i = 0; i < cart.size(); i++){
//在庫数を購入数分減らす処理
Shoes shoes = new Shoes();
shoes = shoesService.findOne((cart.get(i)).getShoesId());
shoes.setStock(shoes.getStock() - (cart.get(i)).getQuantity());
shoesService.update(shoes);
//購入履歴に情報を追加する処理
SalesLog salesLog = new SalesLog();
salesLog.setUserId(userId);
salesLog.setShoesId((cart.get(i)).getShoesId());
salesLog.setPrice((cart.get(i)).getPrice());
salesLog.setCreated(today);
salesLogRepository.save(salesLog);
count++;
}
System.out.println(count);
}
public List<SalesLog> history(Integer id) {
......
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