Skip to content

Commit aef5fb0

Browse files
committed
1 parent bec2011 commit aef5fb0

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/main/java/guru/springframework/spring6restmvc/controller/CustomerController.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package guru.springframework.spring6restmvc.controller;
22

3-
import guru.springframework.spring6restmvc.model.Beer;
43
import guru.springframework.spring6restmvc.model.Customer;
54
import guru.springframework.spring6restmvc.service.CustomerService;
65
import lombok.AllArgsConstructor;
76
import lombok.extern.slf4j.Slf4j;
8-
import org.springframework.web.bind.annotation.PathVariable;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.RequestMethod;
11-
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.*;
1211

1312
import java.util.List;
1413
import java.util.UUID;
@@ -21,6 +20,16 @@ public class CustomerController {
2120

2221
private final CustomerService customerService;
2322

23+
@PostMapping
24+
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
25+
Customer savedCustomer = customerService.saveNewCustomer(customer);
26+
27+
HttpHeaders headers = new HttpHeaders();
28+
headers.add("Location", "/api/v1/customer/" + savedCustomer.getId().toString() );
29+
30+
return new ResponseEntity(headers, HttpStatus.CREATED);
31+
}
32+
2433
@RequestMapping(method = RequestMethod.GET)
2534
public List<Customer> listCustomers() {
2635
return customerService.getAllCustomers();

src/main/java/guru/springframework/spring6restmvc/service/CustomerService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
public interface CustomerService {
88
Customer getCustomerById(UUID id);
99
List<Customer> getAllCustomers();
10+
11+
Customer saveNewCustomer(Customer customer);
1012
}

src/main/java/guru/springframework/spring6restmvc/service/CustomerServiceImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,19 @@ public Customer getCustomerById(UUID id) {
5252
public List<Customer> getAllCustomers() {
5353
return new ArrayList<>(customerMap.values());
5454
}
55+
56+
@Override
57+
public Customer saveNewCustomer(Customer customer) {
58+
Customer savedCustomer = Customer.builder()
59+
.id(UUID.randomUUID())
60+
.version(1)
61+
.createdDate(LocalDateTime.now())
62+
.updateDate(LocalDateTime.now())
63+
.name(customer.getName())
64+
.build();
65+
66+
customerMap.put(savedCustomer.getId(), savedCustomer);
67+
68+
return savedCustomer;
69+
}
5570
}

0 commit comments

Comments
 (0)