统一变量命名,逻辑功能完善

This commit is contained in:
myh 2023-11-12 16:47:52 +08:00
parent 9a0f860092
commit 587ac7bb45
21 changed files with 304 additions and 188 deletions

View File

@ -6,6 +6,8 @@ import com.example.springdemo.service.MerchantsService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/merchants") @RequestMapping("/merchants")
public class MerchantsController { public class MerchantsController {
@ -13,19 +15,19 @@ public class MerchantsController {
private MerchantsService merchantsService; private MerchantsService merchantsService;
@PostMapping("/add") @PostMapping("/add")
public Merchants addMerchants(@RequestBody Merchants merchants, public Merchants addMerchants(@RequestBody Merchants merchant,
@RequestBody MerchantsPassword merchantsPassword) { @RequestBody MerchantsPassword merchantPassword) {
return merchantsService.addMerchants(merchants, merchantsPassword); return merchantsService.addMerchant(merchant, merchantPassword);
} }
@DeleteMapping("/delete/{id}") @DeleteMapping("/delete/{id}")
public void deleteMerchants(@PathVariable("id") Long merchantID) { public void deleteMerchants(@PathVariable("id") Long merchantId) {
merchantsService.deleteMerchantsById(merchantID); merchantsService.deleteMerchantById(merchantId);
} }
@PutMapping("/update/info") @PutMapping("/update/info")
public Merchants updateMerchants(@RequestBody Merchants merchants) { public Merchants updateMerchants(@RequestBody Merchants merchant) {
return merchantsService.updateMerchants(merchants); return merchantsService.updateMerchant(merchant);
} }
@GetMapping("/find") @GetMapping("/find")
@ -34,17 +36,17 @@ public class MerchantsController {
} }
@GetMapping("/find/{name}") @GetMapping("/find/{name}")
public Merchants getMerchantsByName(@PathVariable("name") String name) { public Optional<Merchants> getMerchantsByName(@PathVariable("name") String name) {
return merchantsService.findByName(name); return merchantsService.findByName(name);
} }
@GetMapping("/find/{id}") @GetMapping("/find/{id}")
public Merchants getMerchantsById(@PathVariable("id") Long merchantID) { public Optional<Merchants> getMerchantsById(@PathVariable("id") Long merchantId) {
return merchantsService.findById(merchantID); return merchantsService.findById(merchantId);
} }
@PutMapping("/update/password") @PutMapping("/update/password")
public void updatePassword(@RequestParam String password, @RequestParam Long merchantID) { public int updatePassword(@RequestParam String password, @RequestParam Long merchantId) {
merchantsService.updatePassword(password, merchantID); return merchantsService.updateMerchantPasswordById(password, merchantId);
} }
} }

View File

@ -6,36 +6,38 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/permissions") @RequestMapping("/permissions")
@RoleVerificationAnnotation(UserIDList = {1}) @RoleVerificationAnnotation(UserIDList = {1})
//FIXME: Ambiguous mapping. Cannot map 'permissionsController' method
public class PermissionsController { public class PermissionsController {
@Resource @Resource
private PermissionsService permissionsService; private PermissionsService permissionsService;
@PostMapping("/add") @PostMapping("/add")
public Permissions addPermissions(@RequestBody Permissions permissions) { public Permissions addPermissions(@RequestBody Permissions permission) {
return permissionsService.createPermissions(permissions); return permissionsService.addPermission(permission);
} }
@DeleteMapping("/delete") @DeleteMapping("/delete/{id}")
public void deletePermissions(@RequestParam("id") Long permissionsID) { public void deletePermissions(@PathVariable("id") Long permissionId) {
permissionsService.deletePermissions(permissionsID); permissionsService.deletePermissionById(permissionId);
} }
@PutMapping("/update") @PutMapping("/update")
public Permissions updatePermissions(@RequestBody Permissions permissions) { public Permissions updatePermissions(@RequestBody Permissions permission) {
return permissionsService.updatePermissions(permissions); return permissionsService.updatePermission(permission);
} }
@GetMapping("/get") @GetMapping("/find")
public Iterable<Permissions> getAllPermissions() { public List<Permissions> getPermissions() {
return permissionsService.getAllPermissions(); return permissionsService.findAllPermissions();
} }
@GetMapping("/get") @GetMapping("/find/{permissionsId}")
public Permissions getPermissions(@RequestParam Long permissionsID) { public Optional<Permissions> getPermissionsById(@PathVariable Long permissionsId) {
return permissionsService.getPermissions(permissionsID); return permissionsService.findById(permissionsId);
} }
} }

