Add initial CustomerController + Service
This commit is contained in:
@@ -20,4 +20,7 @@ public class ControllerPathConfig {
|
|||||||
public static final String ACCOUNT_BASE = "/account";
|
public static final String ACCOUNT_BASE = "/account";
|
||||||
public static final String EMAIL_BASE = "/email";
|
public static final String EMAIL_BASE = "/email";
|
||||||
|
|
||||||
|
//CustomerController
|
||||||
|
public static final String CUSTOMER_BASE = "/customer";
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,5 +8,6 @@ public class ParameterConfig {
|
|||||||
public static final String PARAM_ARTICLE_ID = "articleId";
|
public static final String PARAM_ARTICLE_ID = "articleId";
|
||||||
public static final String PARAM_IMAGE_ID = "imageId";
|
public static final String PARAM_IMAGE_ID = "imageId";
|
||||||
public static final String PARAM_EMAIL = "email";
|
public static final String PARAM_EMAIL = "email";
|
||||||
|
public static final String PARAM_PASSWORD = "password";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package de.htwsaar.webshop.controller;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.entities.Customer;
|
||||||
|
import de.htwsaar.webshop.service.CustomerService;
|
||||||
|
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.CUSTOMER_BASE;
|
||||||
|
import static de.htwsaar.webshop.config.ParameterConfig.PARAM_ID;
|
||||||
|
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class CustomerController {
|
||||||
|
|
||||||
|
private final ValidatorService validatorService;
|
||||||
|
private final CustomerService customerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public CustomerController(ValidatorService validatorService, CustomerService customerService) {
|
||||||
|
this.validatorService = validatorService;
|
||||||
|
this.customerService = customerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||||
|
public ResponseEntity<Customer> createCustomer(HttpServletRequest request,
|
||||||
|
@RequestBody Customer customer) {
|
||||||
|
logRequest(request);
|
||||||
|
if (validatorService.isInvalid(customer)) {
|
||||||
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
Customer saved = customerService.save(customer);
|
||||||
|
if(saved.getId() == null) {
|
||||||
|
return ResponseEntity.internalServerError().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||||
|
public ResponseEntity<Void> deleteCustomer(HttpServletRequest request,
|
||||||
|
@RequestBody Customer customer) {
|
||||||
|
logRequest(request);
|
||||||
|
if (validatorService.isInvalid(customer)) {
|
||||||
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
customerService.delete(customer);
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = CUSTOMER_BASE, method = RequestMethod.PUT, produces = "application/json")
|
||||||
|
public ResponseEntity<Customer> updateCustomer(HttpServletRequest request,
|
||||||
|
@RequestParam(PARAM_ID) Long id,
|
||||||
|
@RequestBody Customer customer) {
|
||||||
|
logRequest(request);
|
||||||
|
if (validatorService.isInvalid(customer)) {
|
||||||
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
if(customerService.findById(id) == null) {
|
||||||
|
log.warn("[{}] AccountID doesn't exist", request.getRequestURI());
|
||||||
|
return ResponseEntity.badRequest().build();
|
||||||
|
}
|
||||||
|
customer.setId(id);
|
||||||
|
return ResponseEntity.ok(customerService.save(customer));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package de.htwsaar.webshop.service;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.entities.Customer;
|
||||||
|
|
||||||
|
public interface CustomerService {
|
||||||
|
Customer save(Customer customer);
|
||||||
|
void delete(Customer customer);
|
||||||
|
Customer findById(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package de.htwsaar.webshop.service.impl;
|
||||||
|
|
||||||
|
import de.htwsaar.webshop.repository.CustomerRepository;
|
||||||
|
import de.htwsaar.webshop.repository.entities.Customer;
|
||||||
|
import de.htwsaar.webshop.service.CustomerService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class CustomerServiceImpl implements CustomerService {
|
||||||
|
private final CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public CustomerServiceImpl(CustomerRepository customerRepository) {
|
||||||
|
this.customerRepository = customerRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Customer save(Customer customer) {
|
||||||
|
return customerRepository.save(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Customer customer) {
|
||||||
|
customerRepository.delete(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Customer findById(Long id) {
|
||||||
|
return customerRepository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user