-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathCustomerController.java
More file actions
104 lines (85 loc) · 3.7 KB
/
CustomerController.java
File metadata and controls
104 lines (85 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.SpringRestAPI.Controllers;
import com.SpringRestAPI.Exceptions.MissingApiKeyException;
import com.SpringRestAPI.Models.Customer;
import com.SpringRestAPI.Services.CustomerService;
import jakarta.validation.Valid;
// @Valid Annotations provide a consistent approach to validation, which improves readability and maintainability.
// It gives you automatic error feedback to the client, indicating exactly which fields are incorrect and why.
// @Valid is used for incoming data validation. Like 'Post' and 'Put' requests. It's not needed for others.
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.logging.Logger;
@RestController
@RequestMapping("/api")
public class CustomerController {
CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
// create new customer
@PostMapping("/customer")
public Customer addNewCustomer(
@RequestHeader("API-Key") String apiKey,
@Valid @RequestBody Customer customer ) {
if (!"123456".equals(apiKey)) {
throw new MissingApiKeyException("API-Key header is missing or invalid");
}
Logger myLogger = Logger.getLogger("CustomerController new customer");
myLogger.info("Adding new customer ");
return customerService.addNewCustomer(
customer.getCustomerName(),
customer.getCustomerEmail(),
customer.getCustomerAge(),
customer.getCustomerAddress()
);
}
// get all customer
@GetMapping("/customer")
public List<Customer> getCustomers(
@RequestHeader("API-Key") String apiKey) {
if (!"123456".equals(apiKey)) {
throw new RuntimeException("Invalid API Key");
}
Logger myLogger = Logger.getLogger("CustomerService get all customers");
myLogger.info("Getting all customers");
return customerService.getCustomers();
}
// get customer by email
@GetMapping("/customer/{customerEmail}")
public List<Customer> getCustomers(
@RequestHeader("API-Key") String apiKey,
@PathVariable String customerEmail) {
if (!"123456".equals(apiKey)) {
throw new RuntimeException("Invalid API Key");
}
Logger myLogger = Logger.getLogger("CustomerService getting customer by email");
myLogger.info("Getting customer by email");
return customerService.getCustomerByEmail(customerEmail);
}
// update customer name
@PutMapping("/customer/{currentName}/{newName}")
public Customer updateCustomerName(
@RequestHeader("API-Key") String apiKey,
@Valid @PathVariable String currentName,
@PathVariable String newName) {
if (!"123456".equals(apiKey)) {
throw new RuntimeException("Invalid API Key");
}
Logger myLogger = Logger.getLogger("CustomerController update customer name");
myLogger.info("Updating customer name");
return customerService.updateCustomerName(currentName, newName);
}
// delete customer
@DeleteMapping("/customer/{customerName}")
public Customer deleteCustomer(
@RequestHeader("API-Key") String apiKey,
@PathVariable String customerName) {
if (!"123456".equals(apiKey)) {
throw new RuntimeException("Invalid API Key");
}
Logger myLogger = Logger.getLogger("CustomerController delete customer name");
myLogger.info("Deleting customer name");
return customerService.deleteCustomer(customerName)
.orElseThrow(() -> new RuntimeException("Customer not found"));
}
}