View File

@ -6,41 +6,43 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RoleVerificationAnnotation(UserIDList = {1}) @RoleVerificationAnnotation(UserIDList = {1})
@RestController @RestController
@RequestMapping("/roles") @RequestMapping("/roles")
//FIXME: Ambiguous mapping. Cannot map 'RolesController' method
public class RolesController { public class RolesController {
@Resource @Resource
private RolesService rolesService; private RolesService rolesService;
@PostMapping("/add") @PostMapping("/add")
public Roles addRoles(@RequestBody Roles roles) { public Roles addRole(@RequestBody Roles role) {
return rolesService.createRoles(roles); return rolesService.createRole(role);
} }
@DeleteMapping("/delete") @DeleteMapping("/delete/{id}")
public void deleteRoles(@RequestParam("id") Long rolesID) { public void deleteRole(@PathVariable("id") Long roleId) {
rolesService.deleteRoles(rolesID); rolesService.deleteRoleById(roleId);
} }
@PutMapping("/update") @PutMapping("/update")
public Roles updateRoles(@RequestBody Roles roles) { public Roles updateRole(@RequestBody Roles role) {
return rolesService.updateRoles(roles); return rolesService.updateRole(role);
} }
@GetMapping("/get") @GetMapping("/find")
public Iterable<Roles> getAllRoles() { public List<Roles> getAllRoles() {
return rolesService.getAllRoles(); return rolesService.findAllRoles();
} }
@GetMapping("/get") @GetMapping("/find/{roleId}")
public Roles getRoles(@RequestParam Long rolesID) { public Optional<Roles> getRolesById(@PathVariable Long roleId) {
return rolesService.getRolesById(rolesID); return rolesService.findById(roleId);
} }
@GetMapping("/get") @GetMapping("/find/{roleName}")
public Roles getRoles(@RequestParam String rolesName) { public Optional<Roles> getRolesByName(@PathVariable String roleName) {
return rolesService.getRolesByName(rolesName); return rolesService.findByName(roleName);
} }
} }

View File

@ -6,6 +6,9 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RoleVerificationAnnotation(UserIDList = {1}) @RoleVerificationAnnotation(UserIDList = {1})
@RestController @RestController
@RequestMapping("/rolesPermissions") @RequestMapping("/rolesPermissions")
@ -19,8 +22,9 @@ public class RolesPermissionsController {
} }
@DeleteMapping("/remove") @DeleteMapping("/remove")
public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) { public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId,
if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId)) { @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId) != 0) {
return "remove success"; return "remove success";
} else { } else {
return "remove failed, you can't update or delete admin role"; return "remove failed, you can't update or delete admin role";
@ -28,8 +32,9 @@ public class RolesPermissionsController {
} }
@PutMapping("/update") @PutMapping("/update")
public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) { public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId,
if (rolesPermissionsService.updateRolePermissions(roleId, permissionId)) { @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.updateRolePermissions(roleId, permissionId) != 0) {
return "update success"; return "update success";
} else { } else {
return "update failed, you can't update or delete admin role"; return "update failed, you can't update or delete admin role";
@ -37,8 +42,13 @@ public class RolesPermissionsController {
} }
@GetMapping("/find") @GetMapping("/find")
public RolesPermissions getRolePermissionById(@RequestParam(name = "rolesPermissionsId") Long rolesPermissionsId) { public List<RolesPermissions> findAllRolesPermissions() {
return rolesPermissionsService.getRolePermissionById(rolesPermissionsId); return rolesPermissionsService.findAllRolesPermissions();
}
@GetMapping("/find/{rolesPermissionsId}")
public Optional<RolesPermissions> getRolePermissionById(@PathVariable(name = "rolesPermissionsId") Long rolesPermissionsId) {
return rolesPermissionsService.findById(rolesPermissionsId);
} }
} }

