feat: manage teachers from platform admin

main
chenyuan 2 months ago
parent c9484ae88d
commit c2b03be5ef

@ -106,20 +106,20 @@ public class PlatformAdminController {
return writeResult(count, "删除成功", "删除失败"); return writeResult(count, "删除成功", "删除失败");
} }
@GetMapping("/school-admins") @GetMapping({"/teachers", "/school-admins"})
@ApiOperation("平台超管-学校管理员列表") @ApiOperation("平台超管-教师列表")
public ResultEntity<PageInfo<Userinfo>> listSchoolAdmins(@RequestParam Integer index, public ResultEntity<PageInfo<Userinfo>> listTeachers(@RequestParam Integer index,
@RequestParam Integer size, @RequestParam Integer size,
@RequestParam(required = false) String schoolId, @RequestParam(required = false) String schoolId,
@RequestParam(required = false) String name, @RequestParam(required = false) String name,
@RequestParam(required = false) String username, @RequestParam(required = false) String username,
@RequestParam(required = false) String phone, @RequestParam(required = false) String phone,
HttpServletRequest request) { HttpServletRequest request) {
requirePlatformAdmin(request); requirePlatformAdmin(request);
PageHelper.startPage(index, size); PageHelper.startPage(index, size);
UserinfoExample example = new UserinfoExample(); UserinfoExample example = new UserinfoExample();
UserinfoExample.Criteria criteria = example.createCriteria(); UserinfoExample.Criteria criteria = example.createCriteria();
criteria.andRoleEqualTo(2); criteria.andRoleEqualTo(3);
if (StringUtils.isNotBlank(schoolId)) { if (StringUtils.isNotBlank(schoolId)) {
criteria.andSchoolIdEqualTo(schoolId); criteria.andSchoolIdEqualTo(schoolId);
} }
@ -136,55 +136,56 @@ public class PlatformAdminController {
return new ResultEntity<>(HttpStatus.OK, "查询成功", new PageInfo<>(userinfoMapper.selectByExample(example))); return new ResultEntity<>(HttpStatus.OK, "查询成功", new PageInfo<>(userinfoMapper.selectByExample(example)));
} }
@GetMapping("/school-admins/{id}") @GetMapping({"/teachers/{id}", "/school-admins/{id}"})
@ApiOperation("平台超管-学校管理员详情") @ApiOperation("平台超管-教师详情")
public ResultEntity<Userinfo> getSchoolAdmin(@PathVariable String id, HttpServletRequest request) { public ResultEntity<Userinfo> getTeacher(@PathVariable String id, HttpServletRequest request) {
requirePlatformAdmin(request); requirePlatformAdmin(request);
return new ResultEntity<>(HttpStatus.OK, "查询成功", requireSchoolAdminUser(id)); return new ResultEntity<>(HttpStatus.OK, "查询成功", requireTeacherUser(id));
} }
@PostMapping("/school-admins") @PostMapping({"/teachers", "/school-admins"})
@ApiOperation("平台超管-新增学校管理员") @ApiOperation("平台超管-新增教师")
public ResultEntity addSchoolAdmin(@RequestBody Userinfo schoolAdmin, HttpServletRequest request) { public ResultEntity addTeacher(@RequestBody Userinfo teacher, HttpServletRequest request) {
requirePlatformAdmin(request); requirePlatformAdmin(request);
if (StringUtils.isBlank(schoolAdmin.getUsername()) || StringUtils.isBlank(schoolAdmin.getName())) { if (StringUtils.isBlank(teacher.getUsername()) || StringUtils.isBlank(teacher.getName())) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "学校管理员姓名和账号不能为空"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "教师姓名和账号不能为空");
} }
String schoolId = platformAdminService.requireSchoolId(schoolAdmin.getSchoolId()); String schoolId = platformAdminService.requireSchoolId(teacher.getSchoolId());
requireSchool(schoolId); requireSchool(schoolId);
if (userInfoService.existsByUserName(schoolAdmin.getUsername())) { if (userInfoService.existsByUserName(teacher.getUsername())) {
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "账号已存在"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "账号已存在");
} }
schoolAdmin.setUserId(UUID.randomUUID().toString()); teacher.setUserId(UUID.randomUUID().toString());
if (StringUtils.isBlank(schoolAdmin.getPassword())) { if (StringUtils.isBlank(teacher.getPassword())) {
schoolAdmin.setPassword("123qwe"); teacher.setPassword("123qwe");
} }
schoolAdmin.setCreateTime(new Date()); teacher.setCreateTime(new Date());
platformAdminService.prepareSchoolAdminForSave(schoolAdmin, schoolId); platformAdminService.prepareTeacherForSave(teacher, schoolId);
int count = userinfoMapper.insertSelective(schoolAdmin); int count = userinfoMapper.insertSelective(teacher);
return writeResult(count, "新增成功", "新增失败"); return writeResult(count, "新增成功", "新增失败");
} }
@PutMapping("/school-admins/{id}") @PutMapping({"/teachers/{id}", "/school-admins/{id}"})
@ApiOperation("平台超管-编辑学校管理员") @ApiOperation("平台超管-编辑教师")
public ResultEntity updateSchoolAdmin(@PathVariable String id, public ResultEntity updateTeacher(@PathVariable String id,
@RequestBody Userinfo schoolAdmin, @RequestBody Userinfo teacher,
HttpServletRequest request) { HttpServletRequest request) {
requirePlatformAdmin(request); requirePlatformAdmin(request);
requireSchoolAdminUser(id); requireTeacherUser(id);
String schoolId = platformAdminService.requireSchoolId(schoolAdmin.getSchoolId()); String schoolId = platformAdminService.requireSchoolId(teacher.getSchoolId());
requireSchool(schoolId); requireSchool(schoolId);
schoolAdmin.setUserId(id); teacher.setUserId(id);
platformAdminService.prepareSchoolAdminForSave(schoolAdmin, schoolId); platformAdminService.prepareTeacherForSave(teacher, schoolId);
int count = userinfoMapper.updateByPrimaryKeySelective(schoolAdmin); int count = userinfoMapper.updateByPrimaryKeySelective(teacher);
return writeResult(count, "编辑成功", "编辑失败"); return writeResult(count, "编辑成功", "编辑失败");
} }
@DeleteMapping("/school-admins/{id}") @DeleteMapping({"/teachers/{id}", "/school-admins/{id}"})
@ApiOperation("平台超管-删除学校管理员") @ApiOperation("平台超管-删除教师")
public ResultEntity deleteSchoolAdmin(@PathVariable String id, HttpServletRequest request) { public ResultEntity deleteTeacher(@PathVariable String id, HttpServletRequest request) {
requirePlatformAdmin(request); requirePlatformAdmin(request);
requireSchoolAdminUser(id); requireTeacherUser(id);
platformAdminService.assertTeacherCanBeDeleted(countClassesCreatedBy(id));
int count = userinfoMapper.deleteByPrimaryKey(id); int count = userinfoMapper.deleteByPrimaryKey(id);
return writeResult(count, "删除成功", "删除失败"); return writeResult(count, "删除成功", "删除失败");
} }
@ -205,17 +206,23 @@ public class PlatformAdminController {
return school; return school;
} }
private Userinfo requireSchoolAdminUser(String userId) { private Userinfo requireTeacherUser(String userId) {
if (StringUtils.isBlank(userId)) { if (StringUtils.isBlank(userId)) {
throw new IllegalArgumentException("用户ID不能为空"); throw new IllegalArgumentException("用户ID不能为空");
} }
Userinfo userinfo = userinfoMapper.selectByPrimaryKey(userId); Userinfo userinfo = userinfoMapper.selectByPrimaryKey(userId);
if (userinfo == null || !Integer.valueOf(2).equals(userinfo.getRole())) { if (userinfo == null || !Integer.valueOf(3).equals(userinfo.getRole())) {
throw new IllegalArgumentException("学校管理员不存在"); throw new IllegalArgumentException("教师不存在");
} }
return userinfo; return userinfo;
} }
private long countClassesCreatedBy(String teacherId) {
SchoolClassExample example = new SchoolClassExample();
example.createCriteria().andCreatedByEqualTo(teacherId);
return schoolClassMapper.countByExample(example);
}
private ResultEntity writeResult(int count, String success, String fail) { private ResultEntity writeResult(int count, String success, String fail) {
if (count > 0) { if (count > 0) {
return new ResultEntity<>(HttpStatus.OK, success); return new ResultEntity<>(HttpStatus.OK, success);

@ -8,5 +8,9 @@ public interface PlatformAdminService {
void prepareSchoolAdminForSave(Userinfo userinfo, String schoolId); void prepareSchoolAdminForSave(Userinfo userinfo, String schoolId);
void prepareTeacherForSave(Userinfo userinfo, String schoolId);
void assertTeacherCanBeDeleted(long createdClassCount);
String requireSchoolId(String schoolId); String requireSchoolId(String schoolId);
} }

@ -22,6 +22,22 @@ public class PlatformAdminServiceImpl implements PlatformAdminService {
userinfo.setRole(2); userinfo.setRole(2);
} }
@Override
public void prepareTeacherForSave(Userinfo userinfo, String schoolId) {
userinfo.setSchoolId(requireSchoolId(schoolId));
userinfo.setRole(3);
if (userinfo.getTeacherAdmin() == null) {
userinfo.setTeacherAdmin(false);
}
}
@Override
public void assertTeacherCanBeDeleted(long createdClassCount) {
if (createdClassCount > 0) {
throw new IllegalArgumentException("Teacher has created classes and cannot be deleted");
}
}
@Override @Override
public String requireSchoolId(String schoolId) { public String requireSchoolId(String schoolId) {
if (StringUtils.isBlank(schoolId)) { if (StringUtils.isBlank(schoolId)) {

@ -0,0 +1,47 @@
package com.sztzjy.linkCommerce.controller.platformadmin;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PlatformAdminControllerTest {
@Test
void exposesPlatformTeacherManagementEndpoints() {
assertTrue(hasGet("/teachers"), "Platform admin must expose teacher list endpoint");
assertTrue(hasGet("/teachers/{id}"), "Platform admin must expose teacher detail endpoint");
assertTrue(hasPost("/teachers"), "Platform admin must expose teacher create endpoint");
assertTrue(hasPut("/teachers/{id}"), "Platform admin must expose teacher update endpoint");
assertTrue(hasDelete("/teachers/{id}"), "Platform admin must expose teacher delete endpoint");
}
private boolean hasGet(String path) {
return Arrays.stream(PlatformAdminController.class.getDeclaredMethods())
.anyMatch(method -> method.isAnnotationPresent(GetMapping.class)
&& Arrays.asList(method.getAnnotation(GetMapping.class).value()).contains(path));
}
private boolean hasPost(String path) {
return Arrays.stream(PlatformAdminController.class.getDeclaredMethods())
.anyMatch(method -> method.isAnnotationPresent(PostMapping.class)
&& Arrays.asList(method.getAnnotation(PostMapping.class).value()).contains(path));
}
private boolean hasPut(String path) {
return Arrays.stream(PlatformAdminController.class.getDeclaredMethods())
.anyMatch(method -> method.isAnnotationPresent(PutMapping.class)
&& Arrays.asList(method.getAnnotation(PutMapping.class).value()).contains(path));
}
private boolean hasDelete(String path) {
return Arrays.stream(PlatformAdminController.class.getDeclaredMethods())
.anyMatch(method -> method.isAnnotationPresent(DeleteMapping.class)
&& Arrays.asList(method.getAnnotation(DeleteMapping.class).value()).contains(path));
}
}

@ -6,7 +6,9 @@ import com.sztzjy.linkCommerce.entity.Userinfo;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PlatformAdminServiceImplTest { class PlatformAdminServiceImplTest {
@Test @Test
@ -47,4 +49,36 @@ class PlatformAdminServiceImplTest {
assertThrows(IllegalArgumentException.class, () -> service.prepareSchoolAdminForSave(userinfo, " ")); assertThrows(IllegalArgumentException.class, () -> service.prepareSchoolAdminForSave(userinfo, " "));
} }
@Test
void prepareTeacherForSaveForcesTeacherRoleAndKeepsTeacherAdminFlag() {
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
Userinfo userinfo = new Userinfo();
userinfo.setRole(1);
userinfo.setSchoolId("other-school");
userinfo.setTeacherAdmin(true);
service.prepareTeacherForSave(userinfo, "school-uuid-1");
assertEquals(3, userinfo.getRole());
assertEquals("school-uuid-1", userinfo.getSchoolId());
assertTrue(userinfo.getTeacherAdmin());
}
@Test
void prepareTeacherForSaveDefaultsTeacherAdminFalse() {
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
Userinfo userinfo = new Userinfo();
service.prepareTeacherForSave(userinfo, "school-uuid-1");
assertFalse(userinfo.getTeacherAdmin());
}
@Test
void assertTeacherCanBeDeletedRejectsTeachersWithCreatedClasses() {
PlatformAdminServiceImpl service = new PlatformAdminServiceImpl();
assertThrows(IllegalArgumentException.class, () -> service.assertTeacherCanBeDeleted(1));
}
} }

Loading…
Cancel
Save