From e28e2e30af28aeddff8187c54e577f78f31c339b Mon Sep 17 00:00:00 2001 From: none Date: Sat, 29 Jul 2023 15:01:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E9=99=84=E4=BB=B6=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/CourseAttachmentController.java | 8 +-- ...CourseAttachmentDownloadLogController.java | 8 ++- .../controller/backend/CourseController.java | 20 ++++--- ...CourseAttachmentDownloadLogController.java | 60 ------------------- .../controller/frontend/CourseController.java | 49 +++++++++++---- .../domain/CourseAttachmentDownloadLog.java | 7 ++- .../CourseAttachmentDownloadLogMapper.java | 2 + .../backend/CourseAttachmentMultiRequest.java | 1 + .../backend/CourseAttachmentRequest.java | 1 + .../CourseAttachmentDownloadLogService.java | 7 ++- ...ourseAttachmentDownloadLogServiceImpl.java | 8 ++- 11 files changed, 79 insertions(+), 92 deletions(-) delete mode 100644 src/main/java/xyz/playedu/api/controller/frontend/CourseAttachmentDownloadLogController.java diff --git a/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentController.java b/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentController.java index 59d4d3a..f5ddb90 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentController.java @@ -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(); } diff --git a/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentDownloadLogController.java b/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentDownloadLogController.java index f4abfef..69a05fd 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentDownloadLogController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/CourseAttachmentDownloadLogController.java @@ -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 result = courseAttachmentDownloadLogService.paginate(page, size, filter); + PaginationResult result = + courseAttachmentDownloadLogService.paginate(page, size, filter); HashMap data = new HashMap<>(); data.put("data", result.getData()); diff --git a/src/main/java/xyz/playedu/api/controller/backend/CourseController.java b/src/main/java/xyz/playedu/api/controller/backend/CourseController.java index f24d116..0c3b765 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/CourseController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/CourseController.java @@ -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 categoryIds = courseService.getCategoryIdsByCourseId(course.getId()); List chapters = chapterService.getChaptersByCourseId(course.getId()); List hours = hourService.getHoursByCourseId(course.getId()); - List attachments = attachmentService.getAttachmentsByCourseId(course.getId()); - if(null != attachments && attachments.size() > 0){ - Map 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 attachments = + attachmentService.getAttachmentsByCourseId(course.getId()); + if (null != attachments && attachments.size() > 0) { + Map 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 data = new HashMap<>(); diff --git a/src/main/java/xyz/playedu/api/controller/frontend/CourseAttachmentDownloadLogController.java b/src/main/java/xyz/playedu/api/controller/frontend/CourseAttachmentDownloadLogController.java deleted file mode 100644 index 6ffabb9..0000000 --- a/src/main/java/xyz/playedu/api/controller/frontend/CourseAttachmentDownloadLogController.java +++ /dev/null @@ -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(); - } -} diff --git a/src/main/java/xyz/playedu/api/controller/frontend/CourseController.java b/src/main/java/xyz/playedu/api/controller/frontend/CourseController.java index 83232f4..30510ca 100644 --- a/src/main/java/xyz/playedu/api/controller/frontend/CourseController.java +++ b/src/main/java/xyz/playedu/api/controller/frontend/CourseController.java @@ -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 params) { Integer page = MapUtils.getInteger(params, "page", 1); @@ -78,24 +81,48 @@ public class CourseController { List courseHours = hourService.getHoursByCourseId(course.getId()); - List attachments = attachmentService.getAttachmentsByCourseId(course.getId()); - if(null != attachments && attachments.size() > 0){ - Map 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 attachments = + attachmentService.getAttachmentsByCourseId(course.getId()); HashMap 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 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); + } } diff --git a/src/main/java/xyz/playedu/api/domain/CourseAttachmentDownloadLog.java b/src/main/java/xyz/playedu/api/domain/CourseAttachmentDownloadLog.java index 7e29407..c62a1ec 100644 --- a/src/main/java/xyz/playedu/api/domain/CourseAttachmentDownloadLog.java +++ b/src/main/java/xyz/playedu/api/domain/CourseAttachmentDownloadLog.java @@ -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()); diff --git a/src/main/java/xyz/playedu/api/mapper/CourseAttachmentDownloadLogMapper.java b/src/main/java/xyz/playedu/api/mapper/CourseAttachmentDownloadLogMapper.java index 2df8901..882c47d 100644 --- a/src/main/java/xyz/playedu/api/mapper/CourseAttachmentDownloadLogMapper.java +++ b/src/main/java/xyz/playedu/api/mapper/CourseAttachmentDownloadLogMapper.java @@ -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; diff --git a/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentMultiRequest.java b/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentMultiRequest.java index 2f4660a..ce27437 100644 --- a/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentMultiRequest.java +++ b/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentMultiRequest.java @@ -16,6 +16,7 @@ package xyz.playedu.api.request.backend; import jakarta.validation.constraints.NotNull; + import lombok.Data; import java.util.List; diff --git a/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentRequest.java b/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentRequest.java index c6cd80c..c2afeb5 100644 --- a/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentRequest.java +++ b/src/main/java/xyz/playedu/api/request/backend/CourseAttachmentRequest.java @@ -17,6 +17,7 @@ package xyz.playedu.api.request.backend; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; + import lombok.Data; @Data diff --git a/src/main/java/xyz/playedu/api/service/CourseAttachmentDownloadLogService.java b/src/main/java/xyz/playedu/api/service/CourseAttachmentDownloadLogService.java index 0a8e67e..89c4b60 100644 --- a/src/main/java/xyz/playedu/api/service/CourseAttachmentDownloadLogService.java +++ b/src/main/java/xyz/playedu/api/service/CourseAttachmentDownloadLogService.java @@ -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 { - PaginationResult paginate(int page, int size, CourseAttachmentDownloadLogPaginateFiler filter); - - + PaginationResult paginate( + int page, int size, CourseAttachmentDownloadLogPaginateFiler filter); } diff --git a/src/main/java/xyz/playedu/api/service/impl/CourseAttachmentDownloadLogServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/CourseAttachmentDownloadLogServiceImpl.java index c9163d4..ff25d69 100644 --- a/src/main/java/xyz/playedu/api/service/impl/CourseAttachmentDownloadLogServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/CourseAttachmentDownloadLogServiceImpl.java @@ -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 +public class CourseAttachmentDownloadLogServiceImpl + extends ServiceImpl implements CourseAttachmentDownloadLogService { @Override - public PaginationResult paginate(int page, int size, CourseAttachmentDownloadLogPaginateFiler filter) { + public PaginationResult paginate( + int page, int size, CourseAttachmentDownloadLogPaginateFiler filter) { filter.setPageStart((page - 1) * size); filter.setPageSize(size);