View File

@ -7,6 +7,7 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
@ -19,59 +20,58 @@ public class UsersController {
//添加新用户 //添加新用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@PostMapping("/add") @PostMapping("/add")
public Users addUsers(@RequestBody UsersPassword usersPassword, public Users addUsers(@RequestBody UsersPassword userPassword,
@RequestBody Users users) { @RequestBody Users user) {
return usersService.addUsers(users, usersPassword); return usersService.addUser(user, userPassword);
} }
//通过ID删除用户 //通过ID删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete/{userID}") @DeleteMapping("/delete/{id}")
public void deleteUsers(@PathVariable(name = "userID") Long userID) { public void deleteUserById(@PathVariable(name = "id") Long userId) {
usersService.deleteUsersById(userID); usersService.deleteUserById(userId);
} }
//通过姓名删除用户 //通过姓名删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete/{name}") @DeleteMapping("/delete/{name}")
public void deleteUsers(@PathVariable(name = "name") String name) { public void deleteUserByName(@PathVariable(name = "name") String name) {
usersService.deleteUsersByName(name); usersService.deleteUserByName(name);
} }
//更新用户信息 //更新用户信息
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/info") @PutMapping("/update/info")
public Users updateUsers(@RequestBody Users users) { public Users updateUser(@RequestBody Users user) {
return usersService.updateUsers(users); return usersService.updateUser(user);
} }
//查找全部用户 //查找全部用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find") @GetMapping("/find")
public Iterable<Users> getUsers() { public List<Users> getUsers() {
return usersService.findAllUsers(); return usersService.findAllUsers();
} }
//根据姓名查找用户 //根据姓名查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find/{name}") @GetMapping("/find/{name}")
public Users getUsersByName(@PathVariable(name = "name") String name) { public Optional<Users> getUsersByName(@PathVariable(name = "name") String name) {
return usersService.findByName(name); return usersService.findByName(name);
} }
//根据ID查找用户 //根据ID查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find/{id}") @GetMapping("/find/{id}")
public Users getUsersById(@PathVariable(name = "id") Long userID) { public Optional<Users> getUsersById(@PathVariable(name = "id") Long userId) {
return usersService.findById(userID); return usersService.findById(userId);
} }
//更新用户密码 //更新用户密码
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1}) @RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/password") @PutMapping("/update/password")
public void updatePassword(@RequestParam(name = "password") String password, public int updatePassword(@RequestParam(name = "password") String password,
@RequestParam(name = "userID") Long userID) { @RequestParam(name = "userId") Long userId) {
usersService.updatePassword(password, userID); return usersService.updateUserPasswordById(password, userId);
} }
} }

View File

@ -3,16 +3,17 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.password.MerchantsPassword; import com.example.springdemo.entities.password.MerchantsPassword;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface MerchantsPasswordRepository public interface MerchantsPasswordRepository
extends JpaRepository<MerchantsPassword, Long> { extends JpaRepository<MerchantsPassword, Long> {
@Modifying
@Query("update MerchantsPassword MerchantsPwd set MerchantsPwd.password = ?1 where MerchantsPwd.merchants.id = ?2") @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 deleteByMerchantsId(Long merchantID);
void deletePasswordById(Long merchantID);
} }

View File

@ -4,9 +4,11 @@ import com.example.springdemo.entities.Merchants;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface MerchantsRepository public interface MerchantsRepository
extends JpaRepository<Merchants, Long> { extends JpaRepository<Merchants, Long> {
Merchants findByName(String name); Optional<Merchants> findByName(String name);
} }

