Reformat Backend

This commit is contained in:
Tim
2025-06-11 12:37:04 +02:00
parent 9668f625a9
commit c023b720b2
21 changed files with 87 additions and 75 deletions

View File

@@ -45,11 +45,11 @@ public class AccountController {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(accountService.existsWithEmail(account.getEmail())) {
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) {
if (accountService.saveNew(account).getId() == null) {
return ResponseEntity.internalServerError().build();
}
return ResponseEntity.ok().build();
@@ -63,10 +63,10 @@ public class AccountController {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(accountService.existsWithEmail(account.getEmail())) {
if (accountService.existsWithEmail(account.getEmail())) {
return ResponseEntity.badRequest().build();
}
if(!accountService.deleteIfExists(account)) {
if (!accountService.deleteIfExists(account)) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
@@ -77,12 +77,12 @@ public class AccountController {
@RequestParam(PARAM_EMAIL) String email,
@RequestParam(PARAM_PASSWORD) String password) {
logRequest(request);
if(!accountService.existsWithEmail(email)) {
if (!accountService.existsWithEmail(email)) {
log.warn("[{}] Account doesn't exist", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
Account acc = accountService.isValidLogin(email, password);
if(acc == null) {
if (acc == null) {
log.warn("[{}] Invalid Credentials", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
@@ -95,22 +95,22 @@ public class AccountController {
@RequestParam(PARAM_PASSWORD) String password,
@RequestBody Account account) {
logRequest(request);
if(validatorService.isInvalid(account)) {
if (validatorService.isInvalid(account)) {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(!accountService.existsWithEmail(email)) {
if (!accountService.existsWithEmail(email)) {
log.warn("[{}] Account doesn't exist", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
Account loggedIn = accountService.isValidLogin(email, password);
if(loggedIn == null) {
if (loggedIn == null) {
log.warn("[{}] Invalid Credentials", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
account.setId(loggedIn.getId());
Account saved = accountService.save(account);
if(saved == null) {
if (saved == null) {
return ResponseEntity.internalServerError().build();
}
return ResponseEntity.ok(saved);

View File

@@ -50,7 +50,7 @@ public class CustomerController {
return ResponseEntity.badRequest().build();
}
Customer saved = customerService.save(customer);
if(saved.getId() == null) {
if (saved.getId() == null) {
return ResponseEntity.internalServerError().build();
}
return ResponseEntity.ok(saved);
@@ -77,7 +77,7 @@ public class CustomerController {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().build();
}
if(customerService.findById(id) == null) {
if (customerService.findById(id) == null) {
log.warn("[{}] AccountID doesn't exist", request.getRequestURI());
return ResponseEntity.badRequest().build();
}

View File

@@ -15,7 +15,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static de.htwsaar.webshop.config.ControllerPathConfig.*;
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_ALL;
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_BASE;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
@@ -36,7 +37,7 @@ public class ImageController {
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
List<Image> images = imageService.getImagesByUUID(uuid);
if(images.isEmpty()) {
if (images.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
List<String> images_base = new ArrayList<>();
@@ -50,7 +51,7 @@ public class ImageController {
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
Image image = imageService.getImageByUUID(uuid);
if(image == null) {
if (image == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return ResponseEntity.ok(image.getBase64());

View File

@@ -14,7 +14,8 @@ import java.util.List;
import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.ORDER_GET_ALL;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.config.ParameterConfig.PARAM_CUSTOMER_ID;
import static de.htwsaar.webshop.config.ParameterConfig.PARAM_ID;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
@RestController
@@ -35,7 +36,7 @@ public class OrderController {
@RequestParam(value = PARAM_CUSTOMER_ID) Long customerId) {
logRequest(request);
List<Order> orders = orderService.getAllOrders(customerId);
if(orders.isEmpty()) {
if (orders.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return ResponseEntity.ok(orders);
@@ -46,7 +47,7 @@ public class OrderController {
@RequestParam(value = PARAM_ID) Long orderId) {
logRequest(request);
Order image = orderService.getOrderById(orderId);
if(image == null) {
if (image == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return ResponseEntity.ok(image);

View File

@@ -13,7 +13,8 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
import static de.htwsaar.webshop.config.ControllerPathConfig.*;
import static de.htwsaar.webshop.config.ControllerPathConfig.REVIEW_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.REVIEW_GET_ALL;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
@@ -35,7 +36,7 @@ public class ReviewController {
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
List<ReviewModel> review = reviewService.getAllByUUID(uuid).stream().map(reviewService::toModel).toList();
if(review.isEmpty()) {
if (review.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(review);
@@ -46,7 +47,7 @@ public class ReviewController {
@RequestParam(value = PARAM_ID) Long reviewId) {
logRequest(request);
Review review = reviewService.getReviewById(reviewId);
if(review == null) {
if (review == null) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(review);

View File

@@ -47,12 +47,12 @@ public class SessionController {
@RequestMapping(value = SESSION_BASE, method = RequestMethod.DELETE, produces = "application/json")
public ResponseEntity<Boolean> delete(@RequestParam String email, @RequestParam UUID token) {
if(email == null || token == null) {
if (email == null || token == null) {
log.warn("Got bad request, email and token");
return ResponseEntity.badRequest().body(false);
}
Session session = sessionService.getByToken(token);
if(!sessionService.isValid(session, email)) {
if (!sessionService.isValid(session, email)) {
log.warn("Got bad request, session is invalid");
return ResponseEntity.badRequest().body(false);
}
@@ -62,16 +62,16 @@ public class SessionController {
@RequestMapping(value = SESSION_BASE, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<Boolean> isValid(@RequestParam String email, @RequestParam UUID token) {
if(email == null || token == null) {
if (email == null || token == null) {
log.warn("Got bad request, email and token");
return ResponseEntity.badRequest().body(false);
}
Session session = sessionService.getByToken(token);
if(session == null) {
if (session == null) {
log.warn("Got bad request, session is invalid");
return ResponseEntity.notFound().build();
}
if(sessionService.isValid(session, email)) {
if (sessionService.isValid(session, email)) {
return ResponseEntity.ok(true);
}
return ResponseEntity.notFound().build();

View File

@@ -1,9 +1,7 @@
package de.htwsaar.webshop.model;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -23,5 +21,4 @@ public class SessionModel {
@Min(VALID_MIN_MILLIS_TIMESTAMP)
long timeout;
}

View File

@@ -3,7 +3,6 @@ package de.htwsaar.webshop.repository;
import de.htwsaar.webshop.repository.entities.Article;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Optional;

View File

@@ -1,15 +1,14 @@
package de.htwsaar.webshop.repository;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import de.htwsaar.webshop.repository.entities.Review;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
@Repository
public interface ReviewRepository extends JpaRepository<Review, Long> {

View File

@@ -3,11 +3,7 @@ package de.htwsaar.webshop.repository.entities;
import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.*;
import java.util.ArrayList;
import java.util.List;

View File

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

View File

@@ -4,6 +4,8 @@ import de.htwsaar.webshop.repository.entities.Customer;
public interface CustomerService {
Customer save(Customer customer);
void delete(Customer customer);
Customer findById(Long id);
}

View File

@@ -6,7 +6,10 @@ import java.util.List;
public interface OrderService {
Order save(Order order);
void delete(Long orderId);
Order getOrderById(Long orderId);
List<Order> getAllOrders(Long customerId);
}

View File

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

View File

@@ -1,16 +1,21 @@
package de.htwsaar.webshop.service;
import java.util.List;
import java.util.UUID;
import de.htwsaar.webshop.model.ReviewModel;
import de.htwsaar.webshop.repository.entities.Review;
import java.util.List;
import java.util.UUID;
public interface ReviewService {
Review save(Review review);
Review save(UUID articleUuid, int rating, String content);
void delete(Long reviewId);
Review getReviewById(Long id);
List<Review> getAllByUUID(UUID uuid);
ReviewModel toModel(Review review);
}

View File

@@ -1,6 +1,5 @@
package de.htwsaar.webshop.service;
import de.htwsaar.webshop.model.SessionModel;
import de.htwsaar.webshop.repository.entities.Account;
import de.htwsaar.webshop.repository.entities.Session;
@@ -8,9 +7,14 @@ import java.util.UUID;
public interface SessionService {
Session create(Account account);
void delete(Session session);
Session findByAccount(Account account);
Session getByToken(UUID token);
boolean isValid(Session session, String email);
boolean isValid(UUID token, String email);
}

View File

@@ -32,7 +32,7 @@ public class AccountServiceImpl implements AccountService {
@Override
public boolean deleteIfExists(Account account) {
Account tbd = accountRepository.getAccountByEmail(account.getEmail());
if(tbd == null) {
if (tbd == null) {
return false;
}
accountRepository.delete(tbd);
@@ -42,7 +42,7 @@ public class AccountServiceImpl implements AccountService {
@Override
public Account isValidLogin(String email, String password) {
Account acc = accountRepository.getAccountByEmail(email);
if(acc == null) {
if (acc == null) {
return null;
}
return passwordService.verifyPassword(password, acc.getPassword()) ? acc : null;

View File

@@ -53,7 +53,7 @@ public class ImageServiceImpl implements ImageService {
@Transactional
public Image save(UUID uuid, String uri) {
Article article = articleService.findByUUID(uuid);
if(article == null) {
if (article == null) {
return null;
}
Image image = new Image(null, article, uri);
@@ -63,12 +63,12 @@ public class ImageServiceImpl implements ImageService {
@Override
public Image save(UUID uuid, MultipartFile file) {
if(uuid == null) {
if (uuid == null) {
log.warn("Got no UUID, aborting");
return null;
}
Article article = articleService.findByUUID(uuid);
if(article == null) {
if (article == null) {
log.warn("Could not find article with id {}", uuid);
return null;
}
@@ -79,7 +79,7 @@ public class ImageServiceImpl implements ImageService {
try {
String based64 = Base64.getEncoder().encodeToString(file.getBytes());
if(based64 == null || based64.isEmpty()) {
if (based64 == null || based64.isEmpty()) {
log.warn("Could not save image with id {} and file size {}", uuid, file.getSize());
return null;
}

View File

@@ -32,7 +32,7 @@ public class ReviewServiceImpl implements ReviewService {
@Override
public Review save(UUID articleUuid, int rating, String content) {
if(articleUuid == null) {
if (articleUuid == null) {
return null;
}
Review review = new Review(null, content, articleService.findByUUID(articleUuid), rating, System.currentTimeMillis());

View File

@@ -1,6 +1,5 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.model.SessionModel;
import de.htwsaar.webshop.repository.AccountRepository;
import de.htwsaar.webshop.repository.SessionRepository;
import de.htwsaar.webshop.repository.entities.Account;
@@ -51,14 +50,14 @@ public class SessionServiceImpl implements SessionService {
@Override
public boolean isValid(Session session, String email) {
if(session == null || email == null) {
if (session == null || email == null) {
return false;
}
Account accountEmail = accountRepository.getAccountByEmail(email);
if(!session.getAccount().equals(accountEmail)) {
if (!session.getAccount().equals(accountEmail)) {
return false;
}
if(session.getTimeout() >= System.currentTimeMillis()) {
if (session.getTimeout() >= System.currentTimeMillis()) {
log.info("Session with email {} is expired", email);
delete(session);
return false;