【全栈】SprintBoot+vue3迷你商城(4)

【全栈】SprintBoot+vue3迷你商城(4)

本期我们继续来写与用户相关的接口。

我们用户分为两种:普通用户商家

普通用户可以显示他们的基本信息;可以将商品添加到购物车;可以查看他们购物车中的信息

而商家除了这些之外,还可以添加商品

我们会在接下来逐步实现

  • 注:接下来的代码都在User相关的层中续写

    比如说,登录的控制器就在UserController中续写,方法就在UserService中续写……

1.显示用户的基本信息

显示用户信息,直接从数据库中取就行了

controller层

 @GetMapping("/userInfo")
    public Result userInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByName(username);
        return Result.success(user);
    }

service层

 User findByName(String username);

方法实现

 @Override
    public User findByName(String username) {
        return userMapper.findByName(username);
    }

mapper层

@Select("select * from user where username=#{username}")
    User findByName(String username);

之后可以在postman中进行接口测试,在【全栈】SprintBoot+vue3迷你商城(3)中有具体演示

2.将商品添加到购物车

将商品添加到购物车中去,就是将用户添加的商品储存到数据库中,那么我们如何储存呢?

我采用的是商品ID加逗号‘,’的方式,例如:1,5,7,8

添加商品的时候,只需要加一个逗号,再将要添加的商品ID拼接上即可

所以我们直接可以写逻辑:

controller层

    @PatchMapping("/cart/add")
    public Result addGoods(@RequestParam("id") Integer id){
        if(goodsService.findGoodsById(id)==null){
            Result.error("商品不存在");
        }
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByName(username);
        String cartIdList = user.getCartIdList();
        cartIdList=(cartIdList==null)? "" +id:cartIdList+","+id;
        userService.addGoodsToCart(cartIdList,user.getId());
        return  Result.success();
    }

service层

void addGoodsToCart(String cartIdList,Integer id);

方法实现

  @Override
    public void addGoodsToCart(String cartIdList,Integer id) {
        userMapper.addGoodsToCart(cartIdList,id);
    }

mapper层

@Update("update  user set cartIdList=#{cartIdList} where id=#{id}")
    void addGoodsToCart(String cartIdList,Integer id);

之后可以在postman中进行接口测试

3.查看购物车中的信息

前面讲到将商品添加到购物车中去,我们显示购物车中的信息就可以将商品ID字符串以逗号分隔开来,得到每个商品ID了

而得到ID之后,我们可以写一个通过ID找到商品的接口,这样就可以得到商品信息了

再者,由于我们不仅需要得到商品信息,也需要得到发布该商品的商家的用户名以及头像,所以我们需要再建立一个model来表示在购物车中显示商品的信息

package com.janium.minimallbe.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;


@Data
@AllArgsConstructor
public class GoodsShowModel {
    private Integer id;
    private String goodsName;
    private String goodsImgUrl;
    private float goodsPrice;
    private String merchantName;
    private String merchantImgUrl;
}

然后就可以实现逻辑:

controller层

 @GetMapping("/cart")
    public Result getCartList(){
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByName(username);
        String strCartIdList= user.getCartIdList();
        String[] cartIdList = strCartIdList.split(",");
        List<GoodsShowModel> goodsShowModelList = new ArrayList<>();
        for (String s : cartIdList) {
            Goods goods=goodsService.findGoodsById(Integer.valueOf(s));
            User merchant=userService.findById(goods.getMerchantId());
            GoodsShowModel goodsShowModel=new GoodsShowModel(goods.getId(),goods.getGoodsName(),goods.getGoodsImgUrl(),
                    goods.getGoodsPrice(),merchant.getUsername(),merchant.getUserPic());
            goodsShowModelList.add(goodsShowModel);
        }
        return Result.success(goodsShowModelList);
    }

service层

 // 这个方法可以写到GoodsService中去
Goods findGoodsById(Integer id);

方法实现

@Override
    public Goods findGoodsById(Integer id) {
        return goodsMapper.findGoodsById(id);
    }

mapper层

 @Select("select * from goods where id=#{id}")
    Goods findGoodsById(Integer id);

之后可以在postman中进行接口测试

4.总结

本期我们基本完成了与用户相关的接口,而商家添加商品涉及到商品表,所以我们放在接下来的与商品相关的接口中来实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值