View File

@ -7,19 +7,26 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
@Repository @Repository
public interface RolesPermissionsRepository public interface RolesPermissionsRepository
extends JpaRepository<RolesPermissions, Long> { extends JpaRepository<RolesPermissions, Long> {
@Modifying
@Query(value = "delete from roles_permissions " +
"where rolesId = ?1 and permissionsId = ?2", nativeQuery = true)
int RemovePermissionsFromRole(Long roleId, Long permissionId);
@Modifying @Modifying
@Transactional @Query(value = "update roles_permissions set rolesId = ?1, permissionsId = ?2 " +
@Query(value = "delete from RolesPermissions " + "where rolesId = ?1 and permissionsId = ?2", nativeQuery = true)
"where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true) int updateRolePermissions(Long roleId, Long permissionId);
void RemovePermissionsFromRole(Long roleId, Long permissionId);
@Modifying Optional<RolesPermissions> findByRolesId(Long roleId);
@Transactional
@Query(value = "update RolesPermissions set RolesID = ?1, PermissionsID = ?2 " + Optional<RolesPermissions> findByPermissionsId(Long permissionId);
"where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true)
void updateRolePermissions(Long roleId, Long permissionId); void deleteByRolesId(Long roleId);
void deleteByPermissionsId(Long permissionId);
} }

View File

@ -4,9 +4,11 @@ import com.example.springdemo.entities.RBAC.Roles;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface RolesRepository public interface RolesRepository
extends JpaRepository<Roles, Long> { extends JpaRepository<Roles, Long> {
Roles findByName(String name); Optional<Roles> findByName(String name);
} }

View File

@ -2,16 +2,17 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.password.UsersPassword; import com.example.springdemo.entities.password.UsersPassword;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UsersPasswordRepository public interface UsersPasswordRepository
extends JpaRepository<UsersPassword, Long> { extends JpaRepository<UsersPassword, Long> {
@Modifying
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2") @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 deleteByUsersId(Long userID);
void deletePasswordById(Long userID);
} }

View File

@ -2,12 +2,16 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository @Repository
public interface UsersRepository public interface UsersRepository
extends JpaRepository<Users, Long> { extends JpaRepository<Users, Long> {
Users findByName(String name); Optional<Users> findByName(String name);
void deleteByName(String name); void deleteByName(String name);
} }

View File

@ -1,22 +1,28 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.entities.Dishes;
import com.example.springdemo.entities.Merchants; import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword; import com.example.springdemo.entities.password.MerchantsPassword;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface MerchantsService { 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<Merchants> findAllMerchants(); List<Merchants> findAllMerchants();
Merchants findById(Long id); Optional<Merchants> findById(Long merchantId);
Merchants findByName(String name); Optional<Merchants> 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);
} }

View File

