第1关:添加收藏
任务描述
本关任务:实现查询用户收藏的商品功能。
相关知识
为了完成本关任务,你需要掌握:
- 添加用户收藏的商品;
- 功能实现。
添加用户收藏的商品
如下图是商品页面的展示:如果我们喜欢该商品那么就可以收藏该商品。
一般流程为:点击收藏—>前端携带用户 id 与商品 id 请求服务器—>服务器判断用户是否收藏该商品—>添加数据库。
功能实现
-
dao 层写好查询用户是否收藏了该商品和添加数据至收藏表的方法。 CollectDao:
//根据用户id,商品id 查询收藏表是否有数据
@Select("select count(1) from collect where user_id=#{user_id} and product_id=#{product_id}")
int findCollect(int user_id,int product_id);
//添加收藏表
@Insert("insert into collect(user_id,product_id,collect_time) values(#{user_id},#{product_id},now())")
void addCollect(int user_id,int product_id);
-
service 层定义接口且 Impl 实现类调用 dao 接口方法。 CollectService:
//定义 service 接口,查询该收藏是否存在
int findCollect(int user_id,int product_id);
//定义 service 接口,添加收藏
void addCollect(int user_id,int product_id);
CollectServiceImpl:
/**
* 添加收藏
* @param user_id 用户id
* @param product_id 商品id
*/
@Override
public void addCollect(int user_id, int product_id) {
collectDao.addCollect(user_id,product_id);
}
/**
* 查询收藏是否存在
* @param user_id 用户id
* @param product_id 商品id
* @return
*/
@Override
public int findCollect(int user_id, int product_id) {
return collectDao.findCollect(user_id,product_id);
}
-
controller 层:判断用户是否收藏商品,收藏了就直接返回结果集,没收藏就添加收藏返回结果集。
/**
* 添加收藏
* @param paramMap
* @return
*/
@RequestMapping(value = "/addCollect")
public Map addCollect(@RequestBody Map<String,Integer> paramMap){
//接收用户 id
Integer user_id = paramMap.get("user_id");
//接收商品 id
Integer product_id = paramMap.get("product_id");
//定义 map 集合
HashMap<String, Object> map = new HashMap<>();
//查询该用户是否收藏该商品
if(collectService.findCollect(user_id,product_id)<1){
//未收藏,就调用 service 的收藏表
collectService.addCollect(user_id,product_id);
//收藏成功后返回map集合 "code":"001","msg":"收藏成功"
map.put("code","001");
map.put("msg","收藏成功");
}else{
//已收藏就返回map集合 "code":"003","msg":"该商品已经添加收藏,请到我的收藏查看"
map.put("code","003");
map.put("msg","该商品已经添加收藏,请到我的收藏查看");
}
return map;
}
1.数据库如下: 商品表:
字段名称 | 类型 | 备注 | |
---|---|---|---|
product_id | int(11) | NOT NULL AUTO_INCREMENT | '商品id' |
product_name | char(100) | NOT NULL | '商品名' |
category_id | int(11) | NOT NULL | '类型id' |
product_title | char(30) | NOT NULL | '商品标题' |
product_intro | text | NOT NULL | '商品描述' |
product_picture | char(200) | DEFAULT NULL | '商品图片' |
product_price | decimal(10,2) | NOT NULL | '商品价格' |
product_selling_price | decimal(10,2) | NOT NULL | '商品售价' |
product_num | int(11) | NOT NULL | '商品存量' |
product_sales | int(11) | NOT NULL | '商品销售数量' |
status | int(11) | DEFAULT NULL | '商品状态' |
收藏表: | |||
字段名称 | 类型 | 备注 | |
------------ | ------------ | ------------ | ------------ |
id | int(11) | NOT NULL AUTO_INCREMENT | '收藏表主键id' |
user_id | int(11) | NOT NULL | '用户id' |
product_id | int(11) | NOT NULL | '商品id' |
collect_time | datetime | NOT NULL | '收藏时间' |
编程要求
在右侧 CollectDao.java、CollectService.java、CollectServiceImpl.ava、CollectController.java 文件里 Begin-End 处根据提示补充代码。
测试说明
平台会对你编写的代码进行测试:发送请求检验结果集是否正确。
测试输入:
{
"user_id":"1018",
"product_id":"1"
}
预期输出:
{"msg":"该商品已经添加收藏,请到我的收藏查看","code":"003"}
开始你的任务吧,祝你成功!
测试要求
这句话一定注意!!!
从任务描述里我们能看到平台是需要我们将输入参数的类型更改为整数而不是字符串,所以我们仅需将CollectController.java中的代码进行修改即可。
CollectController.java
package com.www.controller;
import com.www.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/collect")
public class CollectController {
@Autowired
private CollectService collectService;
/**
* 添加收藏
* @param paramMap
* @return
*/
@PostMapping(value = "/addCollect")
public Map<String, Object> addCollect(@RequestBody Map<String,Integer> paramMap){
//接收用户 id
Integer user_id = paramMap.get("user_id");
//接收商品 id
Integer product_id = paramMap.get("product_id");
//定义 map 集合
HashMap<String, Object> map = new HashMap<>();
//查询该用户是否收藏该商品
if(collectService.findCollect(user_id,product_id)<1){
//未收藏,就调用 service 的收藏表
collectService.addCollect(user_id,product_id);
//收藏成功后返回map集合 "code":"001","msg":"收藏成功"
map.put("code","001");
map.put("msg","收藏成功");
}else{
//已收藏就返回map集合 "code":"003","msg":"该商品已经添加收藏,请到我的收藏查看"
map.put("code","003");
map.put("msg","该商品已经添加收藏,请到我的收藏查看");
}
return map;
}
}
最后还是那句话,喜欢的别忘了你们的小心心哦。你们的小心心能让我快乐一整天呢!