diff --git a/src/main/java/com/example/springdemo/controller/MerchantsController.java b/src/main/java/com/example/springdemo/controller/MerchantsController.java index 463763e..2cc08fb 100644 --- a/src/main/java/com/example/springdemo/controller/MerchantsController.java +++ b/src/main/java/com/example/springdemo/controller/MerchantsController.java @@ -6,6 +6,8 @@ import com.example.springdemo.service.MerchantsService; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; +import java.util.Optional; + @RestController @RequestMapping("/merchants") public class MerchantsController { @@ -13,19 +15,19 @@ public class MerchantsController { private MerchantsService merchantsService; @PostMapping("/add") - public Merchants addMerchants(@RequestBody Merchants merchants, - @RequestBody MerchantsPassword merchantsPassword) { - return merchantsService.addMerchants(merchants, merchantsPassword); + public Merchants addMerchants(@RequestBody Merchants merchant, + @RequestBody MerchantsPassword merchantPassword) { + return merchantsService.addMerchant(merchant, merchantPassword); } @DeleteMapping("/delete/{id}") - public void deleteMerchants(@PathVariable("id") Long merchantID) { - merchantsService.deleteMerchantsById(merchantID); + public void deleteMerchants(@PathVariable("id") Long merchantId) { + merchantsService.deleteMerchantById(merchantId); } @PutMapping("/update/info") - public Merchants updateMerchants(@RequestBody Merchants merchants) { - return merchantsService.updateMerchants(merchants); + public Merchants updateMerchants(@RequestBody Merchants merchant) { + return merchantsService.updateMerchant(merchant); } @GetMapping("/find") @@ -34,17 +36,17 @@ public class MerchantsController { } @GetMapping("/find/{name}") - public Merchants getMerchantsByName(@PathVariable("name") String name) { + public Optional getMerchantsByName(@PathVariable("name") String name) { return merchantsService.findByName(name); } @GetMapping("/find/{id}") - public Merchants getMerchantsById(@PathVariable("id") Long merchantID) { - return merchantsService.findById(merchantID); + public Optional getMerchantsById(@PathVariable("id") Long merchantId) { + return merchantsService.findById(merchantId); } @PutMapping("/update/password") - public void updatePassword(@RequestParam String password, @RequestParam Long merchantID) { - merchantsService.updatePassword(password, merchantID); + public int updatePassword(@RequestParam String password, @RequestParam Long merchantId) { + return merchantsService.updateMerchantPasswordById(password, merchantId); } } diff --git a/src/main/java/com/example/springdemo/controller/PermissionsController.java b/src/main/java/com/example/springdemo/controller/PermissionsController.java index 6d535e1..55a213c 100644 --- a/src/main/java/com/example/springdemo/controller/PermissionsController.java +++ b/src/main/java/com/example/springdemo/controller/PermissionsController.java @@ -6,36 +6,38 @@ import com.example.springdemo.utils.RoleVerificationAnnotation; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.Optional; + @RestController @RequestMapping("/permissions") @RoleVerificationAnnotation(UserIDList = {1}) -//FIXME: Ambiguous mapping. Cannot map 'permissionsController' method public class PermissionsController { @Resource private PermissionsService permissionsService; @PostMapping("/add") - public Permissions addPermissions(@RequestBody Permissions permissions) { - return permissionsService.createPermissions(permissions); + public Permissions addPermissions(@RequestBody Permissions permission) { + return permissionsService.addPermission(permission); } - @DeleteMapping("/delete") - public void deletePermissions(@RequestParam("id") Long permissionsID) { - permissionsService.deletePermissions(permissionsID); + @DeleteMapping("/delete/{id}") + public void deletePermissions(@PathVariable("id") Long permissionId) { + permissionsService.deletePermissionById(permissionId); } @PutMapping("/update") - public Permissions updatePermissions(@RequestBody Permissions permissions) { - return permissionsService.updatePermissions(permissions); + public Permissions updatePermissions(@RequestBody Permissions permission) { + return permissionsService.updatePermission(permission); } - @GetMapping("/get") - public Iterable getAllPermissions() { - return permissionsService.getAllPermissions(); + @GetMapping("/find") + public List getPermissions() { + return permissionsService.findAllPermissions(); } - @GetMapping("/get") - public Permissions getPermissions(@RequestParam Long permissionsID) { - return permissionsService.getPermissions(permissionsID); + @GetMapping("/find/{permissionsId}") + public Optional getPermissionsById(@PathVariable Long permissionsId) { + return permissionsService.findById(permissionsId); } } diff --git a/src/main/java/com/example/springdemo/controller/RolesController.java b/src/main/java/com/example/springdemo/controller/RolesController.java index a6cecc6..b66a603 100644 --- a/src/main/java/com/example/springdemo/controller/RolesController.java +++ b/src/main/java/com/example/springdemo/controller/RolesController.java @@ -6,41 +6,43 @@ import com.example.springdemo.utils.RoleVerificationAnnotation; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.Optional; + @RoleVerificationAnnotation(UserIDList = {1}) @RestController @RequestMapping("/roles") -//FIXME: Ambiguous mapping. Cannot map 'RolesController' method public class RolesController { @Resource private RolesService rolesService; @PostMapping("/add") - public Roles addRoles(@RequestBody Roles roles) { - return rolesService.createRoles(roles); + public Roles addRole(@RequestBody Roles role) { + return rolesService.createRole(role); } - @DeleteMapping("/delete") - public void deleteRoles(@RequestParam("id") Long rolesID) { - rolesService.deleteRoles(rolesID); + @DeleteMapping("/delete/{id}") + public void deleteRole(@PathVariable("id") Long roleId) { + rolesService.deleteRoleById(roleId); } @PutMapping("/update") - public Roles updateRoles(@RequestBody Roles roles) { - return rolesService.updateRoles(roles); + public Roles updateRole(@RequestBody Roles role) { + return rolesService.updateRole(role); } - @GetMapping("/get") - public Iterable getAllRoles() { - return rolesService.getAllRoles(); + @GetMapping("/find") + public List getAllRoles() { + return rolesService.findAllRoles(); } - @GetMapping("/get") - public Roles getRoles(@RequestParam Long rolesID) { - return rolesService.getRolesById(rolesID); + @GetMapping("/find/{roleId}") + public Optional getRolesById(@PathVariable Long roleId) { + return rolesService.findById(roleId); } - @GetMapping("/get") - public Roles getRoles(@RequestParam String rolesName) { - return rolesService.getRolesByName(rolesName); + @GetMapping("/find/{roleName}") + public Optional getRolesByName(@PathVariable String roleName) { + return rolesService.findByName(roleName); } } diff --git a/src/main/java/com/example/springdemo/controller/RolesPermissionsController.java b/src/main/java/com/example/springdemo/controller/RolesPermissionsController.java index edf572b..aeec3ce 100644 --- a/src/main/java/com/example/springdemo/controller/RolesPermissionsController.java +++ b/src/main/java/com/example/springdemo/controller/RolesPermissionsController.java @@ -6,6 +6,9 @@ import com.example.springdemo.utils.RoleVerificationAnnotation; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.Optional; + @RoleVerificationAnnotation(UserIDList = {1}) @RestController @RequestMapping("/rolesPermissions") @@ -19,8 +22,9 @@ public class RolesPermissionsController { } @DeleteMapping("/remove") - public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) { - if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId)) { + public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId, + @RequestParam(name = "permissionId") Long permissionId) { + if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId) != 0) { return "remove success"; } else { return "remove failed, you can't update or delete admin role"; @@ -28,8 +32,9 @@ public class RolesPermissionsController { } @PutMapping("/update") - public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) { - if (rolesPermissionsService.updateRolePermissions(roleId, permissionId)) { + public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId, + @RequestParam(name = "permissionId") Long permissionId) { + if (rolesPermissionsService.updateRolePermissions(roleId, permissionId) != 0) { return "update success"; } else { return "update failed, you can't update or delete admin role"; @@ -37,8 +42,13 @@ public class RolesPermissionsController { } @GetMapping("/find") - public RolesPermissions getRolePermissionById(@RequestParam(name = "rolesPermissionsId") Long rolesPermissionsId) { - return rolesPermissionsService.getRolePermissionById(rolesPermissionsId); + public List findAllRolesPermissions() { + return rolesPermissionsService.findAllRolesPermissions(); + } + + @GetMapping("/find/{rolesPermissionsId}") + public Optional getRolePermissionById(@PathVariable(name = "rolesPermissionsId") Long rolesPermissionsId) { + return rolesPermissionsService.findById(rolesPermissionsId); } } diff --git a/src/main/java/com/example/springdemo/controller/UsersController.java b/src/main/java/com/example/springdemo/controller/UsersController.java index f23940b..20deda7 100644 --- a/src/main/java/com/example/springdemo/controller/UsersController.java +++ b/src/main/java/com/example/springdemo/controller/UsersController.java @@ -7,6 +7,7 @@ import com.example.springdemo.utils.RoleVerificationAnnotation; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; +import java.util.List; import java.util.Optional; @RestController @@ -19,59 +20,58 @@ public class UsersController { //添加新用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @PostMapping("/add") - public Users addUsers(@RequestBody UsersPassword usersPassword, - @RequestBody Users users) { - return usersService.addUsers(users, usersPassword); + public Users addUsers(@RequestBody UsersPassword userPassword, + @RequestBody Users user) { + return usersService.addUser(user, userPassword); } //通过ID删除用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) - @DeleteMapping("/delete/{userID}") - public void deleteUsers(@PathVariable(name = "userID") Long userID) { - usersService.deleteUsersById(userID); + @DeleteMapping("/delete/{id}") + public void deleteUserById(@PathVariable(name = "id") Long userId) { + usersService.deleteUserById(userId); } //通过姓名删除用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @DeleteMapping("/delete/{name}") - public void deleteUsers(@PathVariable(name = "name") String name) { - usersService.deleteUsersByName(name); + public void deleteUserByName(@PathVariable(name = "name") String name) { + usersService.deleteUserByName(name); } //更新用户信息 @RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1}) @PutMapping("/update/info") - public Users updateUsers(@RequestBody Users users) { - return usersService.updateUsers(users); + public Users updateUser(@RequestBody Users user) { + return usersService.updateUser(user); } //查找全部用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @GetMapping("/find") - public Iterable getUsers() { + public List getUsers() { return usersService.findAllUsers(); } //根据姓名查找用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @GetMapping("/find/{name}") - public Users getUsersByName(@PathVariable(name = "name") String name) { + public Optional getUsersByName(@PathVariable(name = "name") String name) { return usersService.findByName(name); } //根据ID查找用户 @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @GetMapping("/find/{id}") - public Users getUsersById(@PathVariable(name = "id") Long userID) { - return usersService.findById(userID); + public Optional getUsersById(@PathVariable(name = "id") Long userId) { + return usersService.findById(userId); } //更新用户密码 @RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1}) @PutMapping("/update/password") - public void updatePassword(@RequestParam(name = "password") String password, - @RequestParam(name = "userID") Long userID) { - usersService.updatePassword(password, userID); + public int updatePassword(@RequestParam(name = "password") String password, + @RequestParam(name = "userId") Long userId) { + return usersService.updateUserPasswordById(password, userId); } - } diff --git a/src/main/java/com/example/springdemo/dao/MerchantsPasswordRepository.java b/src/main/java/com/example/springdemo/dao/MerchantsPasswordRepository.java index f0bea15..2144c84 100644 --- a/src/main/java/com/example/springdemo/dao/MerchantsPasswordRepository.java +++ b/src/main/java/com/example/springdemo/dao/MerchantsPasswordRepository.java @@ -3,16 +3,17 @@ package com.example.springdemo.dao; import com.example.springdemo.entities.password.MerchantsPassword; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface MerchantsPasswordRepository extends JpaRepository { + @Modifying @Query("update MerchantsPassword MerchantsPwd set MerchantsPwd.password = ?1 where MerchantsPwd.merchants.id = ?2") - void updatePassword(String password, Long merchantID); + int updatePassword(String password, Long merchantID); //删除账户时删除密码 - @Query("delete from MerchantsPassword MerchantsPwd where MerchantsPwd.merchants.id = ?1") - void deletePasswordById(Long merchantID); + void deleteByMerchantsId(Long merchantID); } diff --git a/src/main/java/com/example/springdemo/dao/MerchantsRepository.java b/src/main/java/com/example/springdemo/dao/MerchantsRepository.java index 8cbef21..ef2f7b9 100644 --- a/src/main/java/com/example/springdemo/dao/MerchantsRepository.java +++ b/src/main/java/com/example/springdemo/dao/MerchantsRepository.java @@ -4,9 +4,11 @@ import com.example.springdemo.entities.Merchants; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface MerchantsRepository extends JpaRepository { - Merchants findByName(String name); + Optional findByName(String name); } diff --git a/src/main/java/com/example/springdemo/dao/RolesPermissionsRepository.java b/src/main/java/com/example/springdemo/dao/RolesPermissionsRepository.java index c7ca19d..0640ca1 100644 --- a/src/main/java/com/example/springdemo/dao/RolesPermissionsRepository.java +++ b/src/main/java/com/example/springdemo/dao/RolesPermissionsRepository.java @@ -7,19 +7,26 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import java.util.Optional; + @Repository public interface RolesPermissionsRepository extends JpaRepository { + @Modifying + @Query(value = "delete from roles_permissions " + + "where rolesId = ?1 and permissionsId = ?2", nativeQuery = true) + int RemovePermissionsFromRole(Long roleId, Long permissionId); @Modifying - @Transactional - @Query(value = "delete from RolesPermissions " + - "where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true) - void RemovePermissionsFromRole(Long roleId, Long permissionId); + @Query(value = "update roles_permissions set rolesId = ?1, permissionsId = ?2 " + + "where rolesId = ?1 and permissionsId = ?2", nativeQuery = true) + int updateRolePermissions(Long roleId, Long permissionId); - @Modifying - @Transactional - @Query(value = "update RolesPermissions set RolesID = ?1, PermissionsID = ?2 " + - "where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true) - void updateRolePermissions(Long roleId, Long permissionId); + Optional findByRolesId(Long roleId); + + Optional findByPermissionsId(Long permissionId); + + void deleteByRolesId(Long roleId); + + void deleteByPermissionsId(Long permissionId); } diff --git a/src/main/java/com/example/springdemo/dao/RolesRepository.java b/src/main/java/com/example/springdemo/dao/RolesRepository.java index 4fc2fff..913acec 100644 --- a/src/main/java/com/example/springdemo/dao/RolesRepository.java +++ b/src/main/java/com/example/springdemo/dao/RolesRepository.java @@ -4,9 +4,11 @@ import com.example.springdemo.entities.RBAC.Roles; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface RolesRepository extends JpaRepository { - Roles findByName(String name); + Optional findByName(String name); } diff --git a/src/main/java/com/example/springdemo/dao/UsersPasswordRepository.java b/src/main/java/com/example/springdemo/dao/UsersPasswordRepository.java index ac4934f..1245d6d 100644 --- a/src/main/java/com/example/springdemo/dao/UsersPasswordRepository.java +++ b/src/main/java/com/example/springdemo/dao/UsersPasswordRepository.java @@ -2,16 +2,17 @@ package com.example.springdemo.dao; import com.example.springdemo.entities.password.UsersPassword; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface UsersPasswordRepository extends JpaRepository { + @Modifying @Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2") - void updatePassword(String password, Long userID); + int updatePassword(String password, Long userID); //删除账户时删除密码 - @Query("delete from UsersPassword UsersPwd where UsersPwd.users.id = ?1") - void deletePasswordById(Long userID); + void deleteByUsersId(Long userID); } diff --git a/src/main/java/com/example/springdemo/dao/UsersRepository.java b/src/main/java/com/example/springdemo/dao/UsersRepository.java index 8a5b6a5..26e5900 100644 --- a/src/main/java/com/example/springdemo/dao/UsersRepository.java +++ b/src/main/java/com/example/springdemo/dao/UsersRepository.java @@ -2,12 +2,16 @@ package com.example.springdemo.dao; import com.example.springdemo.entities.Users; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface UsersRepository extends JpaRepository { - Users findByName(String name); + Optional findByName(String name); void deleteByName(String name); } diff --git a/src/main/java/com/example/springdemo/service/MerchantsService.java b/src/main/java/com/example/springdemo/service/MerchantsService.java index 81d7abc..a4d2ffb 100644 --- a/src/main/java/com/example/springdemo/service/MerchantsService.java +++ b/src/main/java/com/example/springdemo/service/MerchantsService.java @@ -1,22 +1,28 @@ package com.example.springdemo.service; +import com.example.springdemo.entities.Dishes; import com.example.springdemo.entities.Merchants; import com.example.springdemo.entities.password.MerchantsPassword; +import java.util.List; import java.util.Optional; public interface MerchantsService { - Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword); + // CRUD operations for Merchants entity + Merchants addMerchant(Merchants merchant, MerchantsPassword merchantPassword); - void deleteMerchantsById(Long id); + void deleteMerchantById(Long merchantId); - Merchants updateMerchants(Merchants merchant); + Merchants updateMerchant(Merchants merchant); - Iterable findAllMerchants(); + List findAllMerchants(); - Merchants findById(Long id); + Optional findById(Long merchantId); - Merchants findByName(String name); + Optional findByName(String name); - void updatePassword(String password, Long id); + int updateMerchantPasswordById(String password, Long merchantId); + + // CRUD operations for MerchantsDishes entity + Dishes addDish(Dishes dish, Long merchantId); } diff --git a/src/main/java/com/example/springdemo/service/MerchantsServiceImpl.java b/src/main/java/com/example/springdemo/service/MerchantsServiceImpl.java index e1252cb..251443c 100644 --- a/src/main/java/com/example/springdemo/service/MerchantsServiceImpl.java +++ b/src/main/java/com/example/springdemo/service/MerchantsServiceImpl.java @@ -1,7 +1,9 @@ package com.example.springdemo.service; +import com.example.springdemo.dao.DishesRepository; import com.example.springdemo.dao.MerchantsPasswordRepository; import com.example.springdemo.dao.MerchantsRepository; +import com.example.springdemo.entities.Dishes; import com.example.springdemo.entities.Merchants; import com.example.springdemo.entities.password.MerchantsPassword; import jakarta.annotation.Resource; @@ -10,6 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; @Service public class MerchantsServiceImpl implements MerchantsService { @@ -18,29 +21,30 @@ public class MerchantsServiceImpl implements MerchantsService { @Resource private MerchantsPasswordRepository merchantsPasswordRepository; + @Resource + private DishesRepository dishesRepository; + @Override - @Modifying @Transactional(rollbackFor = Exception.class) //添加商户时添加密码 - public Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword) { + public Merchants addMerchant(Merchants merchant, MerchantsPassword merchantPassword) { Merchants m = merchantsRepository.save(merchant); - merchantsPasswordRepository.save(merchantsPassword); + merchantsPasswordRepository.save(merchantPassword); return m; } @Override - @Modifying @Transactional(rollbackFor = Exception.class) //删除商户时删除密码 - public void deleteMerchantsById(Long merchantID) { - merchantsRepository.deleteById(merchantID); - merchantsPasswordRepository.deletePasswordById(merchantID); + public void deleteMerchantById(Long merchantId) { + merchantsRepository.deleteById(merchantId); + merchantsPasswordRepository.deleteByMerchantsId(merchantId); } @Override - @Modifying @Transactional(rollbackFor = Exception.class) - public Merchants updateMerchants(Merchants merchant) { + //修改商户信息 + public Merchants updateMerchant(Merchants merchant) { return merchantsRepository.save(merchant); } @@ -50,20 +54,25 @@ public class MerchantsServiceImpl implements MerchantsService { } @Override - public Merchants findById(Long id) { - return merchantsRepository.findById(id).orElse(null); + public Optional findById(Long merchantId) { + return merchantsRepository.findById(merchantId); } @Override - public Merchants findByName(String name) { + public Optional findByName(String name) { return merchantsRepository.findByName(name); } @Override - @Modifying @Transactional(rollbackFor = Exception.class) //修改密码 - public void updatePassword(String password, Long merchantID) { - merchantsPasswordRepository.updatePassword(password, merchantID); + public int updateMerchantPasswordById(String password, Long merchantId) { + return merchantsPasswordRepository.updatePassword(password, merchantId); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Dishes addDish(Dishes dish, Long merchantId) { + return dishesRepository.save(dish); } } diff --git a/src/main/java/com/example/springdemo/service/PermissionsService.java b/src/main/java/com/example/springdemo/service/PermissionsService.java index 06d7457..da875ce 100644 --- a/src/main/java/com/example/springdemo/service/PermissionsService.java +++ b/src/main/java/com/example/springdemo/service/PermissionsService.java @@ -2,15 +2,18 @@ package com.example.springdemo.service; import com.example.springdemo.entities.RBAC.Permissions; +import java.util.List; +import java.util.Optional; + public interface PermissionsService { - Permissions createPermissions(Permissions permissions); + Permissions addPermission(Permissions permission); - Permissions updatePermissions(Permissions permissions); + Permissions updatePermission(Permissions permission); - void deletePermissions(Long id); + void deletePermissionById(Long permissionId); - Permissions getPermissions(Long id); + Optional findById(Long permissionId); - Iterable getAllPermissions(); + List findAllPermissions(); } diff --git a/src/main/java/com/example/springdemo/service/PermissionsServiceImpl.java b/src/main/java/com/example/springdemo/service/PermissionsServiceImpl.java index cf3ac1b..f296293 100644 --- a/src/main/java/com/example/springdemo/service/PermissionsServiceImpl.java +++ b/src/main/java/com/example/springdemo/service/PermissionsServiceImpl.java @@ -1,38 +1,58 @@ package com.example.springdemo.service; import com.example.springdemo.dao.PermissionsRepository; +import com.example.springdemo.dao.RolesPermissionsRepository; import com.example.springdemo.entities.RBAC.Permissions; import jakarta.annotation.Resource; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; @Service public class PermissionsServiceImpl implements PermissionsService { @Resource private PermissionsRepository permissionsRepository; - @Override - public Permissions createPermissions(Permissions permissions) { + @Resource + private RolesPermissionsRepository rolesPermissionsRepository; - return permissionsRepository.save(permissions); + @Override + @Transactional(rollbackFor = Exception.class) + public Permissions addPermission(Permissions permission) { + return permissionsRepository.save(permission); } @Override - public Permissions updatePermissions(Permissions permissions) { - return permissionsRepository.save(permissions); + @Transactional(rollbackFor = Exception.class) + public Permissions updatePermission(Permissions permission) { + return permissionsRepository.save(permission); } @Override - public void deletePermissions(Long permissionsID) { - permissionsRepository.deleteById(permissionsID); + @Transactional(rollbackFor = Exception.class) + public void deletePermissionById(Long permissionId) { + if (this.checkPermissionsRolesIfExist(permissionId)) { + //删除权限与角色的关联关系 + rolesPermissionsRepository.deleteByPermissionsId(permissionId); + } + //删除权限 + permissionsRepository.deleteById(permissionId); + } + + private boolean checkPermissionsRolesIfExist(Long permissionId) { + return rolesPermissionsRepository.findByPermissionsId(permissionId).isPresent(); } @Override - public Permissions getPermissions(Long permissionsID) { - return permissionsRepository.findById(permissionsID).orElse(null); + public Optional findById(Long permissionId) { + return permissionsRepository.findById(permissionId); } @Override - public Iterable getAllPermissions() { + public List findAllPermissions() { return permissionsRepository.findAll(); } } diff --git a/src/main/java/com/example/springdemo/service/RolesPermissionsService.java b/src/main/java/com/example/springdemo/service/RolesPermissionsService.java index ceee431..72bc11d 100644 --- a/src/main/java/com/example/springdemo/service/RolesPermissionsService.java +++ b/src/main/java/com/example/springdemo/service/RolesPermissionsService.java @@ -1,18 +1,20 @@ package com.example.springdemo.service; import com.example.springdemo.entities.RBAC.RolesPermissions; -import com.example.springdemo.entities.Users; + +import java.util.List; +import java.util.Optional; public interface RolesPermissionsService { RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions); - Boolean RemovePermissionsFromRole(Long roleId, Long permissionId); + int RemovePermissionsFromRole(Long roleId, Long permissionId); - Boolean updateRolePermissions(Long roleId, Long permissionId); + int updateRolePermissions(Long roleId, Long permissionId); - RolesPermissions getRolePermissionById(Long rolesPermissionsId); + Optional findById(Long rolesPermissionsId); - Iterable getAllRolesPermissions(); + List findAllRolesPermissions(); Boolean CheckIfDeleteOrUpdateAdmin(Long roleId); } diff --git a/src/main/java/com/example/springdemo/service/RolesPermissionsServiceImpl.java b/src/main/java/com/example/springdemo/service/RolesPermissionsServiceImpl.java index 88904de..4526f3b 100644 --- a/src/main/java/com/example/springdemo/service/RolesPermissionsServiceImpl.java +++ b/src/main/java/com/example/springdemo/service/RolesPermissionsServiceImpl.java @@ -4,6 +4,10 @@ import com.example.springdemo.dao.RolesPermissionsRepository; import com.example.springdemo.entities.RBAC.RolesPermissions; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; @Service public class RolesPermissionsServiceImpl implements RolesPermissionsService { @@ -11,43 +15,48 @@ public class RolesPermissionsServiceImpl implements RolesPermissionsService { private RolesPermissionsRepository rolesPermissionsRepository; @Override + @Transactional(rollbackFor = Exception.class) public RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions) { return rolesPermissionsRepository.save(rolesPermissions); } @Override - public Boolean RemovePermissionsFromRole(Long roleId, Long permissionId) { + @Transactional(rollbackFor = Exception.class) + public int RemovePermissionsFromRole(Long roleId, Long permissionId) { + int count; if (this.CheckIfDeleteOrUpdateAdmin(roleId)) { - rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId); - return true; + count = rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId); } else { - return false; + count = 0; } - + return count; } @Override - public Boolean updateRolePermissions(Long roleId, Long permissionId) { + @Transactional(rollbackFor = Exception.class) + public int updateRolePermissions(Long roleId, Long permissionId) { + int count; if (this.CheckIfDeleteOrUpdateAdmin(roleId)) { - rolesPermissionsRepository.updateRolePermissions(roleId, permissionId); - return true; + count = rolesPermissionsRepository.updateRolePermissions(roleId, permissionId); } else { - return false; + count = 0; } + return count; } @Override - public RolesPermissions getRolePermissionById(Long rolesPermissionsId) { - return rolesPermissionsRepository.findById(rolesPermissionsId).orElse(null); + public Optional findById(Long rolesPermissionsId) { + return rolesPermissionsRepository.findById(rolesPermissionsId); } - //TODO: 查询全部权限和角色的对应关系 + //TODO: 查找全部时返回名字而不是id @Override - public Iterable getAllRolesPermissions() { - return null; + public List findAllRolesPermissions() { + return rolesPermissionsRepository.findAll(); } @Override + //检查是否删除或更新管理员 public Boolean CheckIfDeleteOrUpdateAdmin(Long roleId) { return roleId != 1; } diff --git a/src/main/java/com/example/springdemo/service/RolesService.java b/src/main/java/com/example/springdemo/service/RolesService.java index 9860996..59baaaf 100644 --- a/src/main/java/com/example/springdemo/service/RolesService.java +++ b/src/main/java/com/example/springdemo/service/RolesService.java @@ -3,18 +3,21 @@ package com.example.springdemo.service; import com.example.springdemo.entities.RBAC.Roles; import com.example.springdemo.entities.Users; +import java.util.List; +import java.util.Optional; + public interface RolesService { - Roles createRoles(Roles roles); + Roles createRole(Roles role); - Roles updateRoles(Roles roles); + Roles updateRole(Roles role); - void deleteRoles(Long id); + void deleteRoleById(Long roleId); - Iterable getAllRoles(); + List findAllRoles(); - Roles getRolesByName(String name); + Optional findByName(String name); - Roles getRolesById(Long id); + Optional findById(Long roleId); //Users角色管理 public void RemoveRoleFromUser(Long userId, Long roleId); diff --git a/src/main/java/com/example/springdemo/service/RolesServiceImpl.java b/src/main/java/com/example/springdemo/service/RolesServiceImpl.java index 41866d9..56aa877 100644 --- a/src/main/java/com/example/springdemo/service/RolesServiceImpl.java +++ b/src/main/java/com/example/springdemo/service/RolesServiceImpl.java @@ -1,10 +1,15 @@ package com.example.springdemo.service; +import com.example.springdemo.dao.RolesPermissionsRepository; import com.example.springdemo.dao.RolesRepository; import com.example.springdemo.entities.RBAC.Roles; import com.example.springdemo.entities.Users; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; @Service public class RolesServiceImpl implements RolesService { @@ -12,40 +17,59 @@ public class RolesServiceImpl implements RolesService { @Resource private RolesRepository rolesRepository; + @Resource + private RolesPermissionsRepository rolesPermissionsRepository; + @Override - public Roles createRoles(Roles roles) { - return rolesRepository.save(roles); + @Transactional(rollbackFor = Exception.class) + //创建角色 + public Roles createRole(Roles role) { + return rolesRepository.save(role); } @Override - public Roles updateRoles(Roles roles) { - return rolesRepository.save(roles); + @Transactional(rollbackFor = Exception.class) + //更新角色信息 + public Roles updateRole(Roles role) { + return rolesRepository.save(role); } @Override - public void deleteRoles(Long id) { - rolesRepository.deleteById(id); + @Transactional(rollbackFor = Exception.class) + //删除角色时,需要删除角色与权限的关联关系 + public void deleteRoleById(Long roleId) { + if (this.checkRolesPermissionsIfExist(roleId)) { + //删除角色与权限的关联关系 + rolesPermissionsRepository.deleteByRolesId(roleId); + } + //删除角色 + rolesRepository.deleteById(roleId); + + } + + //检查角色与权限的关联关系是否存在 + private boolean checkRolesPermissionsIfExist(Long roleId) { + return rolesPermissionsRepository.findByRolesId(roleId).isPresent(); } @Override - public Iterable getAllRoles() { + public List findAllRoles() { return rolesRepository.findAll(); } @Override - public Roles getRolesByName(String name) { + public Optional findByName(String name) { return rolesRepository.findByName(name); } @Override - public Roles getRolesById(Long id) { - return rolesRepository.findById(id).orElse(null); + public Optional findById(Long roleId) { + return rolesRepository.findById(roleId); } //TODO: Users角色管理 @Override public void RemoveRoleFromUser(Long userId, Long roleId) { - } @Override diff --git a/src/main/java/com/example/springdemo/service/UsersService.java b/src/main/java/com/example/springdemo/service/UsersService.java index ae34bfd..c4c060d 100644 --- a/src/main/java/com/example/springdemo/service/UsersService.java +++ b/src/main/java/com/example/springdemo/service/UsersService.java @@ -3,21 +3,24 @@ package com.example.springdemo.service; import com.example.springdemo.entities.Users; import com.example.springdemo.entities.password.UsersPassword; +import java.util.List; +import java.util.Optional; + public interface UsersService { - Users addUsers(Users user, UsersPassword usersPassword); + Users addUser(Users user, UsersPassword userPassword); - void deleteUsersById(Long userID); + void deleteUserById(Long userId); - void deleteUsersByName(String name); + void deleteUserByName(String name); - Users updateUsers(Users user); + Users updateUser(Users user); - Iterable findAllUsers(); + List findAllUsers(); - Users findById(Long userID); + Optional findById(Long userId); - Users findByName(String name); + Optional findByName(String name); - void updatePassword(String password, Long userID); + int updateUserPasswordById(String password, Long userId); } diff --git a/src/main/java/com/example/springdemo/service/UsersServiceImpl.java b/src/main/java/com/example/springdemo/service/UsersServiceImpl.java index 82e9b3e..9cc6014 100644 --- a/src/main/java/com/example/springdemo/service/UsersServiceImpl.java +++ b/src/main/java/com/example/springdemo/service/UsersServiceImpl.java @@ -9,6 +9,9 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Optional; + @Service public class UsersServiceImpl implements UsersService { @Resource @@ -18,62 +21,63 @@ public class UsersServiceImpl implements UsersService { private UsersPasswordRepository usersPasswordRepository; @Override - @Modifying @Transactional(rollbackFor = Exception.class) //添加用户时同步添加密码 - public Users addUsers(Users user, UsersPassword usersPassword) { + public Users addUser(Users user, UsersPassword userPassword) { Users u = usersRepository.save(user); - usersPasswordRepository.save(usersPassword); + usersPasswordRepository.save(userPassword); return u; } @Override - @Modifying @Transactional(rollbackFor = Exception.class) //删除用户时同步删除密码 - public void deleteUsersById(Long userID) { - usersRepository.deleteById(userID); - usersPasswordRepository.deletePasswordById(userID); + public void deleteUserById(Long userId) { + usersRepository.deleteById(userId); + usersPasswordRepository.deleteByUsersId(userId); } @Override - @Modifying @Transactional(rollbackFor = Exception.class) //删除用户时同步删除密码 - public void deleteUsersByName(String name) { - usersRepository.deleteByName(name); - //获取用户ID - Long userID = usersRepository.findByName(name).getId(); - usersPasswordRepository.deletePasswordById(userID); + public void deleteUserByName(String name) { + Long userId; + //如果用户存在 + if (usersRepository.findByName(name).isPresent()) { + usersRepository.deleteByName(name); + //获取用户ID + userId = usersRepository.findByName(name).get().getId(); + //删除密码 + usersPasswordRepository.deleteByUsersId(userId); + } } @Override - @Modifying @Transactional(rollbackFor = Exception.class) - public Users updateUsers(Users user) { + //更新用户信息 + public Users updateUser(Users user) { return usersRepository.save(user); } @Override - public Iterable findAllUsers() { + public List findAllUsers() { return usersRepository.findAll(); } @Override - public Users findById(Long userID) { - return usersRepository.findById(userID).orElse(null); + public Optional findById(Long userId) { + return usersRepository.findById(userId); } @Override - public Users findByName(String name) { + public Optional findByName(String name) { return usersRepository.findByName(name); } @Override - @Modifying @Transactional(rollbackFor = Exception.class) //更新用户密码 - public void updatePassword(String password, Long userID) { - usersPasswordRepository.updatePassword(password, userID); + public int updateUserPasswordById(String password, Long userId) { + return usersPasswordRepository.updatePassword(password, userId); } }