Add missing CRUD
This commit is contained in:
@@ -12,6 +12,7 @@ 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 static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||
|
||||
@RestController
|
||||
@@ -28,7 +29,7 @@ public class AccountController {
|
||||
}
|
||||
|
||||
@RequestMapping(path = EMAIL_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<Boolean> isEmailValid(HttpServletRequest request,
|
||||
public ResponseEntity<Boolean> isEmailUsed(HttpServletRequest request,
|
||||
@RequestParam(PARAM_EMAIL) String email) {
|
||||
logRequest(request);
|
||||
return accountService.existsWithEmail(email) ?
|
||||
@@ -71,4 +72,48 @@ public class AccountController {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<Account> getAccount(HttpServletRequest request,
|
||||
@RequestParam(PARAM_EMAIL) String email,
|
||||
@RequestParam(PARAM_PASSWORD) String password) {
|
||||
logRequest(request);
|
||||
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) {
|
||||
log.warn("[{}] Invalid Credentials", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
return ResponseEntity.ok(acc);
|
||||
}
|
||||
|
||||
@RequestMapping(path = ACCOUNT_BASE, method = RequestMethod.PUT, produces = "application/json")
|
||||
public ResponseEntity<Account> updateAccount(HttpServletRequest request,
|
||||
@RequestParam(PARAM_EMAIL) String email,
|
||||
@RequestParam(PARAM_PASSWORD) String password,
|
||||
@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(email)) {
|
||||
log.warn("[{}] Account doesn't exist", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
Account loggedIn = accountService.isValidLogin(email, password);
|
||||
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) {
|
||||
return ResponseEntity.internalServerError().build();
|
||||
}
|
||||
return ResponseEntity.ok(saved);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class ArticleController {
|
||||
return ResponseEntity.ok(a != null);
|
||||
}
|
||||
|
||||
@RequestMapping(path = ARTICLE_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||
@RequestMapping(path = ARTICLE_BASE, method = RequestMethod.PUT, produces = "application/json")
|
||||
public ResponseEntity<Boolean> update(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_UUID) UUID uuid,
|
||||
@RequestBody Article article) {
|
||||
@@ -66,4 +66,17 @@ public class ArticleController {
|
||||
|
||||
return ResponseEntity.ok(articleService.save(article) != null);
|
||||
}
|
||||
|
||||
@RequestMapping(path = ARTICLE_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||
public ResponseEntity<Boolean> delete(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_UUID) UUID uuid) {
|
||||
logRequest(request);
|
||||
if (uuid == null || uuid.toString().isEmpty() || articleService.findByUUID(uuid) == null) {
|
||||
log.warn("[{}] got invalid UUID {}", request.getRequestURI(), uuid);
|
||||
return ResponseEntity.badRequest().body(false);
|
||||
}
|
||||
articleService.delete(uuid);
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,21 @@ public class CustomerController {
|
||||
this.customerService = customerService;
|
||||
}
|
||||
|
||||
@RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<Customer> getCustomerById(HttpServletRequest request,
|
||||
@RequestParam(PARAM_ID) Long customerId) {
|
||||
logRequest(request);
|
||||
if (customerId == null) {
|
||||
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
Customer customer = customerService.findById(customerId);
|
||||
if (customer == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
return ResponseEntity.ok(customer);
|
||||
}
|
||||
|
||||
@RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||
public ResponseEntity<Customer> createCustomer(HttpServletRequest request,
|
||||
@RequestBody Customer customer) {
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ImageController {
|
||||
return ResponseEntity.ok(a != null);
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.PUT, produces = "application/json")
|
||||
public ResponseEntity<Boolean> update(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_IMAGE_ID) Long imageId,
|
||||
@RequestBody Image image) {
|
||||
@@ -78,4 +78,20 @@ public class ImageController {
|
||||
image.setId(imageService.getImageById(imageId).getId());
|
||||
return ResponseEntity.ok(imageService.save(image) != null);
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||
public ResponseEntity<Boolean> delete(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_IMAGE_ID) Long imageId) {
|
||||
logRequest(request);
|
||||
if (imageId == null) {
|
||||
log.warn("[{}] got invalid imageId", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().body(false);
|
||||
}
|
||||
if (imageService.getImageById(imageId) != null) {
|
||||
log.warn("[{}] got invalid imageId", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().body(false);
|
||||
}
|
||||
imageService.deleteById(imageId);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,6 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||
Optional<Article> findArticleByName(@NonNull String Name);
|
||||
|
||||
Optional<Article> findArticleByUuid(@NonNull UUID uuid);
|
||||
|
||||
void deleteByUuid(UUID uuid);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ 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);
|
||||
Account isValidLogin(String email, String password);
|
||||
boolean existsWithEmail(String email);
|
||||
}
|
||||
@@ -18,6 +18,8 @@ public interface ArticleService {
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
void delete(UUID uuid);
|
||||
|
||||
Article save(Article article);
|
||||
|
||||
double getRating(Long id);
|
||||
|
||||
@@ -14,6 +14,8 @@ public interface ImageService {
|
||||
|
||||
Image save(Image image);
|
||||
|
||||
void deleteById(Long imageId);
|
||||
|
||||
ImageModel from(Image image);
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,17 +40,12 @@ public class AccountServiceImpl implements AccountService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account getAccountByEmail(String email) {
|
||||
return accountRepository.getAccountByEmail(email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidLogin(String email, String password) {
|
||||
public Account isValidLogin(String email, String password) {
|
||||
Account acc = accountRepository.getAccountByEmail(email);
|
||||
if(acc == null) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
return passwordService.verifyPassword(password, acc.getPassword());
|
||||
return passwordService.verifyPassword(password, acc.getPassword()) ? acc : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -50,6 +50,11 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
articleRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(UUID uuid) {
|
||||
articleRepository.deleteByUuid(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Article save(Article article) {
|
||||
return articleRepository.save(article);
|
||||
|
||||
@@ -41,6 +41,11 @@ public class ImageServiceImpl implements ImageService {
|
||||
return imageRepository.save(image);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long imageId) {
|
||||
imageRepository.deleteById(imageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageModel from(Image image) {
|
||||
return new ImageModel(image.getArticle().getUuid(), image.getUri());
|
||||
|
||||
Reference in New Issue
Block a user