mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-10 20:04:06 +08:00
管理员的增删改查
This commit is contained in:
parent
dbbfdb00ce
commit
01d794905d
@ -2,14 +2,16 @@ package xyz.playedu.api.controller.backend;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.request.backend.AdminUserRequest;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
@ -20,9 +22,93 @@ public class AdminUserController {
|
||||
private AdminUserService adminUserService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse List(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
|
||||
public JsonResponse Index(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
|
||||
PaginationResult<AdminUser> result = adminUserService.paginate(page, size, null);
|
||||
return JsonResponse.data(result);
|
||||
}
|
||||
|
||||
@GetMapping("/create")
|
||||
public JsonResponse create() {
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public JsonResponse store(@RequestBody @Validated AdminUserRequest request) {
|
||||
if (request.getPassword() == null || request.getPassword().length() == 0) {
|
||||
return JsonResponse.error("请输入密码");
|
||||
}
|
||||
|
||||
if (adminUserService.findByEmail(request.getEmail()) != null) {
|
||||
return JsonResponse.error("邮箱已存在");
|
||||
}
|
||||
|
||||
String salt = HelperUtil.randomString(6);
|
||||
|
||||
AdminUser adminUser = new AdminUser();
|
||||
adminUser.setName(request.getName());
|
||||
adminUser.setEmail(request.getEmail());
|
||||
adminUser.setSalt(salt);
|
||||
adminUser.setPassword(HelperUtil.MD5(request.getPassword() + salt));
|
||||
adminUser.setIsBanLogin(request.getIsBanLogin());
|
||||
adminUser.setCreatedAt(new Date());
|
||||
adminUser.setUpdatedAt(new Date());
|
||||
|
||||
if (!adminUserService.save(adminUser)) {
|
||||
return JsonResponse.error("添加管理员失败");
|
||||
}
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public JsonResponse edit(@PathVariable Integer id) {
|
||||
AdminUser adminUser = adminUserService.findById(id);
|
||||
if (adminUser == null) {
|
||||
return JsonResponse.error("管理员不存在");
|
||||
}
|
||||
adminUser.setPassword(null);
|
||||
adminUser.setSalt(null);
|
||||
return JsonResponse.data(adminUser);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public JsonResponse update(@PathVariable Integer id, @RequestBody @Validated AdminUserRequest request) {
|
||||
AdminUser adminUser = adminUserService.findById(id);
|
||||
if (adminUser == null) {
|
||||
return JsonResponse.error("管理员不存在");
|
||||
}
|
||||
|
||||
AdminUser updateAdminUser = new AdminUser();
|
||||
updateAdminUser.setId(adminUser.getId());
|
||||
|
||||
if (!adminUser.getEmail().equals(request.getEmail())) {//更换了邮箱
|
||||
if (adminUserService.findByEmail(request.getEmail()) != null) {
|
||||
return JsonResponse.error("邮箱已存在");
|
||||
}
|
||||
updateAdminUser.setEmail(request.getEmail());
|
||||
}
|
||||
|
||||
if (request.getPassword() != null && request.getPassword().length() > 0) {//更换了密码
|
||||
updateAdminUser.setPassword(HelperUtil.MD5(request.getPassword() + adminUser.getSalt()));
|
||||
}
|
||||
|
||||
if (!request.getName().equals(adminUser.getName())) {//更换了姓名
|
||||
updateAdminUser.setName(request.getName());
|
||||
}
|
||||
|
||||
if (!adminUserService.updateById(updateAdminUser)) {
|
||||
return JsonResponse.error("更新管理员资料失败");
|
||||
}
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public JsonResponse destroy(@PathVariable Integer id) {
|
||||
if (!adminUserService.removeById(id)) {
|
||||
return JsonResponse.error("删除管理员失败");
|
||||
}
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,10 +13,9 @@ import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.event.AdminUserLoginEvent;
|
||||
import xyz.playedu.api.exception.JwtLogoutException;
|
||||
import xyz.playedu.api.middleware.ImageCaptchaCheckMiddleware;
|
||||
import xyz.playedu.api.request.LoginRequest;
|
||||
import xyz.playedu.api.request.backend.LoginRequest;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.service.JWTService;
|
||||
import xyz.playedu.api.types.JWTPayload;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.types.JwtToken;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
@ -8,7 +8,7 @@ import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.playedu.api.service.ImageCaptchaService;
|
||||
import xyz.playedu.api.request.types.ImageCaptchaRequestInterface;
|
||||
import xyz.playedu.api.request.backend.types.ImageCaptchaRequestInterface;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
@Aspect
|
||||
|
@ -0,0 +1,33 @@
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/19 09:43
|
||||
*/
|
||||
@Data
|
||||
public class AdminUserRequest implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull(message = "请输入管理员姓名")
|
||||
@Length(min = 1, max = 12, message = "管理员姓名长度在1-12个字符之间")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "请输入管理员邮箱")
|
||||
@Email(message = "请输入合法邮箱")
|
||||
private String email;
|
||||
|
||||
private String password;
|
||||
|
||||
private Integer isBanLogin;
|
||||
|
||||
}
|
@ -1,14 +1,16 @@
|
||||
package xyz.playedu.api.request;
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import xyz.playedu.api.request.types.ImageCaptchaRequestInterface;
|
||||
import xyz.playedu.api.request.backend.types.ImageCaptchaRequestInterface;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class LoginRequest implements Serializable, ImageCaptchaRequestInterface {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull(message = "请输入邮箱")
|
@ -1,12 +1,14 @@
|
||||
package xyz.playedu.api.request;
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class PaginationRequest implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Min(value = 1, message = "page参数值不能少于1")
|
@ -1,4 +1,4 @@
|
||||
package xyz.playedu.api.request.types;
|
||||
package xyz.playedu.api.request.backend.types;
|
||||
|
||||
public interface ImageCaptchaRequestInterface {
|
||||
|
@ -19,6 +19,10 @@ public class JsonResponse {
|
||||
return new JsonResponse(0, msg, null);
|
||||
}
|
||||
|
||||
public static JsonResponse success() {
|
||||
return new JsonResponse(0, "", null);
|
||||
}
|
||||
|
||||
public static JsonResponse data(Object data) {
|
||||
return new JsonResponse(0, "", data);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user