@ -1,7 +1,9 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.dao.DishesRepository;
import com.example.springdemo.dao.MerchantsPasswordRepository; import com.example.springdemo.dao.MerchantsPasswordRepository;
import com.example.springdemo.dao.MerchantsRepository; import com.example.springdemo.dao.MerchantsRepository;
import com.example.springdemo.entities.Dishes;
import com.example.springdemo.entities.Merchants; import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword; import com.example.springdemo.entities.password.MerchantsPassword;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -10,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class MerchantsServiceImpl implements MerchantsService { public class MerchantsServiceImpl implements MerchantsService {
@ -18,29 +21,30 @@ public class MerchantsServiceImpl implements MerchantsService {
@Resource @Resource
private MerchantsPasswordRepository merchantsPasswordRepository; private MerchantsPasswordRepository merchantsPasswordRepository;
@Resource
private DishesRepository dishesRepository;
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//添加商户时添加密码 //添加商户时添加密码
public Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword) { public Merchants addMerchant(Merchants merchant, MerchantsPassword merchantPassword) {
Merchants m = merchantsRepository.save(merchant); Merchants m = merchantsRepository.save(merchant);
merchantsPasswordRepository.save(merchantsPassword); merchantsPasswordRepository.save(merchantPassword);
return m; return m;
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//删除商户时删除密码 //删除商户时删除密码
public void deleteMerchantsById(Long merchantID) { public void deleteMerchantById(Long merchantId) {
merchantsRepository.deleteById(merchantID); merchantsRepository.deleteById(merchantId);
merchantsPasswordRepository.deletePasswordById(merchantID); merchantsPasswordRepository.deleteByMerchantsId(merchantId);
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Merchants updateMerchants(Merchants merchant) { //修改商户信息
public Merchants updateMerchant(Merchants merchant) {
return merchantsRepository.save(merchant); return merchantsRepository.save(merchant);
} }
@ -50,20 +54,25 @@ public class MerchantsServiceImpl implements MerchantsService {
} }
@Override @Override
public Merchants findById(Long id) { public Optional<Merchants> findById(Long merchantId) {
return merchantsRepository.findById(id).orElse(null); return merchantsRepository.findById(merchantId);
} }
@Override @Override
public Merchants findByName(String name) { public Optional<Merchants> findByName(String name) {
return merchantsRepository.findByName(name); return merchantsRepository.findByName(name);
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//修改密码 //修改密码
public void updatePassword(String password, Long merchantID) { public int updateMerchantPasswordById(String password, Long merchantId) {
merchantsPasswordRepository.updatePassword(password, merchantID); return merchantsPasswordRepository.updatePassword(password, merchantId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Dishes addDish(Dishes dish, Long merchantId) {
return dishesRepository.save(dish);
} }
} }

View File

@ -2,15 +2,18 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Permissions; import com.example.springdemo.entities.RBAC.Permissions;
import java.util.List;
import java.util.Optional;
public interface PermissionsService { 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<Permissions> findById(Long permissionId);
Iterable<Permissions> getAllPermissions(); List<Permissions> findAllPermissions();
} }

View File

@ -1,38 +1,58 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.dao.PermissionsRepository; import com.example.springdemo.dao.PermissionsRepository;
import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.entities.RBAC.Permissions; import com.example.springdemo.entities.RBAC.Permissions;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service @Service
public class PermissionsServiceImpl implements PermissionsService { public class PermissionsServiceImpl implements PermissionsService {
@Resource @Resource
private PermissionsRepository permissionsRepository; private PermissionsRepository permissionsRepository;
@Override @Resource
public Permissions createPermissions(Permissions permissions) { private RolesPermissionsRepository rolesPermissionsRepository;
return permissionsRepository.save(permissions); @Override
@Transactional(rollbackFor = Exception.class)
public Permissions addPermission(Permissions permission) {
return permissionsRepository.save(permission);
} }
@Override @Override
public Permissions updatePermissions(Permissions permissions) { @Transactional(rollbackFor = Exception.class)
return permissionsRepository.save(permissions); public Permissions updatePermission(Permissions permission) {
return permissionsRepository.save(permission);
} }
@Override @Override
public void deletePermissions(Long permissionsID) { @Transactional(rollbackFor = Exception.class)
permissionsRepository.deleteById(permissionsID); 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 @Override
public Permissions getPermissions(Long permissionsID) { public Optional<Permissions> findById(Long permissionId) {
return permissionsRepository.findById(permissionsID).orElse(null); return permissionsRepository.findById(permissionId);
} }
@Override @Override
public Iterable<Permissions> getAllPermissions() { public List<Permissions> findAllPermissions() {
return permissionsRepository.findAll(); return permissionsRepository.findAll();
} }
} }

View File

@ -1,18 +1,20 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.RolesPermissions; import com.example.springdemo.entities.RBAC.RolesPermissions;
import com.example.springdemo.entities.Users;
import java.util.List;
import java.util.Optional;
public interface RolesPermissionsService { public interface RolesPermissionsService {
RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions); 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<RolesPermissions> findById(Long rolesPermissionsId);
Iterable<RolesPermissions> getAllRolesPermissions(); List<RolesPermissions> findAllRolesPermissions();
Boolean CheckIfDeleteOrUpdateAdmin(Long roleId); Boolean CheckIfDeleteOrUpdateAdmin(Long roleId);
} }

View File

@ -4,6 +4,10 @@ import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.entities.RBAC.RolesPermissions; import com.example.springdemo.entities.RBAC.RolesPermissions;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service @Service
public class RolesPermissionsServiceImpl implements RolesPermissionsService { public class RolesPermissionsServiceImpl implements RolesPermissionsService {
@ -11,43 +15,48 @@ public class RolesPermissionsServiceImpl implements RolesPermissionsService {
private RolesPermissionsRepository rolesPermissionsRepository; private RolesPermissionsRepository rolesPermissionsRepository;
@Override @Override
@Transactional(rollbackFor = Exception.class)
public RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions) { public RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions) {
return rolesPermissionsRepository.save(rolesPermissions); return rolesPermissionsRepository.save(rolesPermissions);
} }
@Override @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)) { if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId); count = rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId);
return true;
} else { } else {
return false; count = 0;
} }
return count;
} }
@Override @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)) { if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.updateRolePermissions(roleId, permissionId); count = rolesPermissionsRepository.updateRolePermissions(roleId, permissionId);
return true;
} else { } else {
return false; count = 0;
} }
return count;
} }
@Override @Override
public RolesPermissions getRolePermissionById(Long rolesPermissionsId) { public Optional<RolesPermissions> findById(Long rolesPermissionsId) {
return rolesPermissionsRepository.findById(rolesPermissionsId).orElse(null); return rolesPermissionsRepository.findById(rolesPermissionsId);
} }
//TODO: 询全部权限和角色的对应关系 //TODO: 找全部时返回名字而不是id
@Override @Override
public Iterable<RolesPermissions> getAllRolesPermissions() { public List<RolesPermissions> findAllRolesPermissions() {
return null; return rolesPermissionsRepository.findAll();
} }
@Override @Override
//检查是否删除或更新管理员
public Boolean CheckIfDeleteOrUpdateAdmin(Long roleId) { public Boolean CheckIfDeleteOrUpdateAdmin(Long roleId) {
return roleId != 1; return roleId != 1;
} }

