Skip to content

Commit b9564c0

Browse files
committed
Assignment springframeworkguru#5 Customer
1 parent 8fad4cb commit b9564c0

File tree

6 files changed

+127
-3
lines changed

6 files changed

+127
-3
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<artifactId>lombok</artifactId>
2828
<optional>true</optional>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-devtools</artifactId>
33+
</dependency>
3034
<dependency>
3135
<groupId>org.springframework.boot</groupId>
3236
<artifactId>spring-boot-starter-test</artifactId>

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import guru.springframework.spring6restmvc.service.BeerService;
55
import lombok.AllArgsConstructor;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.web.bind.annotation.PathVariable;
78
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
810
import org.springframework.web.bind.annotation.RestController;
911

1012
import java.util.List;
@@ -13,18 +15,20 @@
1315
@Slf4j
1416
@AllArgsConstructor
1517
@RestController
18+
@RequestMapping("/api/v1/beer")
1619
public class BeerController {
1720

1821
private final BeerService beerService;
1922

20-
@RequestMapping("/api/v1/beer")
23+
@RequestMapping(method = RequestMethod.GET)
2124
public List<Beer> listBeers() {
2225
return beerService.listBeers();
2326
}
2427

25-
public Beer getBeerById(UUID id) {
28+
@RequestMapping(value = "{beerId}", method = RequestMethod.GET)
29+
public Beer getBeerById(@PathVariable("beerId") UUID beerId) {
2630
log.debug("getBeerById() called in Controller");
27-
return beerService.getBeerById(id);
31+
return beerService.getBeerById(beerId);
2832
}
2933

3034
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package guru.springframework.spring6restmvc.controller;
2+
3+
import guru.springframework.spring6restmvc.model.Beer;
4+
import guru.springframework.spring6restmvc.model.Customer;
5+
import guru.springframework.spring6restmvc.service.CustomerService;
6+
import lombok.AllArgsConstructor;
7+
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;
12+
13+
import java.util.List;
14+
import java.util.UUID;
15+
16+
@Slf4j
17+
@AllArgsConstructor
18+
@RestController
19+
@RequestMapping("/api/v1/customer")
20+
public class CustomerController {
21+
22+
private final CustomerService customerService;
23+
24+
@RequestMapping(method = RequestMethod.GET)
25+
public List<Customer> listCustomers() {
26+
return customerService.getAllCustomers();
27+
}
28+
29+
@RequestMapping(value = "{customerId}", method = RequestMethod.GET)
30+
public Customer getCustomerById(@PathVariable("customerId") UUID customerId) {
31+
return customerService.getCustomerById(customerId);
32+
}
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package guru.springframework.spring6restmvc.model;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.UUID;
8+
9+
@Data
10+
@Builder
11+
public class Customer {
12+
private UUID id;
13+
private Integer version;
14+
private String name;
15+
private LocalDateTime createdDate;
16+
private LocalDateTime updateDate;
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package guru.springframework.spring6restmvc.service;
2+
3+
import guru.springframework.spring6restmvc.model.Customer;
4+
5+
import java.util.List;
6+
import java.util.UUID;
7+
public interface CustomerService {
8+
Customer getCustomerById(UUID id);
9+
List<Customer> getAllCustomers();
10+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package guru.springframework.spring6restmvc.service;
2+
3+
import guru.springframework.spring6restmvc.model.Customer;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.time.LocalDateTime;
7+
import java.util.*;
8+
9+
@Service
10+
public class CustomerServiceImpl implements CustomerService {
11+
12+
private Map<UUID, Customer> customerMap;
13+
14+
public CustomerServiceImpl() {
15+
customerMap = new HashMap<>();
16+
17+
Customer customer1 = Customer.builder()
18+
.id(UUID.randomUUID())
19+
.version(1)
20+
.name("Customer 1")
21+
.createdDate(LocalDateTime.now())
22+
.updateDate(LocalDateTime.now())
23+
.build();
24+
25+
Customer customer2 = Customer.builder()
26+
.id(UUID.randomUUID())
27+
.version(1)
28+
.name("Customer 2")
29+
.createdDate(LocalDateTime.now())
30+
.updateDate(LocalDateTime.now())
31+
.build();
32+
33+
Customer customer3 = Customer.builder()
34+
.id(UUID.randomUUID())
35+
.version(1)
36+
.name("Customer 3")
37+
.createdDate(LocalDateTime.now())
38+
.updateDate(LocalDateTime.now())
39+
.build();
40+
41+
customerMap.put(customer1.getId(), customer1);
42+
customerMap.put(customer2.getId(), customer2);
43+
customerMap.put(customer3.getId(), customer3);
44+
}
45+
46+
@Override
47+
public Customer getCustomerById(UUID id) {
48+
return customerMap.get(id);
49+
}
50+
51+
@Override
52+
public List<Customer> getAllCustomers() {
53+
return new ArrayList<>(customerMap.values());
54+
}
55+
}

0 commit comments

Comments
 (0)