-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCustomerController.java
More file actions
57 lines (48 loc) · 1.81 KB
/
CustomerController.java
File metadata and controls
57 lines (48 loc) · 1.81 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
package com.ironhack.lab_springboot_api.controller;
import com.ironhack.lab_springboot_api.model.Customer;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/customers")
public class CustomerController {
private List<Customer> customers = new ArrayList<>();
@PostMapping
public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer customer) {
customers.add(customer);
return new ResponseEntity<>(customer, HttpStatus.CREATED);
}
@GetMapping
public List<Customer> getAll() {
return customers;
}
@GetMapping("/{email}")
public ResponseEntity<Customer> getByEmail(@PathVariable String email) {
for (Customer c : customers) {
if (c.getEmail().equalsIgnoreCase(email)) {
return ResponseEntity.ok(c);
}
}
return ResponseEntity.notFound().build();
}
@PutMapping("/{email}")
public ResponseEntity<Customer> updateCustomer(@PathVariable String email, @Valid @RequestBody Customer updated) {
for (Customer c : customers) {
if (c.getEmail().equalsIgnoreCase(email)) {
c.setName(updated.getName());
c.setAge(updated.getAge());
c.setAddress(updated.getAddress());
return ResponseEntity.ok(c);
}
}
return ResponseEntity.notFound().build();
}
@DeleteMapping("/{email}")
public ResponseEntity<Void> deleteCustomer(@PathVariable String email) {
customers.removeIf(c -> c.getEmail().equalsIgnoreCase(email));
return ResponseEntity.noContent().build();
}
}