取消收藏(头歌)

第1关:取消收藏

任务描述

本关任务:实现删除用户收藏的商品功能。

相关知识

为了完成本关任务,你需要掌握:

  1. 删除用户收藏的商品;
  2. 功能实现。
删除用户收藏的商品

如下图是商品收藏页面的展示:如果我们不喜欢该商品那么就可以删除该商品收藏。

一般流程为:点击删除收藏—>前端携带用户 id 与商品 id 请求服务器—>服务器判断用户是否收藏该商品—>删除数据库收藏表该条数据。

功能实现
  1. dao 层写好查询用户是否收藏了该商品和删除收藏表数据的方法。 CollectDao:

     
      
    1. //根据用户id,商品id 查询收藏表是否有数据
    2. //根据用户id,商品id 查询收藏表是否有数据
    3. @Select("select count(1) from collect where user_id=#{user_id} and product_id=#{product_id}")
    4. int findCollect(int user_id,int product_id);
    5. //根据 user_id、product_id 删除 collect 表中的信息
    6. @Delete("delete from collect where user_id=#{user_id} and product_id=#{product_id}")
    7. void deleteCollect(int user_id, int product_id);
  2. service 层定义接口且 Impl 实现类调用 dao 接口方法。 CollectService:

     
      
    1. //定义 service 接口,查询该收藏是否存在
    2. int findCollect(int user_id,int product_id);
    3. //定义 service 接口,删除收藏
    4. void deleteCollect(int user_id, int product_id);

    CollectServiceImpl:

     
      
    1. /**
    2. * 查询收藏是否存在
    3. * @param user_id 用户id
    4. * @param product_id 商品id
    5. * @return
    6. */
    7. @Override
    8. public int findCollect(int user_id, int product_id) {
    9. return collectDao.findCollect(user_id,product_id);
    10. }
    11. /**
    12. * 删除收藏
    13. * @param user_id
    14. * @param product_id
    15. */
    16. @Override
    17. public void deleteCollect(int user_id, int product_id) {
    18. collectDao.deleteCollect(user_id,product_id);
    19. }
  3. controller 层:判断用户是否收藏商品,收藏了就调用 service 层方法删除该收藏,没收藏就直接返回结果集。

     
      
    1. /**
    2. * 删除收藏
    3. * @param paramMap
    4. * @return
    5. */
    6. @RequestMapping(value = "/deleteCollect")
    7. public Map deleteCollect(@RequestBody Map<String,Integer> paramMap){
    8. //接收参数用户id: user_id
    9. Integer user_id = paramMap.get("user_id");
    10. //接收参数商品id: product_id
    11. Integer product_id = paramMap.get("product_id");
    12. //定义结果集 map 集合
    13. HashMap<String, String> map = new HashMap<>();
    14. //调用 service 方法,查询该用户是否收藏该商品
    15. if(collectService.findCollect(user_id,product_id)>0){
    16. //调用 service 层删除收藏方法,收藏了该商品就删除该收藏
    17. collectService.deleteCollect(user_id,product_id);
    18. //删除收藏后返回 map 集合: “code”:"001","msg":"取消收藏成功"
    19. map.put("code","001");
    20. map.put("msg","取消收藏成功");
    21. }else {
    22. //返回 map 集合: “code”:"003","msg":"取消收藏失败"
    23. map.put("code","003");
    24. map.put("msg","取消收藏失败");
    25. }
    26. return map;
    27. }
  4. 数据库如下: 商品表:

    字段名称类型备注
    product_idint(11)NOT NULL AUTO_INCREMENT'商品id'
    product_namechar(100)NOT NULL'商品名'
    category_idint(11)NOT NULL'类型id'
    product_titlechar(30)NOT NULL'商品标题'
    product_introtextNOT NULL'商品描述'
    product_picturechar(200)DEFAULT NULL'商品图片'
    product_pricedecimal(10,2)NOT NULL'商品价格'
    product_selling_pricedecimal(10,2)NOT NULL'商品售价'
    product_numint(11)NOT NULL'商品存量'
    product_salesint(11)NOT NULL'商品销售数量'
    statusint(11)DEFAULT NULL'商品状态'

