mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-23 17:49:34 +08:00
资源管理
This commit is contained in:
parent
e1796e423b
commit
b3e2c2c37e
@ -5,4 +5,6 @@ public class BackendConstant {
|
||||
|
||||
public final static String[] RESOURCE_TYPE_WHITELIST = {"IMAGE", "PDF", "VIDEO", "WORD", "PPT"};
|
||||
|
||||
public final static String[] RESOURCE_DISK_WHITELIST = {"MINIO"};
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import xyz.playedu.api.request.backend.AdminUserRequest;
|
||||
import xyz.playedu.api.service.AdminRoleService;
|
||||
import xyz.playedu.api.service.AdminUserRoleService;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
|
@ -0,0 +1,88 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import xyz.playedu.api.request.backend.ResourceRequest;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/23 10:50
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/backend/v1/resource")
|
||||
public class ResourceController {
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
@Autowired
|
||||
private ResourceCategoryService categoryService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size, @RequestParam(name = "name", defaultValue = "") String name) {
|
||||
ResourcePaginateFilter filter = new ResourcePaginateFilter();
|
||||
if (name != null && name.length() > 0) {
|
||||
filter.setName(name);
|
||||
}
|
||||
|
||||
PaginationResult<Resource> result = resourceService.paginate(page, size, filter);
|
||||
|
||||
return JsonResponse.data(result);
|
||||
}
|
||||
|
||||
@GetMapping("/create")
|
||||
public JsonResponse create(@RequestParam(name = "type") String type) {
|
||||
List<ResourceCategory> categories = categoryService.getByType(type);
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("categories", categories);
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public JsonResponse store(@RequestBody @Validated ResourceRequest request) {
|
||||
if (categoryService.getById(request.getCategoryId()) == null) {
|
||||
return JsonResponse.error("资源分类不存在");
|
||||
}
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_DISK_WHITELIST).contains(request.getDisk())) {
|
||||
return JsonResponse.error("存储磁盘参数错误");
|
||||
}
|
||||
|
||||
Resource resource = new Resource();
|
||||
|
||||
resource.setCategoryId(request.getCategoryId());
|
||||
resource.setName(request.getName());
|
||||
resource.setExtension(request.getExtension());
|
||||
resource.setSize(request.getSize());
|
||||
resource.setDisk(request.getDisk());
|
||||
resource.setFileId(request.getFileId());
|
||||
resource.setPath(request.getPath());
|
||||
resource.setUrl(request.getUrl());
|
||||
resource.setCreatedAt(new Date());
|
||||
|
||||
resourceService.save(resource);
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
|
||||
resourceService.removeById(id);
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
}
|
133
src/main/java/xyz/playedu/api/domain/Resource.java
Normal file
133
src/main/java/xyz/playedu/api/domain/Resource.java
Normal file
@ -0,0 +1,133 @@
|
||||
package xyz.playedu.api.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
*
|
||||
* @TableName resources
|
||||
*/
|
||||
@TableName(value ="resources")
|
||||
@Data
|
||||
public class Resource implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 分类id
|
||||
*/
|
||||
private Integer categoryId;
|
||||
|
||||
/**
|
||||
* 资源名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
*/
|
||||
private String extension;
|
||||
|
||||
/**
|
||||
* 大小[字节]
|
||||
*/
|
||||
private Long size;
|
||||
|
||||
/**
|
||||
* 存储磁盘
|
||||
*/
|
||||
private String disk;
|
||||
|
||||
/**
|
||||
* 文件id
|
||||
*/
|
||||
private String fileId;
|
||||
|
||||
/**
|
||||
* 相对地址
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* URL地址
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Date createdAt;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Resource other = (Resource) that;
|
||||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
|
||||
&& (this.getCategoryId() == null ? other.getCategoryId() == null : this.getCategoryId().equals(other.getCategoryId()))
|
||||
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
|
||||
&& (this.getExtension() == null ? other.getExtension() == null : this.getExtension().equals(other.getExtension()))
|
||||
&& (this.getSize() == null ? other.getSize() == null : this.getSize().equals(other.getSize()))
|
||||
&& (this.getDisk() == null ? other.getDisk() == null : this.getDisk().equals(other.getDisk()))
|
||||
&& (this.getFileId() == null ? other.getFileId() == null : this.getFileId().equals(other.getFileId()))
|
||||
&& (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath()))
|
||||
&& (this.getUrl() == null ? other.getUrl() == null : this.getUrl().equals(other.getUrl()))
|
||||
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
|
||||
result = prime * result + ((getCategoryId() == null) ? 0 : getCategoryId().hashCode());
|
||||
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
|
||||
result = prime * result + ((getExtension() == null) ? 0 : getExtension().hashCode());
|
||||
result = prime * result + ((getSize() == null) ? 0 : getSize().hashCode());
|
||||
result = prime * result + ((getDisk() == null) ? 0 : getDisk().hashCode());
|
||||
result = prime * result + ((getFileId() == null) ? 0 : getFileId().hashCode());
|
||||
result = prime * result + ((getPath() == null) ? 0 : getPath().hashCode());
|
||||
result = prime * result + ((getUrl() == null) ? 0 : getUrl().hashCode());
|
||||
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", id=").append(id);
|
||||
sb.append(", categoryId=").append(categoryId);
|
||||
sb.append(", name=").append(name);
|
||||
sb.append(", extension=").append(extension);
|
||||
sb.append(", size=").append(size);
|
||||
sb.append(", disk=").append(disk);
|
||||
sb.append(", fileId=").append(fileId);
|
||||
sb.append(", path=").append(path);
|
||||
sb.append(", url=").append(url);
|
||||
sb.append(", createdAt=").append(createdAt);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
20
src/main/java/xyz/playedu/api/mapper/ResourceMapper.java
Normal file
20
src/main/java/xyz/playedu/api/mapper/ResourceMapper.java
Normal file
@ -0,0 +1,20 @@
|
||||
package xyz.playedu.api.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【resources】的数据库操作Mapper
|
||||
* @createDate 2023-02-23 10:50:26
|
||||
* @Entity xyz.playedu.api.domain.Resource
|
||||
*/
|
||||
@Mapper
|
||||
public interface ResourceMapper extends BaseMapper<Resource> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,43 @@
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/23 10:52
|
||||
*/
|
||||
@Data
|
||||
public class ResourceRequest {
|
||||
|
||||
@NotNull(message = "请选择资源分类")
|
||||
@JsonProperty("category_id")
|
||||
private Integer categoryId;
|
||||
|
||||
@NotNull(message = "资源名不能为空")
|
||||
@Length(min = 1, max = 254, message = "资源名长度在1-254个字符之间")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "请输入资源扩展")
|
||||
@Length(min = 1, max = 254, message = "资源扩展长度在1-20个字符之间")
|
||||
private String extension;
|
||||
|
||||
@NotNull(message = "请输入文件大小")
|
||||
private Long size;
|
||||
|
||||
@NotNull(message = "请输入文件存储磁盘")
|
||||
private String disk;
|
||||
|
||||
@NotNull(message = "请输入fileId")
|
||||
@JsonProperty("file_id")
|
||||
private String fileId;
|
||||
|
||||
@NotNull(message = "请输入存储路径")
|
||||
private String path;
|
||||
|
||||
@NotNull(message = "请输入访问URL")
|
||||
private String url;
|
||||
|
||||
}
|
@ -3,9 +3,7 @@ package xyz.playedu.api.service;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
|
||||
import java.util.Date;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
|
17
src/main/java/xyz/playedu/api/service/ResourceService.java
Normal file
17
src/main/java/xyz/playedu/api/service/ResourceService.java
Normal file
@ -0,0 +1,17 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【resources】的数据库操作Service
|
||||
* @createDate 2023-02-23 10:50:26
|
||||
*/
|
||||
public interface ResourceService extends IService<Resource> {
|
||||
|
||||
PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter);
|
||||
|
||||
}
|
@ -8,7 +8,7 @@ import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.mapper.AdminUserMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
|
||||
@Service
|
||||
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
|
||||
|
@ -0,0 +1,50 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.mapper.ResourceMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xyz.playedu.api.types.paginate.PaginationResult;
|
||||
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【resources】的数据库操作Service实现
|
||||
* @createDate 2023-02-23 10:50:26
|
||||
*/
|
||||
@Service
|
||||
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
|
||||
|
||||
@Override
|
||||
public PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter) {
|
||||
QueryWrapper<Resource> wrapper = query().getWrapper().eq("1", "1");
|
||||
if (filter != null) {
|
||||
if (filter.getName() != null) {
|
||||
wrapper.like("name", "%" + filter.getName() + "%");
|
||||
}
|
||||
if (filter.getDisk() != null) {
|
||||
wrapper.eq("disk", filter.getDisk());
|
||||
}
|
||||
if (filter.getExtension() != null) {
|
||||
wrapper.eq("extension", filter.getExtension());
|
||||
}
|
||||
}
|
||||
|
||||
IPage<Resource> adminPage = new Page<>(page, size);
|
||||
adminPage = page(adminPage, wrapper);
|
||||
|
||||
PaginationResult<Resource> pageResult = new PaginationResult<>();
|
||||
pageResult.setData(adminPage.getRecords());
|
||||
pageResult.setTotal(adminPage.getTotal());
|
||||
|
||||
return pageResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package xyz.playedu.api.types;
|
||||
package xyz.playedu.api.types.paginate;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -0,0 +1,18 @@
|
||||
package xyz.playedu.api.types.paginate;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/23 11:18
|
||||
*/
|
||||
@Data
|
||||
public class ResourcePaginateFilter {
|
||||
|
||||
private String name;
|
||||
|
||||
private String extension;
|
||||
|
||||
private String disk;
|
||||
|
||||
}
|
26
src/main/resources/mapper/ResourceMapper.xml
Normal file
26
src/main/resources/mapper/ResourceMapper.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?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="xyz.playedu.api.mapper.ResourceMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.Resource">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="categoryId" column="category_id" jdbcType="INTEGER"/>
|
||||
<result property="name" column="name" jdbcType="VARCHAR"/>
|
||||
<result property="extension" column="extension" jdbcType="VARCHAR"/>
|
||||
<result property="size" column="size" jdbcType="BIGINT"/>
|
||||
<result property="disk" column="disk" jdbcType="VARCHAR"/>
|
||||
<result property="fileId" column="file_id" jdbcType="VARCHAR"/>
|
||||
<result property="path" column="path" jdbcType="VARCHAR"/>
|
||||
<result property="url" column="url" jdbcType="VARCHAR"/>
|
||||
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,category_id,name,
|
||||
extension,size,disk,
|
||||
file_id,path,url,
|
||||
created_at
|
||||
</sql>
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user