课程附件下载重构

This commit is contained in:
none 2023-07-29 15:01:39 +08:00
parent d697ded1ec
commit e28e2e30af
11 changed files with 79 additions and 92 deletions

View File

@ -16,10 +16,12 @@
package xyz.playedu.api.controller.backend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.CourseAttachment;
@ -62,11 +64,7 @@ public class CourseAttachmentController {
CourseAttachment courseAttachment =
attachmentService.create(
courseId,
req.getSort(),
req.getTitle(),
type,
req.getRid());
courseId, req.getSort(), req.getTitle(), type, req.getRid());
return JsonResponse.success();
}

View File

@ -16,9 +16,11 @@
package xyz.playedu.api.controller.backend;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.annotation.Log;
import xyz.playedu.api.constant.BusinessType;
import xyz.playedu.api.domain.*;
@ -50,7 +52,8 @@ public class CourseAttachmentDownloadLogController {
Integer courserAttachmentId = MapUtils.getInteger(params, "courser_attachment_id");
Integer rid = MapUtils.getInteger(params, "rid");
CourseAttachmentDownloadLogPaginateFiler filter = new CourseAttachmentDownloadLogPaginateFiler();
CourseAttachmentDownloadLogPaginateFiler filter =
new CourseAttachmentDownloadLogPaginateFiler();
filter.setUserId(userId);
filter.setCourseId(courseId);
filter.setTitle(title);
@ -59,7 +62,8 @@ public class CourseAttachmentDownloadLogController {
filter.setSortField(sortField);
filter.setSortAlgo(sortAlgo);
PaginationResult<CourseAttachmentDownloadLog> result = courseAttachmentDownloadLogService.paginate(page, size, filter);
PaginationResult<CourseAttachmentDownloadLog> result =
courseAttachmentDownloadLogService.paginate(page, size, filter);
HashMap<String, Object> data = new HashMap<>();
data.put("data", result.getData());

View File

@ -27,7 +27,6 @@ import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.annotation.Log;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.constant.BusinessType;
import xyz.playedu.api.domain.*;
import xyz.playedu.api.event.CourseDestroyEvent;
@ -234,13 +233,18 @@ public class CourseController {
List<Integer> categoryIds = courseService.getCategoryIdsByCourseId(course.getId());
List<CourseChapter> chapters = chapterService.getChaptersByCourseId(course.getId());
List<CourseHour> hours = hourService.getHoursByCourseId(course.getId());
List<CourseAttachment> attachments = attachmentService.getAttachmentsByCourseId(course.getId());
if(null != attachments && attachments.size() > 0){
Map<Integer,String> resourceMap = resourceService.chunks(attachments.stream().map(CourseAttachment::getRid).toList())
.stream().collect(Collectors.toMap(Resource::getId, Resource::getUrl));
attachments.forEach(courseAttachment -> {
courseAttachment.setUrl(resourceMap.get(courseAttachment.getRid()));
});
List<CourseAttachment> attachments =
attachmentService.getAttachmentsByCourseId(course.getId());
if (null != attachments && attachments.size() > 0) {
Map<Integer, String> resourceMap =
resourceService
.chunks(attachments.stream().map(CourseAttachment::getRid).toList())
.stream()
.collect(Collectors.toMap(Resource::getId, Resource::getUrl));
attachments.forEach(
courseAttachment -> {
courseAttachment.setUrl(resourceMap.get(courseAttachment.getRid()));
});
}
HashMap<String, Object> data = new HashMap<>();

View File

@ -1,60 +0,0 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.api.controller.frontend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.domain.*;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.*;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.util.IpUtil;
import java.util.Date;
@RestController
@Slf4j
@RequestMapping("/api/v1/course/{courseId}/attachment/{attachmentId}/resource")
public class CourseAttachmentDownloadLogController {
@Autowired private CourseService courseService;
@Autowired private CourseAttachmentDownloadLogService courseAttachmentDownloadLogService;
@PostMapping("/{id}")
public JsonResponse store(@PathVariable(name = "courseId") Integer courseId,
@PathVariable(name = "attachmentId") Integer attachmentId,
@PathVariable(name = "id") Integer id) throws NotFoundException {
Course course = courseService.findOrFail(courseId);
CourseAttachmentDownloadLog courseAttachmentDownloadLog =
new CourseAttachmentDownloadLog() {
{
setUserId(FCtx.getId());
setCourseId(course.getId());
setTitle(course.getTitle());
setCourserAttachmentId(attachmentId);
setRid(id);
setIp(IpUtil.getIpAddress());
setCreatedAt(new Date());
}
};
courseAttachmentDownloadLogService.save(courseAttachmentDownloadLog);
return JsonResponse.success();
}
}

View File

@ -27,10 +27,11 @@ import xyz.playedu.api.service.*;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.util.IpUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@ -56,6 +57,8 @@ public class CourseController {
@Autowired private UserCourseHourRecordService userCourseHourRecordService;
@Autowired private CourseAttachmentDownloadLogService courseAttachmentDownloadLogService;
@GetMapping("/index")
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
Integer page = MapUtils.getInteger(params, "page", 1);
@ -78,24 +81,48 @@ public class CourseController {
List<CourseHour> courseHours = hourService.getHoursByCourseId(course.getId());
List<CourseAttachment> attachments = attachmentService.getAttachmentsByCourseId(course.getId());
if(null != attachments && attachments.size() > 0){
Map<Integer,String> resourceMap = resourceService.chunks(attachments.stream().map(CourseAttachment::getRid).toList())
.stream().collect(Collectors.toMap(Resource::getId, Resource::getUrl));
attachments.forEach(courseAttachment -> {
courseAttachment.setUrl(resourceMap.get(courseAttachment.getRid()));
});
}
List<CourseAttachment> attachments =
attachmentService.getAttachmentsByCourseId(course.getId());
HashMap<String, Object> data = new HashMap<>();
data.put("course", course);
data.put("chapters", chapterService.getChaptersByCourseId(course.getId()));
data.put("hours", courseHours.stream().collect(Collectors.groupingBy(CourseHour::getChapterId)));
data.put(
"hours",
courseHours.stream().collect(Collectors.groupingBy(CourseHour::getChapterId)));
data.put("learn_record", userCourseRecordService.find(FCtx.getId(), course.getId()));
data.put("learn_hour_records",
data.put(
"learn_hour_records",
userCourseHourRecordService.getRecords(FCtx.getId(), course.getId()).stream()
.collect(Collectors.toMap(UserCourseHourRecord::getHourId, e -> e)));
data.put("attachments", attachments);
return JsonResponse.data(data);
}
@GetMapping("/{courseId}/attach/{id}")
@SneakyThrows
public JsonResponse attachmentDownload(
@PathVariable(name = "courseId") Integer courseId,
@PathVariable(name = "id") Integer id) {
CourseAttachment attachment = attachmentService.findOrFail(id, courseId);
Resource resource = resourceService.findOrFail(attachment.getRid());
HashMap<String, Object> data = new HashMap<>();
data.put("download_url", resource.getUrl());
courseAttachmentDownloadLogService.save(
new CourseAttachmentDownloadLog() {
{
setUserId(FCtx.getId());
setCourseId(attachment.getCourseId());
setCourserAttachmentId(attachment.getId());
setRid(resource.getId());
setTitle(attachment.getTitle());
setIp(IpUtil.getIpAddress());
setCreatedAt(new Date());
}
});
return JsonResponse.data(data);
}
}

View File

@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
@ -106,7 +107,11 @@ public class CourseAttachmentDownloadLog implements Serializable {
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getCourseId() == null) ? 0 : getCourseId().hashCode());
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
result = prime * result + ((getCourserAttachmentId() == null) ? 0 : getCourserAttachmentId().hashCode());
result =
prime * result
+ ((getCourserAttachmentId() == null)
? 0
: getCourserAttachmentId().hashCode());
result = prime * result + ((getRid() == null) ? 0 : getRid().hashCode());
result = prime * result + ((getIp() == null) ? 0 : getIp().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());

View File

@ -16,7 +16,9 @@
package xyz.playedu.api.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.CourseAttachmentDownloadLog;
import xyz.playedu.api.types.paginate.CourseAttachmentDownloadLogPaginateFiler;

View File

@ -16,6 +16,7 @@
package xyz.playedu.api.request.backend;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.util.List;

View File

@ -17,6 +17,7 @@ package xyz.playedu.api.request.backend;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data

View File

@ -16,14 +16,15 @@
package xyz.playedu.api.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.stereotype.Service;
import xyz.playedu.api.domain.CourseAttachmentDownloadLog;
import xyz.playedu.api.types.paginate.CourseAttachmentDownloadLogPaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
@Service
public interface CourseAttachmentDownloadLogService extends IService<CourseAttachmentDownloadLog> {
PaginationResult<CourseAttachmentDownloadLog> paginate(int page, int size, CourseAttachmentDownloadLogPaginateFiler filter);
PaginationResult<CourseAttachmentDownloadLog> paginate(
int page, int size, CourseAttachmentDownloadLogPaginateFiler filter);
}

View File

@ -16,7 +16,9 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.playedu.api.domain.CourseAttachmentDownloadLog;
import xyz.playedu.api.mapper.CourseAttachmentDownloadLogMapper;
import xyz.playedu.api.service.CourseAttachmentDownloadLogService;
@ -24,10 +26,12 @@ import xyz.playedu.api.types.paginate.CourseAttachmentDownloadLogPaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
@Service
public class CourseAttachmentDownloadLogServiceImpl extends ServiceImpl<CourseAttachmentDownloadLogMapper, CourseAttachmentDownloadLog>
public class CourseAttachmentDownloadLogServiceImpl
extends ServiceImpl<CourseAttachmentDownloadLogMapper, CourseAttachmentDownloadLog>
implements CourseAttachmentDownloadLogService {
@Override
public PaginationResult<CourseAttachmentDownloadLog> paginate(int page, int size, CourseAttachmentDownloadLogPaginateFiler filter) {
public PaginationResult<CourseAttachmentDownloadLog> paginate(
int page, int size, CourseAttachmentDownloadLogPaginateFiler filter) {
filter.setPageStart((page - 1) * size);
filter.setPageSize(size);