master
parent
f218d9c3c9
commit
bd8a678259
@ -1,4 +1,55 @@
|
|||||||
package com.book.demo.controller;
|
package com.book.demo.controller;
|
||||||
|
|
||||||
|
import com.book.demo.entity.Cart;
|
||||||
|
import com.book.demo.mapper.CartMapper;
|
||||||
|
import com.book.demo.services.Result;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@Controller
|
||||||
public class CartController {
|
public class CartController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CartMapper cartMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
Result res;
|
||||||
|
|
||||||
|
//添加商品进购物车 id为isbn
|
||||||
|
@ResponseBody
|
||||||
|
@GetMapping("/addintocart")
|
||||||
|
public Result AddBookIn(@RequestParam("username")String username,@RequestParam("id") int id){
|
||||||
|
//把数量默认都设为1
|
||||||
|
int state=cartMapper.insertItem(username,id);
|
||||||
|
if(state>0){
|
||||||
|
res.setCode(0);
|
||||||
|
res.setMsg("添加成功");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
res.setCode(-1);
|
||||||
|
res.setMsg("添加失败");
|
||||||
|
}
|
||||||
|
res.data=null;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
//清空购物车
|
||||||
|
@ResponseBody
|
||||||
|
@GetMapping("/clearCart")
|
||||||
|
public Result clearCart(@RequestParam("username")String username){
|
||||||
|
cartMapper.clearCart(username);
|
||||||
|
Cart cart=cartMapper.queryByName(username);
|
||||||
|
if(cart==null){
|
||||||
|
res.setCode(0);
|
||||||
|
res.setMsg("清空成功");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
res.setCode(-1);
|
||||||
|
res.setMsg("清空失败");
|
||||||
|
}
|
||||||
|
res.data=null;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.book.demo.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Cart {
|
||||||
|
private String username;
|
||||||
|
private String bookname;
|
||||||
|
private float price;
|
||||||
|
private int count;
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.book.demo.entity;
|
||||||
|
|
||||||
|
public class Order {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.book.demo.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Mapper
|
||||||
|
public interface CartMapper {
|
||||||
|
}
|
Loading…
Reference in new issue