From abe9a76fc0e67177bea9fb663c85fc6c0188bee5 Mon Sep 17 00:00:00 2001 From: Tim <47184194+imgde@users.noreply.github.com> Date: Wed, 18 Jun 2025 13:55:07 +0200 Subject: [PATCH] /account/admin DELETE --- .../webshop/config/ControllerPathConfig.java | 2 ++ .../webshop/controller/AccountController.java | 16 ++++++++++++++++ .../webshop/repository/AccountRepository.java | 2 ++ .../htwsaar/webshop/service/AccountService.java | 2 ++ .../webshop/service/impl/AccountServiceImpl.java | 10 ++++++++++ 5 files changed, 32 insertions(+) diff --git a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java index bbd4dac..e738b97 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java @@ -14,7 +14,9 @@ public class ControllerPathConfig { //AccountController public static final String ACCOUNT_BASE = "/account"; + public static final String ACCOUNT_BASE_ALL = "/account/all"; + public static final String ACCOUNT_ADMIN = "/account/admin"; public static final String EMAIL_BASE = "/email"; diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/AccountController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/AccountController.java index 6867a91..28d1688 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/AccountController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/AccountController.java @@ -93,6 +93,22 @@ public class AccountController { return ResponseEntity.ok().build(); } + @RequestMapping(path = ACCOUNT_ADMIN, method = RequestMethod.DELETE, produces = "application/json") + public ResponseEntity deleteAccount(HttpServletRequest request, + @RequestParam(PARAM_EMAIL) String email, + @RequestParam(PARAM_PASSWORD) UUID token, + @RequestParam(PARAM_ID) Long accountId) { + logRequest(request); + if (!sessionService.isAdmin(token, email)) { + log.warn("Invalid session requesting Admin {}", token); + return ResponseEntity.status(403).build(); + } + if(!accountService.deleteIfExists(accountId)) { + return ResponseEntity.badRequest().build(); + } + return ResponseEntity.ok(); + } + @RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.GET, produces = "application/json") public ResponseEntity getAccount(HttpServletRequest request, @RequestParam(PARAM_EMAIL) String email, diff --git a/00-backend/src/main/java/de/htwsaar/webshop/repository/AccountRepository.java b/00-backend/src/main/java/de/htwsaar/webshop/repository/AccountRepository.java index 8dedd67..460cf8d 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/repository/AccountRepository.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/repository/AccountRepository.java @@ -9,4 +9,6 @@ public interface AccountRepository extends JpaRepository { Account getAccountByEmail(String email); boolean existsAccountByEmail(String email); + + Account getAccountById(Long id); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/AccountService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/AccountService.java index 7ae0b5b..dafa148 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/AccountService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/AccountService.java @@ -13,6 +13,8 @@ public interface AccountService { boolean deleteIfExists(Account account); + boolean deleteIfExists(Long id); + Account isValidLogin(String email, String password); boolean existsWithEmail(String email); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/AccountServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/AccountServiceImpl.java index cfdeea2..a3049a1 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/AccountServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/AccountServiceImpl.java @@ -53,6 +53,16 @@ public class AccountServiceImpl implements AccountService { return true; } + @Override + public boolean deleteIfExists(Long id) { + Account tbd = accountRepository.getAccountById(id); + if (tbd == null) { + return false; + } + accountRepository.delete(tbd); + return true; + } + @Override public Account isValidLogin(String email, String password) { Account acc = accountRepository.getAccountByEmail(email);