11package com .example .relationaldataaccess .controller ;
22
3- import com .example .relationaldataaccess .Customer ;
3+ import java .util .List ;
4+
45import org .springframework .beans .factory .annotation .Autowired ;
56import org .springframework .http .ResponseEntity ;
67import org .springframework .jdbc .core .JdbcTemplate ;
7- import org .springframework .web .bind .annotation .*;
8+ import org .springframework .web .bind .annotation .CrossOrigin ;
9+ import org .springframework .web .bind .annotation .DeleteMapping ;
10+ import org .springframework .web .bind .annotation .GetMapping ;
11+ import org .springframework .web .bind .annotation .PathVariable ;
12+ import org .springframework .web .bind .annotation .PostMapping ;
13+ import org .springframework .web .bind .annotation .RequestBody ;
14+ import org .springframework .web .bind .annotation .RequestMapping ;
15+ import org .springframework .web .bind .annotation .RequestParam ;
16+ import org .springframework .web .bind .annotation .RestController ;
817
9- import java .util .List ;
18+ import com .example .relationaldataaccess .Customer ;
19+
20+ import io .swagger .v3 .oas .annotations .Operation ;
21+ import io .swagger .v3 .oas .annotations .Parameter ;
22+ import io .swagger .v3 .oas .annotations .media .Content ;
23+ import io .swagger .v3 .oas .annotations .media .ExampleObject ;
24+ import io .swagger .v3 .oas .annotations .media .Schema ;
25+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
26+ import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
27+ import io .swagger .v3 .oas .annotations .tags .Tag ;
1028
1129@ RestController
1230@ RequestMapping ("/api/customers" )
1331@ CrossOrigin (origins = "${cors.allowed-origins:http://localhost:5173,http://localhost:5174}" )
32+ @ Tag (name = "Customer Management" , description = "REST API for managing customer data with full CRUD operations and search functionality" )
1433public class CustomerController {
1534
1635 @ Autowired
1736 private JdbcTemplate jdbcTemplate ;
1837
38+ @ Operation (
39+ summary = "Get all customers" ,
40+ description = "Retrieve a complete list of all customers in the system, ordered by ID"
41+ )
42+ @ ApiResponses (value = {
43+ @ ApiResponse (
44+ responseCode = "200" ,
45+ description = "Successfully retrieved customers" ,
46+ content = @ Content (
47+ mediaType = "application/json" ,
48+ schema = @ Schema (implementation = Customer .class ),
49+ examples = @ ExampleObject (
50+ name = "Customer list example" ,
51+ value = "[{\" id\" :1,\" firstName\" :\" John\" ,\" lastName\" :\" Doe\" },{\" id\" :2,\" firstName\" :\" Jane\" ,\" lastName\" :\" Smith\" }]"
52+ )
53+ )
54+ )
55+ })
1956 @ GetMapping
2057 public List <Customer > getAllCustomers () {
2158 return jdbcTemplate .query (
@@ -28,8 +65,32 @@ public List<Customer> getAllCustomers() {
2865 );
2966 }
3067
68+ @ Operation (
69+ summary = "Get customer by ID" ,
70+ description = "Retrieve a specific customer by their unique identifier"
71+ )
72+ @ ApiResponses (value = {
73+ @ ApiResponse (
74+ responseCode = "200" ,
75+ description = "Customer found" ,
76+ content = @ Content (
77+ mediaType = "application/json" ,
78+ schema = @ Schema (implementation = Customer .class ),
79+ examples = @ ExampleObject (
80+ name = "Customer example" ,
81+ value = "{\" id\" :1,\" firstName\" :\" John\" ,\" lastName\" :\" Doe\" }"
82+ )
83+ )
84+ ),
85+ @ ApiResponse (
86+ responseCode = "404" ,
87+ description = "Customer not found with the specified ID"
88+ )
89+ })
3190 @ GetMapping ("/{id}" )
32- public ResponseEntity <Customer > getCustomerById (@ PathVariable Long id ) {
91+ public ResponseEntity <Customer > getCustomerById (
92+ @ Parameter (description = "Unique identifier of the customer" , required = true , example = "1" )
93+ @ PathVariable Long id ) {
3394 List <Customer > customers = jdbcTemplate .query (
3495 "SELECT id, first_name, last_name FROM customers WHERE id = ?" ,
3596 (rs , rowNum ) -> new Customer (
@@ -47,8 +108,44 @@ public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
47108 return ResponseEntity .ok (customers .get (0 ));
48109 }
49110
111+ @ Operation (
112+ summary = "Create a new customer" ,
113+ description = "Create a new customer with the provided first name and last name. Names are automatically sanitized and validated for security."
114+ )
115+ @ ApiResponses (value = {
116+ @ ApiResponse (
117+ responseCode = "200" ,
118+ description = "Customer successfully created" ,
119+ content = @ Content (
120+ mediaType = "application/json" ,
121+ schema = @ Schema (implementation = Customer .class ),
122+ examples = @ ExampleObject (
123+ name = "Created customer example" ,
124+ value = "{\" id\" :3,\" firstName\" :\" John\" ,\" lastName\" :\" Doe\" }"
125+ )
126+ )
127+ ),
128+ @ ApiResponse (
129+ responseCode = "400" ,
130+ description = "Invalid customer data provided (empty names, null data, etc.)"
131+ )
132+ })
50133 @ PostMapping
51- public Customer createCustomer (@ RequestBody Customer customer ) {
134+ public Customer createCustomer (
135+ @ Parameter (description = "Customer data with firstName and lastName" , required = true )
136+ @ io .swagger .v3 .oas .annotations .parameters .RequestBody (
137+ description = "Customer object with first name and last name" ,
138+ required = true ,
139+ content = @ Content (
140+ mediaType = "application/json" ,
141+ schema = @ Schema (implementation = Customer .class ),
142+ examples = @ ExampleObject (
143+ name = "New customer example" ,
144+ value = "{\" firstName\" :\" John\" ,\" lastName\" :\" Doe\" }"
145+ )
146+ )
147+ )
148+ @ RequestBody Customer customer ) {
52149 // Input validation and sanitization
53150 if (customer == null ) {
54151 throw new IllegalArgumentException ("Customer data cannot be null" );
@@ -99,7 +196,28 @@ public Customer createCustomer(@RequestBody Customer customer) {
99196 }
100197
101198 @ DeleteMapping ("/{id}" )
102- public ResponseEntity <Void > deleteCustomer (@ PathVariable Long id ) {
199+ @ Operation (
200+ summary = "Delete a customer" ,
201+ description = "Deletes a customer by their unique ID. Returns 200 OK if the customer was successfully deleted, or 404 Not Found if no customer exists with the specified ID."
202+ )
203+ @ ApiResponses (value = {
204+ @ ApiResponse (
205+ responseCode = "200" ,
206+ description = "Customer successfully deleted"
207+ ),
208+ @ ApiResponse (
209+ responseCode = "404" ,
210+ description = "Customer not found with the specified ID" ,
211+ content = @ Content
212+ )
213+ })
214+ public ResponseEntity <Void > deleteCustomer (
215+ @ Parameter (
216+ description = "The unique identifier of the customer to delete" ,
217+ required = true ,
218+ example = "1"
219+ )
220+ @ PathVariable Long id ) {
103221 int rowsAffected = jdbcTemplate .update ("DELETE FROM customers WHERE id = ?" , id );
104222
105223 if (rowsAffected > 0 ) {
0 commit comments