This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
245 additions
and
5 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/com/sport/sportsmailserver/controller/CartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.sport.sportsmailserver.controller; | ||
|
||
import com.sport.sportsmailserver.dto.LoginUser; | ||
import com.sport.sportsmailserver.dto.RestModel; | ||
import com.sport.sportsmailserver.security.MustUserLogin; | ||
import com.sport.sportsmailserver.service.CartService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author itning | ||
* @date 2020/2/12 16:44 | ||
*/ | ||
@RestController | ||
public class CartController { | ||
private final CartService cartService; | ||
|
||
@Autowired | ||
public CartController(CartService cartService) { | ||
this.cartService = cartService; | ||
} | ||
|
||
/** | ||
* 添加到购物车 | ||
* | ||
* @param loginUser 登录用户 | ||
* @param commodityId 商品ID | ||
* @param num 数量 | ||
* @param cumulative 累加 | ||
* @return 添加的商品 | ||
*/ | ||
@PostMapping("/cart") | ||
public ResponseEntity<?> addToCart(@MustUserLogin LoginUser loginUser, | ||
@RequestParam String commodityId, | ||
@RequestParam(defaultValue = "1") int num, | ||
@RequestParam(defaultValue = "true") boolean cumulative) { | ||
return RestModel.created(cartService.addToCart(loginUser, commodityId, num, cumulative)); | ||
} | ||
|
||
/** | ||
* 获取某用户所有购物车 | ||
* | ||
* @param loginUser 登录用户 | ||
* @param pageable 分页 | ||
* @return ResponseEntity | ||
*/ | ||
@GetMapping("carts") | ||
public ResponseEntity<?> getCarts(@MustUserLogin LoginUser loginUser, | ||
@PageableDefault( | ||
size = 20, sort = {"gmtModified"}, | ||
direction = Sort.Direction.DESC | ||
) | ||
Pageable pageable) { | ||
return RestModel.ok(cartService.getAll(loginUser, pageable)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/sport/sportsmailserver/service/CartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.sport.sportsmailserver.service; | ||
|
||
import com.sport.sportsmailserver.dto.LoginUser; | ||
import com.sport.sportsmailserver.entity.Cart; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
/** | ||
* 购物车服务 | ||
* | ||
* @author itning | ||
* @date 2020/2/12 16:45 | ||
*/ | ||
public interface CartService { | ||
/** | ||
* <p>商品加入购物车 | ||
* <p>每调用一次商品数量变为num | ||
* | ||
* @param loginUser 登录用户 | ||
* @param commodityId 商品ID | ||
* @param num 数量 | ||
* @param cumulative 累加 | ||
* @return 加入的购物车 | ||
*/ | ||
Cart addToCart(LoginUser loginUser, String commodityId, int num, boolean cumulative); | ||
|
||
/** | ||
* 获取所有购物车 | ||
* | ||
* @param loginUser 登录用户 | ||
* @param pageable 分页 | ||
* @return 购物车集合 | ||
*/ | ||
Page<Cart> getAll(LoginUser loginUser, Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/sport/sportsmailserver/service/impl/CartServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.sport.sportsmailserver.service.impl; | ||
|
||
import com.sport.sportsmailserver.dto.LoginUser; | ||
import com.sport.sportsmailserver.entity.Cart; | ||
import com.sport.sportsmailserver.entity.CartPrimaryKey; | ||
import com.sport.sportsmailserver.entity.Commodity; | ||
import com.sport.sportsmailserver.entity.User; | ||
import com.sport.sportsmailserver.exception.IdNotFoundException; | ||
import com.sport.sportsmailserver.exception.SecurityServerException; | ||
import com.sport.sportsmailserver.repository.CartRepository; | ||
import com.sport.sportsmailserver.repository.CommodityRepository; | ||
import com.sport.sportsmailserver.service.CartService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* @author itning | ||
* @date 2020/2/12 16:46 | ||
*/ | ||
@Service | ||
@Transactional(rollbackFor = Exception.class) | ||
public class CartServiceImpl implements CartService { | ||
private final CartRepository cartRepository; | ||
private final CommodityRepository commodityRepository; | ||
|
||
@Autowired | ||
public CartServiceImpl(CartRepository cartRepository, CommodityRepository commodityRepository) { | ||
this.cartRepository = cartRepository; | ||
this.commodityRepository = commodityRepository; | ||
} | ||
|
||
@Override | ||
public Cart addToCart(LoginUser loginUser, String commodityId, int num, boolean cumulative) { | ||
Commodity cc = commodityRepository.findById(commodityId).orElseThrow(() -> new IdNotFoundException("商品不存在")); | ||
if (cc.isTakeOff()) { | ||
throw new SecurityServerException("商品已下架", HttpStatus.NOT_FOUND); | ||
} | ||
CartPrimaryKey cartPrimaryKey = new CartPrimaryKey(); | ||
cartPrimaryKey.setUser(loginUser.getUsername()); | ||
cartPrimaryKey.setCommodity(commodityId); | ||
Optional<Cart> cartOptional = cartRepository.findById(cartPrimaryKey); | ||
Cart cart; | ||
if (cartOptional.isPresent()) { | ||
// 商品已经在购物车里了 | ||
cart = cartOptional.get(); | ||
if (cumulative) { | ||
cart.setCountNum(cart.getCountNum() + num); | ||
} else { | ||
cart.setCountNum(num); | ||
} | ||
} else { | ||
User user = new User(); | ||
user.setUsername(loginUser.getUsername()); | ||
|
||
cart = new Cart(); | ||
cart.setCountNum(num); | ||
cart.setUser(user); | ||
cart.setCommodity(cc); | ||
} | ||
return cartRepository.save(cart); | ||
} | ||
|
||
@Override | ||
public Page<Cart> getAll(LoginUser loginUser, Pageable pageable) { | ||
User user = new User(); | ||
user.setUsername(loginUser.getUsername()); | ||
return cartRepository.findAllByUser(user, pageable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters