@GetMapping("/recommend") public Result recommend() { Integer userid = TokenUtils.getCurrentUser().getId(); QueryWrapper<Goods> queryWrapper = new QueryWrapper<>(); queryWrapper.eq( "status", 1).eq("recommend", 1); List<Goods> list = goodsService.list(queryWrapper); for (Goods goods : list) { List<Praise> praises = praiseService.list(); List<Collect> collects = collectService.list(); Praise praise = praiseService.getOne(new QueryWrapper<Praise>().eq("userid", userid).eq("goodsid", goods.getId())); Collect collect = collectService.getOne(new QueryWrapper<Collect>().eq("userid", userid).eq("goodsid", goods.getId())); goods.setHasPraise(praise != null); goods.setHasCollect(collect != null); goods.setPraiseCount(praises.stream().filter(p -> p.getGoodsid().equals(goods.getId())).count()); goods.setCollectCount(collects.stream().filter(c -> c.getGoodsid().equals(goods.getId())).count()); } return Result.success(list); }
时间: 2023-06-20 18:07:09 浏览: 64
这段代码是一个 Spring Boot 的 Controller,使用了 GetMapping 注解映射了一个 "/recommend" 的接口。该接口会返回一个 Result 对象,其中包含了一个 List<Goods> 对象,这个 List 包含了所有状态为1且被推荐的商品。在获取商品列表后,代码会对每个商品进行一些处理。首先,它会从数据库中获取所有点赞和收藏记录,并检查当前用户是否已经对该商品进行了点赞或收藏。然后,它会计算出该商品的点赞数和收藏数,将这些信息赋值给该商品。最后,这个方法会将这个 List<Goods> 对象返回给前端。
相关问题
@GetMapping("/{id}") public Result findOne(@PathVariable Integer id) { Integer userid = TokenUtils.getCurrentUser().getId(); Goods goods = goodsService.getById(id); List<Praise> praises = praiseService.list(); List<Collect> collects = collectService.list(); Praise praise = praiseService.getOne(new QueryWrapper<Praise>().eq("userid", userid).eq("goodsid", id)); Collect collect = collectService.getOne(new QueryWrapper<Collect>().eq("userid", userid).eq("goodsid", id)); goods.setHasPraise(praise != null); goods.setHasCollect(collect != null); goods.setPraiseCount(praises.stream().filter(p -> p.getGoodsid().equals(goods.getId())).count()); goods.setCollectCount(collects.stream().filter(c -> c.getGoodsid().equals(goods.getId())).count()); return Result.success(goods); }
这是一个基于Spring框架的Java后端代码,使用了@GetMapping注解来映射HTTP GET请求。其中,@PathVariable注解用于获取请求路径中的id参数,TokenUtils.getCurrentUser()用于获取当前登录用户的id,goodsService、praiseService、collectService都是该系统中的服务组件,用于管理商品、点赞、收藏等功能。在返回结果之前,代码会通过查询判断当前用户是否已经点赞、收藏过该商品,并计算出该商品的点赞数和收藏数,最终将结果封装在Result对象中返回给前端。
@GetMapping public Result findAll() { return Result.success(addressService.list(new QueryWrapper<Address>().eq("userid", TokenUtils.getCurrentUser().getId()))); }
这段代码是一个基于Spring框架的RESTful API的接口实现,使用了@GetMapping注解表示这是一个GET请求。该接口的功能是返回当前用户的所有地址信息。
具体实现是通过调用addressService的list方法,并传入一个查询条件,查询条件是根据当前用户的ID查询其所有的地址信息。最后将查询结果通过Result.success方法封装成一个标准的响应格式并返回。