View File

@ -3,18 +3,21 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Roles; import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import java.util.List;
import java.util.Optional;
public interface RolesService { 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<Roles> getAllRoles(); List<Roles> findAllRoles();
Roles getRolesByName(String name); Optional<Roles> findByName(String name);
Roles getRolesById(Long id); Optional<Roles> findById(Long roleId);
//Users角色管理 //Users角色管理
public void RemoveRoleFromUser(Long userId, Long roleId); public void RemoveRoleFromUser(Long userId, Long roleId);

View File

@ -1,10 +1,15 @@
package com.example.springdemo.service; package com.example.springdemo.service;
import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.dao.RolesRepository; import com.example.springdemo.dao.RolesRepository;
import com.example.springdemo.entities.RBAC.Roles; import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service @Service
public class RolesServiceImpl implements RolesService { public class RolesServiceImpl implements RolesService {
@ -12,40 +17,59 @@ public class RolesServiceImpl implements RolesService {
@Resource @Resource
private RolesRepository rolesRepository; private RolesRepository rolesRepository;
@Resource
private RolesPermissionsRepository rolesPermissionsRepository;
@Override @Override
public Roles createRoles(Roles roles) { @Transactional(rollbackFor = Exception.class)
return rolesRepository.save(roles); //创建角色
public Roles createRole(Roles role) {
return rolesRepository.save(role);
} }
@Override @Override
public Roles updateRoles(Roles roles) { @Transactional(rollbackFor = Exception.class)
return rolesRepository.save(roles); //更新角色信息
public Roles updateRole(Roles role) {
return rolesRepository.save(role);
} }
@Override @Override
public void deleteRoles(Long id) { @Transactional(rollbackFor = Exception.class)
rolesRepository.deleteById(id); //删除角色时需要删除角色与权限的关联关系
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 @Override
public Iterable<Roles> getAllRoles() { public List<Roles> findAllRoles() {
return rolesRepository.findAll(); return rolesRepository.findAll();
} }
@Override @Override
public Roles getRolesByName(String name) { public Optional<Roles> findByName(String name) {
return rolesRepository.findByName(name); return rolesRepository.findByName(name);
} }
@Override @Override
public Roles getRolesById(Long id) { public Optional<Roles> findById(Long roleId) {
return rolesRepository.findById(id).orElse(null); return rolesRepository.findById(roleId);
} }
//TODO: Users角色管理 //TODO: Users角色管理
@Override @Override
public void RemoveRoleFromUser(Long userId, Long roleId) { public void RemoveRoleFromUser(Long userId, Long roleId) {
} }
@Override @Override

View File

@ -3,21 +3,24 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.Users; import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword; import com.example.springdemo.entities.password.UsersPassword;
import java.util.List;
import java.util.Optional;
public interface UsersService { 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<Users> findAllUsers(); List<Users> findAllUsers();
Users findById(Long userID); Optional<Users> findById(Long userId);
Users findByName(String name); Optional<Users> findByName(String name);
void updatePassword(String password, Long userID); int updateUserPasswordById(String password, Long userId);
} }

View File

@ -9,6 +9,9 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service @Service
public class UsersServiceImpl implements UsersService { public class UsersServiceImpl implements UsersService {
@Resource @Resource
@ -18,62 +21,63 @@ public class UsersServiceImpl implements UsersService {
private UsersPasswordRepository usersPasswordRepository; private UsersPasswordRepository usersPasswordRepository;
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//添加用户时同步添加密码 //添加用户时同步添加密码
public Users addUsers(Users user, UsersPassword usersPassword) { public Users addUser(Users user, UsersPassword userPassword) {
Users u = usersRepository.save(user); Users u = usersRepository.save(user);
usersPasswordRepository.save(usersPassword); usersPasswordRepository.save(userPassword);
return u; return u;
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码 //删除用户时同步删除密码
public void deleteUsersById(Long userID) { public void deleteUserById(Long userId) {
usersRepository.deleteById(userID); usersRepository.deleteById(userId);
usersPasswordRepository.deletePasswordById(userID); usersPasswordRepository.deleteByUsersId(userId);
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码 //删除用户时同步删除密码
public void deleteUsersByName(String name) { public void deleteUserByName(String name) {
usersRepository.deleteByName(name); Long userId;
//获取用户ID //如果用户存在
Long userID = usersRepository.findByName(name).getId(); if (usersRepository.findByName(name).isPresent()) {
usersPasswordRepository.deletePasswordById(userID); usersRepository.deleteByName(name);
//获取用户ID
userId = usersRepository.findByName(name).get().getId();
//删除密码
usersPasswordRepository.deleteByUsersId(userId);
}
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Users updateUsers(Users user) { //更新用户信息
public Users updateUser(Users user) {
return usersRepository.save(user); return usersRepository.save(user);
} }
@Override @Override
public Iterable<Users> findAllUsers() { public List<Users> findAllUsers() {
return usersRepository.findAll(); return usersRepository.findAll();
} }
@Override @Override
public Users findById(Long userID) { public Optional<Users> findById(Long userId) {
return usersRepository.findById(userID).orElse(null); return usersRepository.findById(userId);
} }
@Override @Override
public Users findByName(String name) { public Optional<Users> findByName(String name) {
return usersRepository.findByName(name); return usersRepository.findByName(name);
} }
@Override @Override
@Modifying
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//更新用户密码 //更新用户密码
public void updatePassword(String password, Long userID) { public int updateUserPasswordById(String password, Long userId) {
usersPasswordRepository.updatePassword(password, userID); return usersPasswordRepository.updatePassword(password, userId);
} }
} }