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 c536e5d..508159c 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,6 +14,8 @@ public class ControllerPathConfig { //AccountController public static final String ACCOUNT_BASE = "/account"; + public static final String ACCOUNT_BASE_ALL = "/account/all"; + public static final String EMAIL_BASE = "/email"; //SessionController 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 c28065f..b3c4a0c 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 @@ -2,6 +2,7 @@ package de.htwsaar.webshop.controller; import de.htwsaar.webshop.repository.entities.Account; import de.htwsaar.webshop.service.AccountService; +import de.htwsaar.webshop.service.SessionService; import de.htwsaar.webshop.service.ValidatorService; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; @@ -9,10 +10,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import static de.htwsaar.webshop.config.ControllerPathConfig.ACCOUNT_BASE; -import static de.htwsaar.webshop.config.ControllerPathConfig.EMAIL_BASE; -import static de.htwsaar.webshop.config.ParameterConfig.PARAM_EMAIL; -import static de.htwsaar.webshop.config.ParameterConfig.PARAM_PASSWORD; +import java.util.List; +import java.util.UUID; + +import static de.htwsaar.webshop.config.ControllerPathConfig.*; +import static de.htwsaar.webshop.config.ParameterConfig.*; import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @RestController @@ -21,11 +23,13 @@ public class AccountController { private final AccountService accountService; private final ValidatorService validatorService; + private final SessionService sessionService; @Autowired - public AccountController(AccountService accountService, ValidatorService validatorService) { + public AccountController(AccountService accountService, ValidatorService validatorService, SessionService sessionService) { this.accountService = accountService; this.validatorService = validatorService; + this.sessionService = sessionService; } @RequestMapping(path = EMAIL_BASE, method = RequestMethod.GET, produces = "application/json") @@ -37,6 +41,19 @@ public class AccountController { ResponseEntity.notFound().build(); } + @RequestMapping(path = ACCOUNT_BASE_ALL, method = RequestMethod.GET, produces = "application/json") + public ResponseEntity> getAllAccounts(HttpServletRequest request, + @RequestParam(PARAM_EMAIL) String email, + @RequestParam(PARAM_SESSION) UUID token) { + logRequest(request); + if (!sessionService.isAdmin(token, email)) { + log.warn("[{}] Account isnt allowed to make request {}", request.getRequestURI(), email); + return ResponseEntity.badRequest().build(); + } + + return ResponseEntity.ok(accountService.findAll()); + } + @RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.POST, produces = "application/json") public ResponseEntity createAccount(HttpServletRequest request, @RequestBody Account account) { 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 313a417..7ae0b5b 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 @@ -2,7 +2,11 @@ package de.htwsaar.webshop.service; import de.htwsaar.webshop.repository.entities.Account; +import java.util.List; + public interface AccountService { + List findAll(); + Account saveNew(Account account); Account save(Account account); 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 0f4ae38..74431cc 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 @@ -7,6 +7,8 @@ import de.htwsaar.webshop.service.PasswordService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class AccountServiceImpl implements AccountService { private final AccountRepository accountRepository; @@ -18,6 +20,11 @@ public class AccountServiceImpl implements AccountService { this.passwordService = passwordService; } + @Override + public List findAll() { + return accountRepository.findAll(); + } + @Override public Account save(Account account) { return accountRepository.save(account);