Get All Accounts
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<List<Account>> 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<Void> createAccount(HttpServletRequest request,
|
||||
@RequestBody Account account) {
|
||||
|
||||
@@ -2,7 +2,11 @@ package de.htwsaar.webshop.service;
|
||||
|
||||
import de.htwsaar.webshop.repository.entities.Account;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AccountService {
|
||||
List<Account> findAll();
|
||||
|
||||
Account saveNew(Account account);
|
||||
|
||||
Account save(Account account);
|
||||
|
||||
@@ -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<Account> findAll() {
|
||||
return accountRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account save(Account account) {
|
||||
return accountRepository.save(account);
|
||||
|
||||
Reference in New Issue
Block a user