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