AccountController + AccountService + PasswordService (useless because missing CustomerController/Service)

This commit is contained in:
Tim
2025-05-23 11:12:26 +02:00
parent fa66263c7d
commit 5f386241ff
10 changed files with 204 additions and 0 deletions

View File

@@ -23,6 +23,12 @@
mvn clean package
```
## Interesting Knowledge
- `controller/` includes all REST-Endpoints
- `model/` includes all REST-Responses
- `service/` includes all Business Logic
- `repository/entities` includes all DB-Objects
# Contributors
- Laura Katharina Dolibois
- Mathusan Saravanapavan

View File

@@ -58,6 +58,11 @@
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
<version>6.4.4</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>

View File

@@ -15,4 +15,9 @@ public class ControllerPathConfig {
//ImageController
public static final String IMAGE_BASE = "/image";
public static final String IMAGE_GET_ALL = IMAGE_BASE + "/all";
//AccountController
public static final String ACCOUNT_BASE = "/account";
public static final String EMAIL_BASE = "/email";
}

View File

@@ -7,5 +7,6 @@ public class ParameterConfig {
public static final String PARAM_UUID = "uuid";
public static final String PARAM_ARTICLE_ID = "articleId";
public static final String PARAM_IMAGE_ID = "imageId";
public static final String PARAM_EMAIL = "email";
}

View File

@@ -0,0 +1,74 @@
package de.htwsaar.webshop.controller;
import de.htwsaar.webshop.repository.entities.Account;
import de.htwsaar.webshop.service.AccountService;
import de.htwsaar.webshop.service.ValidatorService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
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.util.LoggerUtil.logRequest;
@RestController
@Slf4j
public class AccountController {
private final AccountService accountService;
private final ValidatorService validatorService;
@Autowired
public AccountController(AccountService accountService, ValidatorService validatorService) {
this.accountService = accountService;
this.validatorService = validatorService;
}
@RequestMapping(path = EMAIL_BASE, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<Boolean> isEmailValid(HttpServletRequest request,
@RequestParam(PARAM_EMAIL) String email) {
logRequest(request);
return accountService.existsWithEmail(email) ?
ResponseEntity.ok(true) :
ResponseEntity.notFound().build();
}
@RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<Void> createAccount(HttpServletRequest request,
@RequestBody Account account) {
logRequest(request);
if (validatorService.isInvalid(account)) {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(accountService.existsWithEmail(account.getEmail())) {
log.warn("[{}] Account cant be created, Email is already in use", request.getRequestURI());
return ResponseEntity.unprocessableEntity().build();
}
if(accountService.saveNew(account).getId() == null) {
return ResponseEntity.internalServerError().build();
}
return ResponseEntity.ok().build();
}
@RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.DELETE, produces = "application/json")
public ResponseEntity<Void> deleteAccount(HttpServletRequest request,
@RequestBody Account account) {
logRequest(request);
if (validatorService.isInvalid(account)) {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(accountService.existsWithEmail(account.getEmail())) {
return ResponseEntity.badRequest().build();
}
if(!accountService.deleteIfExists(account)) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
}

View File

@@ -6,5 +6,7 @@ import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
Account getAccountByEmail(String email);
boolean existsAccountByEmail(String email);
}

View File

@@ -0,0 +1,12 @@
package de.htwsaar.webshop.service;
import de.htwsaar.webshop.repository.entities.Account;
public interface AccountService {
Account saveNew(Account account);
Account save(Account account);
boolean deleteIfExists(Account account);
Account getAccountByEmail(String email);
boolean isValidLogin(String email, String password);
boolean existsWithEmail(String email);
}

View File

@@ -0,0 +1,6 @@
package de.htwsaar.webshop.service;
public interface PasswordService {
String hashPassword(String password);
boolean verifyPassword(String password, String hashedPassword);
}

View File

@@ -0,0 +1,60 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.repository.AccountRepository;
import de.htwsaar.webshop.repository.entities.Account;
import de.htwsaar.webshop.service.AccountService;
import de.htwsaar.webshop.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService {
private final AccountRepository accountRepository;
private final PasswordService passwordService;
@Autowired
public AccountServiceImpl(AccountRepository accountRepository, PasswordService passwordService) {
this.accountRepository = accountRepository;
this.passwordService = passwordService;
}
@Override
public Account save(Account account) {
return accountRepository.save(account);
}
@Override
public Account saveNew(Account account) {
account.setPassword(passwordService.hashPassword(account.getPassword()));
return this.save(account);
}
@Override
public boolean deleteIfExists(Account account) {
Account tbd = accountRepository.getAccountByEmail(account.getEmail());
if(tbd == null) {
return false;
}
accountRepository.delete(tbd);
return true;
}
@Override
public Account getAccountByEmail(String email) {
return accountRepository.getAccountByEmail(email);
}
@Override
public boolean isValidLogin(String email, String password) {
Account acc = accountRepository.getAccountByEmail(email);
if(acc == null) {
return false;
}
return passwordService.verifyPassword(password, acc.getPassword());
}
@Override
public boolean existsWithEmail(String email) {
return accountRepository.existsAccountByEmail(email);
}
}

View File

@@ -0,0 +1,33 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.service.PasswordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
/**
* Implementation of {@link PasswordService} responsible for creating the salt string.
* <p>
* This service generates the salt.
* </p>
*/
@Service
@Slf4j
public class PasswordServiceImpl implements PasswordService {
/**
* A way to generate Password aafterBCrypt Standard <br>
* This is computationally expensive
*
* @return a non-deterministic Salt for BCrypt.
*/
@Override
public String hashPassword(String password) {
return BCrypt.hashpw(password, BCrypt.gensalt(12));
}
@Override
public boolean verifyPassword(String password, String hashedPassword) {
return BCrypt.checkpw(password, hashedPassword);
}
}