mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-29 17:02:42 +08:00
资源分类优化
This commit is contained in:
parent
5672c70ccb
commit
97bed52b2f
@ -48,7 +48,7 @@ public class LoginController {
|
||||
if (!adminUser.getPassword().equals(password)) {
|
||||
return JsonResponse.error("邮箱或密码错误");
|
||||
}
|
||||
if (adminUser.getIsBanLogin() == 1) {
|
||||
if (adminUser.getIsBanLogin().equals(1)) {
|
||||
return JsonResponse.error("当前用户已禁止登录");
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,15 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import xyz.playedu.api.event.ResourceCategoryDestroyEvent;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
||||
import xyz.playedu.api.request.backend.ResourceCategoryRequest;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
@ -27,11 +31,16 @@ public class ResourceCategoryController {
|
||||
@Autowired
|
||||
private ResourceCategoryService resourceCategoryService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext ctx;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(@RequestParam(name = "type") String type) {
|
||||
List<ResourceCategory> categories = resourceCategoryService.getByType(type);
|
||||
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("data", categories);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@ -45,53 +54,29 @@ public class ResourceCategoryController {
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
|
||||
@PostMapping("/create")
|
||||
public JsonResponse store(@RequestBody @Validated ResourceCategoryRequest request) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(request.getType())) {
|
||||
public JsonResponse store(@RequestBody @Validated ResourceCategoryRequest req) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(req.getType())) {
|
||||
return JsonResponse.error("资源类型不支持");
|
||||
}
|
||||
|
||||
ResourceCategory category = new ResourceCategory();
|
||||
|
||||
category.setType(request.getType());
|
||||
category.setSort(request.getSort());
|
||||
category.setName(request.getName());
|
||||
category.setCreatedAt(new Date());
|
||||
category.setUpdatedAt(new Date());
|
||||
|
||||
resourceCategoryService.save(category);
|
||||
|
||||
resourceCategoryService.create(req.getType(), req.getSort(), req.getName());
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
|
||||
@GetMapping("/{id}")
|
||||
public JsonResponse edit(@PathVariable(name = "id") Integer id) {
|
||||
ResourceCategory category = resourceCategoryService.getById(id);
|
||||
if (category == null) {
|
||||
return JsonResponse.error("分类不存在");
|
||||
}
|
||||
public JsonResponse edit(@PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
ResourceCategory category = resourceCategoryService.findOrFail(id);
|
||||
return JsonResponse.data(category);
|
||||
}
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
|
||||
@PutMapping("/{id}")
|
||||
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated ResourceCategoryRequest request) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(request.getType())) {
|
||||
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated ResourceCategoryRequest req) throws NotFoundException {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(req.getType())) {
|
||||
return JsonResponse.error("资源类型不支持");
|
||||
}
|
||||
|
||||
ResourceCategory category = resourceCategoryService.getById(id);
|
||||
if (category == null) {
|
||||
return JsonResponse.error("分类不存在");
|
||||
}
|
||||
|
||||
ResourceCategory newCategory = new ResourceCategory();
|
||||
newCategory.setId(category.getId());
|
||||
newCategory.setName(request.getName());
|
||||
newCategory.setSort(request.getSort());
|
||||
|
||||
resourceCategoryService.updateById(newCategory);
|
||||
|
||||
ResourceCategory category = resourceCategoryService.findOrFail(id);
|
||||
resourceCategoryService.update(category, req.getSort(), req.getName());
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@ -99,6 +84,7 @@ public class ResourceCategoryController {
|
||||
@DeleteMapping("/{id}")
|
||||
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
|
||||
resourceCategoryService.removeById(id);
|
||||
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), id, new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package xyz.playedu.api.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/26 17:10
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class ResourceCategoryDestroyEvent extends ApplicationEvent {
|
||||
|
||||
private Integer adminId;
|
||||
private Integer categoryId;
|
||||
private Date date;
|
||||
|
||||
public ResourceCategoryDestroyEvent(Object source, Integer adminId, Integer categoryId, Date date) {
|
||||
super(source);
|
||||
this.adminId = adminId;
|
||||
this.categoryId = categoryId;
|
||||
this.date = date;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,4 +15,10 @@ public interface ResourceCategoryService extends IService<ResourceCategory> {
|
||||
|
||||
List<ResourceCategory> getByType(String type);
|
||||
|
||||
void create(String type, Integer sort, String name);
|
||||
|
||||
ResourceCategory findOrFail(Integer id) throws NotFoundException;
|
||||
|
||||
void update(ResourceCategory category, Integer sort, String name);
|
||||
|
||||
}
|
||||
|
@ -2,10 +2,12 @@ package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.mapper.ResourceCategoryMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -20,6 +22,37 @@ public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMap
|
||||
public List<ResourceCategory> getByType(String type) {
|
||||
return list(query().getWrapper().eq("type", type).orderByAsc("id"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(String type, Integer sort, String name) {
|
||||
ResourceCategory category = new ResourceCategory();
|
||||
|
||||
category.setType(type);
|
||||
category.setSort(sort);
|
||||
category.setName(name);
|
||||
category.setCreatedAt(new Date());
|
||||
category.setUpdatedAt(new Date());
|
||||
|
||||
save(category);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceCategory findOrFail(Integer id) throws NotFoundException {
|
||||
ResourceCategory category = getOne(query().getWrapper().eq("id", id));
|
||||
if (category == null) {
|
||||
throw new NotFoundException("分类不存在");
|
||||
}
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(ResourceCategory category, Integer sort, String name) {
|
||||
ResourceCategory newCategory = new ResourceCategory();
|
||||
newCategory.setId(category.getId());
|
||||
newCategory.setSort(sort);
|
||||
newCategory.setName(name);
|
||||
updateById(newCategory);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user