commit
a5ebfcedf0
16 changed files with 367 additions and 251 deletions
@ -1,65 +1,65 @@ |
||||
//package com.teaching.backend.controller.thumb;
|
||||
//
|
||||
//import com.teaching.backend.common.BaseResponse;
|
||||
//import com.teaching.backend.common.ErrorCode;
|
||||
//import com.teaching.backend.exception.BusinessException;
|
||||
//import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAddRequest;
|
||||
//import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAllRequest;
|
||||
//import com.teaching.backend.service.thumb.SeCourseThumbService;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.apache.commons.lang3.StringUtils;
|
||||
//import org.springframework.web.bind.annotation.PostMapping;
|
||||
//import org.springframework.web.bind.annotation.RequestBody;
|
||||
//import org.springframework.web.bind.annotation.RequestMapping;
|
||||
//import org.springframework.web.bind.annotation.RestController;
|
||||
//
|
||||
//import javax.annotation.Resource;
|
||||
//
|
||||
///**
|
||||
// * @Author:youhang
|
||||
// * @Date:2024-05-30-20:22
|
||||
// * @Description:
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/api/course_thumb")
|
||||
//@Slf4j
|
||||
//public class SeCourseThumbController {
|
||||
//
|
||||
// @Resource
|
||||
// private SeCourseThumbService seCourseThumbService;
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 点赞/取消点赞
|
||||
// *
|
||||
// * @param courseThumbAddRequest
|
||||
// * @return result 执行情况
|
||||
// */
|
||||
// @PostMapping("/")
|
||||
// // HttpServletRequest request 在一个fitter里面进行鉴权操作
|
||||
// public BaseResponse<Boolean> doThumb(@RequestBody CourseThumbAddRequest courseThumbAddRequest) {
|
||||
//
|
||||
// return seCourseThumbService.doCourseThumb(courseThumbAddRequest.getCourseId(), courseThumbAddRequest.getUserId());
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 课程点赞总数
|
||||
// *
|
||||
// * @param courseThumbAddRequest
|
||||
// * @return result 课程点赞总数
|
||||
// */
|
||||
// @PostMapping("/count")
|
||||
// // HttpServletRequest request 在一个fitter里面进行鉴权操作
|
||||
// public BaseResponse<Long> thumbCount(@RequestBody CourseThumbAddRequest courseThumbAddRequest) {
|
||||
// if (StringUtils.isAnyBlank(courseThumbAddRequest.getCourseId())) {
|
||||
// throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
|
||||
// }
|
||||
//
|
||||
// return seCourseThumbService.thumbCount(courseThumbAddRequest.getCourseId());
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
package com.teaching.backend.controller.thumb; |
||||
|
||||
import com.teaching.backend.common.BaseResponse; |
||||
import com.teaching.backend.common.ErrorCode; |
||||
import com.teaching.backend.exception.BusinessException; |
||||
import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAddRequest; |
||||
import com.teaching.backend.model.dto.thumb.coursethumb.CourseThumbAllRequest; |
||||
import com.teaching.backend.service.thumb.SeCourseThumbService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
/** |
||||
* @Author:youhang |
||||
* @Date:2024-05-30-20:22 |
||||
* @Description: |
||||
*/ |
||||
@RestController |
||||
@RequestMapping("/api/course_thumb") |
||||
@Slf4j |
||||
public class SeCourseThumbController { |
||||
|
||||
@Resource |
||||
private SeCourseThumbService seCourseThumbService; |
||||
|
||||
|
||||
/** |
||||
* 点赞/取消点赞 |
||||
* |
||||
* @param courseThumbAddRequest |
||||
* @return result 执行情况 |
||||
*/ |
||||
@PostMapping("/") |
||||
// HttpServletRequest request 在一个fitter里面进行鉴权操作
|
||||
public BaseResponse<Boolean> doThumb(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { |
||||
|
||||
return seCourseThumbService.doCourseThumb(courseThumbAddRequest.getCourseId(), courseThumbAddRequest.getUserId()); |
||||
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* 课程点赞总数 |
||||
* |
||||
* @param courseThumbAddRequest |
||||
* @return result 课程点赞总数 |
||||
*/ |
||||
@PostMapping("/count") |
||||
// HttpServletRequest request 在一个fitter里面进行鉴权操作
|
||||
public BaseResponse<Long> thumbCount(@RequestBody CourseThumbAddRequest courseThumbAddRequest) { |
||||
if (StringUtils.isAnyBlank(courseThumbAddRequest.getCourseId())) { |
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空"); |
||||
} |
||||
|
||||
return seCourseThumbService.thumbCount(courseThumbAddRequest.getCourseId()); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -1,108 +1,100 @@ |
||||
//package com.teaching.backend.service.impl.thumb;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
//import com.teaching.backend.common.BaseResponse;
|
||||
//import com.teaching.backend.common.ErrorCode;
|
||||
//import com.teaching.backend.common.ResultUtils;
|
||||
//import com.teaching.backend.exception.BusinessException;
|
||||
//import com.teaching.backend.mapper.courses.CoursesMapper;
|
||||
//import com.teaching.backend.mapper.thumb.SeCourseThumbMapper;
|
||||
//import com.teaching.backend.model.entity.courses.Courses;
|
||||
//import com.teaching.backend.model.entity.thumb.SeCourseThumb;
|
||||
//import com.teaching.backend.model.entity.thumb.SeKnowThumb;
|
||||
//import com.teaching.backend.model.entity.thumb.SeResourceThumb;
|
||||
//import com.teaching.backend.service.thumb.SeCourseThumbService;
|
||||
//import com.teaching.backend.service.thumb.SeKnowThumbService;
|
||||
//import com.teaching.backend.service.thumb.SeResourceThumbService;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//
|
||||
//import javax.annotation.Resource;
|
||||
//import java.util.concurrent.locks.Lock;
|
||||
//import java.util.concurrent.locks.ReentrantLock;
|
||||
//
|
||||
//
|
||||
///**
|
||||
// * @Author:youhang
|
||||
// * @Date:2024-05-30-20:23
|
||||
// * @Description:
|
||||
// */
|
||||
//@Service
|
||||
//public class SeCourseThumbServiceImpl extends ServiceImpl<SeCourseThumbMapper, SeCourseThumb> implements SeCourseThumbService {
|
||||
//
|
||||
//
|
||||
// @Resource
|
||||
// private SeResourceThumbService seResourceThumbService;
|
||||
//
|
||||
// @Resource
|
||||
// SeKnowThumbService seKnowThumbService;
|
||||
//
|
||||
//
|
||||
// @Autowired
|
||||
// CoursesMapper coursesMapper;
|
||||
//
|
||||
// @Override
|
||||
// public BaseResponse<Boolean> doCourseThumb(String courseId, String userId) {
|
||||
//
|
||||
// //从数据库中校验是否存在courseid
|
||||
// Courses courses = coursesMapper.selectById(courseId);
|
||||
// boolean result = false;
|
||||
// if(courses == null){
|
||||
// return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS);
|
||||
// }
|
||||
//
|
||||
// // 先查询数据库记录,该用户是否点赞
|
||||
// SeCourseThumb seCourseThumb = new SeCourseThumb();
|
||||
// seCourseThumb.setCourseId(courseId);
|
||||
// seCourseThumb.setUserId(userId);
|
||||
// QueryWrapper<SeCourseThumb> thumbQueryWrapper = new QueryWrapper<>(seCourseThumb);
|
||||
// SeCourseThumb oldSeCourseThumb = this.getOne(thumbQueryWrapper);
|
||||
// //已点赞
|
||||
// if (oldSeCourseThumb != null) {
|
||||
// // 取消点赞 删除记录
|
||||
// result = this.remove(thumbQueryWrapper);
|
||||
// if (result) {
|
||||
// System.out.println("取消点赞成功");
|
||||
// return ResultUtils.success(result);
|
||||
// } else {
|
||||
// throw new BusinessException(ErrorCode.SYSTEM_ERROR);
|
||||
// }
|
||||
// } else {
|
||||
// // 每个用户串行点赞
|
||||
// // 锁必须要包裹住事务方法
|
||||
// Lock lock = new ReentrantLock();
|
||||
// lock.lock();
|
||||
// try {
|
||||
// result = this.save(seCourseThumb);
|
||||
// } catch (Exception e) {
|
||||
// throw new BusinessException(ErrorCode.OPERATION_ERROR);
|
||||
// } finally {
|
||||
// lock.unlock();
|
||||
// System.out.println("点赞成功");
|
||||
// return ResultUtils.success(result);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public BaseResponse<Long> thumbCount(String courseId) {
|
||||
//
|
||||
// //从数据库中校验是否存在courseid
|
||||
// Courses courses = coursesMapper.selectById(courseId);
|
||||
// if(courses == null){
|
||||
// return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS);
|
||||
// }
|
||||
//
|
||||
// QueryWrapper<SeCourseThumb> thumbQueryWrapper = new QueryWrapper<>();
|
||||
// thumbQueryWrapper.eq("course_id", courseId);
|
||||
// long count = this.count(thumbQueryWrapper);
|
||||
// if (count == 0) {
|
||||
// throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询参数不存在");
|
||||
// } else {
|
||||
// return ResultUtils.success(count);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
package com.teaching.backend.service.impl.thumb; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import com.teaching.backend.common.BaseResponse; |
||||
import com.teaching.backend.common.ErrorCode; |
||||
import com.teaching.backend.common.ResultUtils; |
||||
import com.teaching.backend.exception.BusinessException; |
||||
import com.teaching.backend.mapper.courses.CoursesMapper; |
||||
import com.teaching.backend.mapper.thumb.SeCourseThumbMapper; |
||||
import com.teaching.backend.model.entity.courses.Courses; |
||||
import com.teaching.backend.model.entity.thumb.SeCourseThumb; |
||||
import com.teaching.backend.model.entity.thumb.SeKnowThumb; |
||||
import com.teaching.backend.model.entity.thumb.SeResourceThumb; |
||||
import com.teaching.backend.service.thumb.SeCourseThumbService; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.concurrent.locks.Lock; |
||||
import java.util.concurrent.locks.ReentrantLock; |
||||
|
||||
|
||||
/** |
||||
* @Author:youhang |
||||
* @Date:2024-05-30-20:23 |
||||
* @Description: |
||||
*/ |
||||
@Service |
||||
public class SeCourseThumbServiceImpl extends ServiceImpl<SeCourseThumbMapper, SeCourseThumb> implements SeCourseThumbService { |
||||
|
||||
|
||||
|
||||
@Autowired |
||||
CoursesMapper coursesMapper; |
||||
|
||||
@Override |
||||
public BaseResponse<Boolean> doCourseThumb(String courseId, String userId) { |
||||
|
||||
//从数据库中校验是否存在courseid
|
||||
Courses courses = coursesMapper.selectById(courseId); |
||||
boolean result = false; |
||||
if(courses == null){ |
||||
return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); |
||||
} |
||||
|
||||
// 先查询数据库记录,该用户是否点赞
|
||||
SeCourseThumb seCourseThumb = new SeCourseThumb(); |
||||
seCourseThumb.setCourseId(courseId); |
||||
seCourseThumb.setUserId(userId); |
||||
QueryWrapper<SeCourseThumb> thumbQueryWrapper = new QueryWrapper<>(seCourseThumb); |
||||
SeCourseThumb oldSeCourseThumb = this.getOne(thumbQueryWrapper); |
||||
//已点赞
|
||||
if (oldSeCourseThumb != null) { |
||||
// 取消点赞 删除记录
|
||||
result = this.remove(thumbQueryWrapper); |
||||
if (result) { |
||||
System.out.println("取消点赞成功"); |
||||
return ResultUtils.success(result); |
||||
} else { |
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR); |
||||
} |
||||
} else { |
||||
// 每个用户串行点赞
|
||||
// 锁必须要包裹住事务方法
|
||||
Lock lock = new ReentrantLock(); |
||||
lock.lock(); |
||||
try { |
||||
result = this.save(seCourseThumb); |
||||
} catch (Exception e) { |
||||
throw new BusinessException(ErrorCode.OPERATION_ERROR); |
||||
} finally { |
||||
lock.unlock(); |
||||
System.out.println("点赞成功"); |
||||
return ResultUtils.success(result); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public BaseResponse<Long> thumbCount(String courseId) { |
||||
|
||||
//从数据库中校验是否存在courseid
|
||||
Courses courses = coursesMapper.selectById(courseId); |
||||
if(courses == null){ |
||||
return ResultUtils.error(ErrorCode.PARAMS_COURSE_NOTEXISTS); |
||||
} |
||||
|
||||
QueryWrapper<SeCourseThumb> thumbQueryWrapper = new QueryWrapper<>(); |
||||
thumbQueryWrapper.eq("course_id", courseId); |
||||
long count = this.count(thumbQueryWrapper); |
||||
if (count == 0) { |
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询参数不存在"); |
||||
} else { |
||||
return ResultUtils.success(count); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -1,33 +1,33 @@ |
||||
//package com.teaching.backend.service.thumb;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.extension.service.IService;
|
||||
//import com.teaching.backend.common.BaseResponse;
|
||||
//import com.teaching.backend.model.entity.thumb.SeCourseThumb;
|
||||
//
|
||||
///**
|
||||
// * @Author:youhang
|
||||
// * @Date:2024-05-30-20:22
|
||||
// * @Description:
|
||||
// */
|
||||
//public interface SeCourseThumbService extends IService<SeCourseThumb> {
|
||||
//
|
||||
// /**
|
||||
// * 点赞
|
||||
// *
|
||||
// * @param courseId
|
||||
// * @param userId
|
||||
// * @return
|
||||
// */
|
||||
// BaseResponse<Boolean> doCourseThumb(String courseId, String userId);
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 点赞总数
|
||||
// *
|
||||
// * @param courseId
|
||||
// * @return
|
||||
// */
|
||||
// BaseResponse<Long> thumbCount(String courseId);
|
||||
//
|
||||
//
|
||||
//}
|
||||
package com.teaching.backend.service.thumb; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.teaching.backend.common.BaseResponse; |
||||
import com.teaching.backend.model.entity.thumb.SeCourseThumb; |
||||
|
||||
/** |
||||
* @Author:youhang |
||||
* @Date:2024-05-30-20:22 |
||||
* @Description: |
||||
*/ |
||||
public interface SeCourseThumbService extends IService<SeCourseThumb> { |
||||
|
||||
/** |
||||
* 点赞 |
||||
* |
||||
* @param courseId |
||||
* @param userId |
||||
* @return |
||||
*/ |
||||
BaseResponse<Boolean> doCourseThumb(String courseId, String userId); |
||||
|
||||
|
||||
/** |
||||
* 点赞总数 |
||||
* |
||||
* @param courseId |
||||
* @return |
||||
*/ |
||||
BaseResponse<Long> thumbCount(String courseId); |
||||
|
||||
|
||||
} |
||||
|
Loading…
Reference in new issue