收藏表:

字段名称类型备注
idint(11)NOT NULL AUTO_INCREMENT'收藏表主键id'
user_idint(11)NOT NULL'用户id'
product_idint(11)NOT NULL'商品id'
collect_timedatetimeNOT NULL'收藏时间'

编程要求

在右侧 CollectDao.java、CollectService.java、CollectServiceImpl.ava、CollectController.java 文件里 Begin-End 处根据提示补充代码。

测试说明

平台会对你编写的代码进行测试:发送请求检验结果集是否正确。

测试输入:

 
  1. {
  2. "user_id":"1018",
  3. "product_id":"1"
  4. }

预期输出:

 
  1. {"msg":"取消收藏成功","code":"001"}

开始你的任务吧,祝你成功!

CollectDao.java
package com.www.dao;


import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface CollectDao {
    //根据用户id,商品id 查询收藏表是否有数据
    /**********************Begin**********************/
     @Select("select count(1) from collect where user_id=#{user_id} and product_id=#{product_id}")
 int findCollect(int user_id,int product_id);
    /**********************End**********************/

    //根据 user_id、product_id 删除 collect 表中的信息
    /**********************Begin**********************/
     @Delete("delete from collect where user_id=#{user_id} and product_id=#{product_id}")
 void deleteCollect(int user_id, int product_id);
    /**********************End**********************/
}
CollectService.java
package com.www.service;

import org.springframework.stereotype.Component;

@Component
public interface CollectService {
    //定义 service 接口,查询该收藏是否存在
    /**********************Begin**********************/
     int findCollect(int user_id,int product_id);
    /**********************End**********************/

    //定义 service 接口,删除收藏
    /**********************Begin**********************/
     void deleteCollect(int user_id, int product_id);
    /**********************End**********************/
}
CollectServiceImpl.java
package com.www.service.impl;

import com.www.dao.CollectDao;
import com.www.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("collectService")
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectDao collectDao;


    /**
     * 查询收藏是否存在
     * @param user_id 用户id
     * @param product_id 商品id
     * @return
     */
    @Override
    public int findCollect(int user_id, int product_id) {
        //调用 dao 层方法,查询用户是否收藏该条商品
        /**********************Begin**********************/
     return collectDao.findCollect(user_id,product_id);
        /**********************End**********************/
    }

    /**
     * 删除收藏
     * @param user_id
     * @param product_id
     */
    @Override
    public void deleteCollect(int user_id, int product_id) {
        //调用 dao 层方法,删除用户收藏
        /**********************Begin**********************/
        collectDao.deleteCollect(user_id,product_id);
        /**********************End**********************/
    }
}
CollectController.java
package com.www.controller;

import com.www.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 删除收藏
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/deleteCollect")
    public Map deleteCollect(@RequestBody Map<String,Integer> paramMap){
        /*************************Begin*************************/
             //接收参数用户id: user_id
     Integer user_id = paramMap.get("user_id");
     //接收参数商品id: product_id
     Integer product_id = paramMap.get("product_id");
     //定义结果集 map 集合
     HashMap<String, String> map = new HashMap<>();
     //调用 service 方法,查询该用户是否收藏该商品
     if(collectService.findCollect(user_id,product_id)>0){
         //调用 service 层删除收藏方法,收藏了该商品就删除该收藏
         collectService.deleteCollect(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;
        /*************************End*************************/
    }
}

Collect.java

package com.www.entity;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;


@Data
public class Collect implements Serializable {

    private int id;
    private int user_id;
    private int product_id;
    private String collect_time;
    private String product_picture;
    private String product_name;
    private String product_title;
    private BigDecimal product_selling_price;
    private BigDecimal product_price;

}
Product.java
package com.www.entity;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;


@Data
public class Product implements Serializable {

    private int product_id;
    private String product_name;
    private int category_id;
    private String product_title;
    private String product_intro;
    private String product_picture;
    private BigDecimal product_price;   //原价
    private BigDecimal product_selling_price;    //售价
    private int product_num;
    private int product_sales;
    private int status;
}

加油哦,同学们!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值