VideoOrder下单接口
RequestEntity
package net.xdclass.online_xdclass.model.request;
import com.fasterxml.jackson.annotation.JsonProperty;
public class VideoOrderRequest {
@JsonProperty("video_id")
private int videoId;
public int getVideoId() {
return videoId;
}
public void setVideoId(int videoId) {
this.videoId = videoId;
}
}
Controller
package net.xdclass.online_xdclass.controller;
import net.xdclass.online_xdclass.model.request.VideoOrderRequest;
import net.xdclass.online_xdclass.service.VideoOrderService;
import net.xdclass.online_xdclass.utils.JsonData;
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 javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {
@Autowired
private VideoOrderService videoOrderService;
/**
* 下单接口
* @return
*/
@RequestMapping("save")
public JsonData saveOrder(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request){
//从requestyu域中获取user_id信息
Integer userId = (Integer) request.getAttribute("user_id");
//根据 user_id和video_id 完成video_order的下单
int rows = videoOrderService.save(userId, videoOrderRequest.getVideoId());
return rows == 0 ? JsonData.buildError("下单失败"):JsonData.buildSuccess();
}
}
Service
interface
package net.xdclass.online_xdclass.service;
public interface VideoOrderService {
int save(int userId, int videoId);
}
impl
package net.xdclass.online_xdclass.service.impl;
import net.xdclass.online_xdclass.mapper.UserMapper;
import net.xdclass.online_xdclass.mapper.VideoMapper;
import net.xdclass.online_xdclass.mapper.VideoOrderMapper;
import net.xdclass.online_xdclass.model.entity.Video;
import net.xdclass.online_xdclass.model.entity.VideoOrder;
import net.xdclass.online_xdclass.model.request.VideoOrderRequest;
import net.xdclass.online_xdclass.service.VideoOrderService;
import net.xdclass.online_xdclass.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;
@Service
public class VideoOrderServiceImpl implements VideoOrderService {
@Autowired
private VideoOrderMapper videoOrderMapper;
@Autowired
private VideoMapper videoMapper;
/**
* 下单操作
* 未来版本:优惠券抵扣,风控用户检查,生成订单基础信息,生成支付信息
* @param userId
* @param videoId
* @return
*/
@Override
public int save(int userId, int videoId) {
//判断是否已经购买
VideoOrder videoOrder = videoOrderMapper.findByUserIdAndVideoIdAndState(userId,videoId,1);
//如果已经购买就返回0
if(videoOrder!=null){return 0;}
//根据video_id查询相关的视频信息
Video video = videoMapper.findById(videoId);
//补全video_order的项目信息
VideoOrder newVideoOrder = new VideoOrder();
newVideoOrder.setCreateTime(new Date());
newVideoOrder.setOutTradeNo(UUID.randomUUID().toString());
newVideoOrder.setState(1);
newVideoOrder.setTotalFee(video.getPrice());
newVideoOrder.setUserId(userId);
newVideoOrder.setVideoId(videoId);
newVideoOrder.setVideoImg(video.getCoverImg());
newVideoOrder.setVideoTitle(video.getTitle());
//保存 video_order
int rows = videoOrderMapper.saveOrder(newVideoOrder);
return rows;
}
}
Dao
mapper
package net.xdclass.online_xdclass.mapper;
import net.xdclass.online_xdclass.model.entity.VideoOrder;
import org.apache.ibatis.annotations.Param;
public interface VideoOrderMapper {
/**
* 查询用户是否购买过此商品
* @param userId
* @param vidoeId
* @param state
* @return
*/
VideoOrder findByUserIdAndVideoIdAndState(@Param("user_id") int userId, @Param("video_id") int videoId, @Param("state") int state);
/**
* 下单
* @return
*/
int saveOrder(VideoOrder videoOrder);
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="net.xdclass.online_xdclass.mapper.VideoOrderMapper">
<select id="findByUserIdAndVideoIdAndState" resultType="VideoOrder">
select * from video_order where user_id = #{user_id} and video_id = #{video_id} and state = #{state}
</select>
<insert id="saveOrder" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO `video_order` (`out_trade_no`, `state`, `create_time`, `total_fee`, `video_id`,
`video_title`, `video_img`, `user_id`)
VALUES
(#{outTradeNo,jdbcType=VARCHAR},#{state,jdbcType=INTEGER},#{createTime,jdbcType=TIMESTAMP},#{totalFee,jdbcType=INTEGER},
#{videoId,jdbcType=INTEGER},#{videoTitle,jdbcType=VARCHAR},#{videoImg,jdbcType=VARCHAR},#{userId,jdbcType=INTEGER});
</insert>
</mapper>
测试
http://localhost:8081/api/v1/pri/order/save
POST Body-raw-JSON
{
"video_id":45
}
Headers
token
xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwiaGVhZF9pbWciOiJodHRwczovL3hkLXZpZGVvLXBjLWltZy5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20veGRjbGFzc19wcm8vZGVmYXVsdC9oZWFkX2ltZy8xNS5qcGVnIiwiaWQiOjgsIm5hbWUiOiJhbGljZSIsImlhdCI6MTYxMDUwMzM5NSwiZXhwIjoxNjExMTA4MTk1fQ.Hru8k9bwXd32t2iQX-6br_30M-HwiYKVerfChv_Recw