Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
del cart
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Feb 12, 2020
1 parent 07e52ff commit b44a788
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
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;
import org.springframework.web.bind.annotation.*;

/**
* @author itning
Expand Down Expand Up @@ -51,7 +48,7 @@ public ResponseEntity<?> addToCart(@MustUserLogin LoginUser loginUser,
* @param pageable 分页
* @return ResponseEntity
*/
@GetMapping("carts")
@GetMapping("/carts")
public ResponseEntity<?> getCarts(@MustUserLogin LoginUser loginUser,
@PageableDefault(
size = 20, sort = {"gmtModified"},
Expand All @@ -60,4 +57,18 @@ public ResponseEntity<?> getCarts(@MustUserLogin LoginUser loginUser,
Pageable pageable) {
return RestModel.ok(cartService.getAll(loginUser, pageable));
}

/**
* 删除购物车某个商品
*
* @param loginUser 登录用户
* @param commodityId 商品ID
* @return ResponseEntity
*/
@DeleteMapping("/cart/{commodityId}")
public ResponseEntity<?> delCart(@MustUserLogin LoginUser loginUser,
@PathVariable String commodityId) {
cartService.delCart(loginUser, commodityId);
return RestModel.noContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -20,4 +21,12 @@ public interface CartRepository extends JpaRepository<Cart, CartPrimaryKey> {
* @return 购物车集合
*/
Page<Cart> findAllByUser(User user, Pageable pageable);

/**
* 删除购物车某样商品
*
* @param commodity 商品
* @param user 用户
*/
void deleteByCommodityAndUser(Commodity commodity, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public interface CartService {
* @return 购物车集合
*/
Page<Cart> getAll(LoginUser loginUser, Pageable pageable);

/**
* 删除购物车某样商品
*
* @param loginUser 登录用户
* @param commodityId 商品ID
*/
void delCart(LoginUser loginUser, String commodityId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public Page<Cart> getAll(LoginUser loginUser, Pageable pageable) {
user.setUsername(loginUser.getUsername());
return cartRepository.findAllByUser(user, pageable);
}

@Override
public void delCart(LoginUser loginUser, String commodityId) {
Commodity cc = commodityRepository.findById(commodityId).orElseThrow(() -> new IdNotFoundException("商品不存在"));

User user = new User();
user.setUsername(loginUser.getUsername());

cartRepository.deleteByCommodityAndUser(cc, user);
}
}

0 comments on commit b44a788

Please sign in to comment.