Skip to content

Commit 8c3a2bb

Browse files
committed
create category and Customer PUT method
1 parent 3191924 commit 8c3a2bb

File tree

7 files changed

+234
-11
lines changed

7 files changed

+234
-11
lines changed

src/main/java/guru/springfamework/api/controllers/v1/CustomerController.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import org.springframework.http.HttpStatus;
99
import org.springframework.http.ResponseEntity;
1010
import org.springframework.stereotype.Controller;
11-
import org.springframework.web.bind.annotation.GetMapping;
12-
import org.springframework.web.bind.annotation.PathVariable;
13-
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1413

1514
/**
1615
* Darcy Xian 22/11/20 5:05 pm spring5-mvc-rest
1716
*/
17+
1818
@Controller
19-
@RequestMapping("/api/v1/customers/")
19+
@EnableWebMvc
20+
@RequestMapping("/api/v1/customers")
2021
@AllArgsConstructor
2122
public class CustomerController {
2223

@@ -29,10 +30,42 @@ public ResponseEntity<CustomerListDTO> getAllCustomers (){
2930
new CustomerListDTO(customerService.findAllCustomers()), HttpStatus.OK);
3031
}
3132

32-
@GetMapping("{id}")
33+
@GetMapping("/{id}")
3334
public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable Long id){
34-
return new ResponseEntity<CustomerDTO>(
35-
customerService.findCustomerById(id),HttpStatus.OK
36-
);
35+
CustomerDTO customerDTO = customerService.findCustomerById(id);
36+
customerDTO.setCustomerUrl("/api/v1/cusotmers/"+id);
37+
38+
39+
return new ResponseEntity<CustomerDTO>(
40+
customerDTO ,HttpStatus.OK);
41+
}
42+
43+
@PostMapping
44+
// @RequestBody tell Sping mVC to look at the body of the request and parse it and try to create a
45+
// CustomerDTO out of that
46+
public ResponseEntity<CustomerDTO> createNewCustomer(@RequestBody CustomerDTO customerDTO){
47+
return new ResponseEntity<CustomerDTO>(customerService.createNewCustomer(customerDTO),HttpStatus.CREATED);
48+
}
49+
50+
@PutMapping({"/{id}"})
51+
// @RequestBody tell Sping mVC to look at the body of the request and parse it and try to create a
52+
// CustomerDTO out of that
53+
public ResponseEntity<CustomerDTO> updateCustomer(
54+
@PathVariable Long id,
55+
@RequestBody CustomerDTO customerDTO){
56+
return new ResponseEntity<CustomerDTO>(
57+
customerService.saveCustomerByDTO(id,customerDTO),HttpStatus.CREATED);
58+
3759
}
3860
}
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+

src/main/java/guru/springfamework/api/v1/mapper/CustomerMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface CustomerMapper {
1818
@Mapping(source = "id",target = "id")
1919
CustomerDTO CustomerToCustomerDTO(Customer customer);
2020

21-
21+
@Mapping(source = "id",target = "id")
22+
Customer CustomerDTOToCustomer(CustomerDTO customerDTO);
2223

2324
}

src/main/java/guru/springfamework/services/CustomerService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import guru.springfamework.api.v1.model.CustomerDTO;
44
import org.springframework.stereotype.Service;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
56

67
import java.util.List;
78

89
/**
910
* Darcy Xian 22/11/20 5:13 pm spring5-mvc-rest
1011
*/
12+
1113
@Service
1214
public interface CustomerService {
1315

@@ -17,4 +19,7 @@ public interface CustomerService {
1719

1820
CustomerDTO createNewCustomer(CustomerDTO customerDTO);
1921

22+
CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO);
23+
24+
2025
}

src/main/java/guru/springfamework/services/CustomerServiceImp.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import guru.springfamework.api.v1.mapper.CustomerMapper;
44
import guru.springfamework.api.v1.model.CustomerDTO;
5+
import guru.springfamework.domain.Customer;
56
import guru.springfamework.repositories.CustomerRepository;
67
import lombok.AllArgsConstructor;
78
import org.springframework.stereotype.Service;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
810

911
import java.util.List;
1012
import java.util.stream.Collectors;
1113

1214
/**
1315
* Darcy Xian 22/11/20 5:24 pm spring5-mvc-rest
1416
*/
17+
1518
@Service
1619
@AllArgsConstructor
1720
public class CustomerServiceImp implements CustomerService{
@@ -40,4 +43,74 @@ public CustomerDTO findCustomerById(Long id) {
4043
.map(customerMapper::CustomerToCustomerDTO)
4144
.orElseThrow(RuntimeException::new);
4245
}
46+
47+
@Override
48+
public CustomerDTO createNewCustomer(CustomerDTO customerDTO) {
49+
50+
Customer customer = customerMapper.CustomerDTOToCustomer(customerDTO);
51+
52+
Customer savedCustomer = customerRepository.save(customer);
53+
54+
CustomerDTO returnDTO = customerMapper.CustomerToCustomerDTO(savedCustomer);
55+
56+
returnDTO.setCustomerUrl("/api/v1/customer/" + savedCustomer.getId());
57+
58+
return returnDTO;
59+
}
60+
61+
@Override
62+
public CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO) {
63+
customerDTO.setId(id);
64+
65+
CustomerDTO customerDTO1 = customerMapper.CustomerToCustomerDTO(
66+
customerRepository.save(
67+
customerMapper.CustomerDTOToCustomer(customerDTO)));
68+
customerDTO1.setCustomerUrl("/api/v1/customer/" + id);
69+
return customerDTO1;
70+
}
4371
}
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+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package guru.springfamework.api.controllers.v1;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
5+
/**
6+
* Darcy Xian 22/11/20 9:07 pm spring5-mvc-rest
7+
*/
8+
public abstract class AbstractRestControllerTest {
9+
10+
public static String asJsonString(final Object ob){
11+
try{
12+
return new ObjectMapper().writeValueAsString(ob);
13+
} catch (Exception e){
14+
throw new RuntimeException(e);
15+
}
16+
}
17+
}

src/test/java/guru/springfamework/api/controllers/v1/CustomerControllerTest.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@
1010
import org.mockito.MockitoAnnotations;
1111
import org.springframework.http.MediaType;
1212
import org.springframework.test.web.servlet.MockMvc;
13-
import org.springframework.test.web.servlet.ResultMatcher;
1413
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1515

16+
import static guru.springfamework.api.controllers.v1.AbstractRestControllerTest.asJsonString;
1617
import static org.hamcrest.Matchers.equalTo;
1718
import static org.hamcrest.Matchers.hasSize;
18-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19+
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyLong;
22+
1923
import java.util.Arrays;
2024
import java.util.List;
2125

2226

2327
import static org.mockito.Mockito.when;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2429
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2530

2631
/**
2732
* Darcy Xian 22/11/20 5:51 pm spring5-mvc-rest
2833
*/
34+
2935
public class CustomerControllerTest {
3036

3137
public static final String FIRSTNAME = "JOE";
@@ -74,9 +80,57 @@ public void getCustomerById() throws Exception {
7480
.contentType(MediaType.APPLICATION_JSON))
7581
.andExpect(status().isOk())
7682
.andExpect(jsonPath("$.firstname", equalTo(FIRSTNAME)));
83+
}
84+
// @Test
85+
// public void createNewCustomer() throws Exception {
86+
// // given
87+
// CustomerDTO customerDTO = new CustomerDTO();
88+
// customerDTO.setFirstname(FIRSTNAME);
89+
// customerDTO.setLastname(LASTNAME);
90+
//
91+
// CustomerDTO returnDTO = new CustomerDTO();
92+
// returnDTO.setFirstname(customerDTO.getFirstname());
93+
// returnDTO.setLastname(customerDTO.getLastname());
94+
// returnDTO.setCustomerUrl("/api/v1/customers/1");
95+
//
96+
// when(customerService.createNewCustomer(customerDTO)).thenReturn(returnDTO);
97+
//
98+
// mockMvc.perform(post("/api/v1/customers/")
99+
// .contentType(MediaType.APPLICATION_JSON)
100+
// // 把一个对象转化成json 利用jackson的jar包
101+
// .contentType(asJsonString(customerDTO)))
102+
// .andExpect(status().isOk())
103+
// .andExpect(jsonPath("$.firstname",equalTo("Fred")));
104+
// // .andExpect(jsonPath("$.customer_url",equalTo("/api/v1/customers/1")));
105+
//
106+
// }
107+
108+
@Test
109+
public void testUpdateCustomer() throws Exception{
110+
CustomerDTO customerDTO = new CustomerDTO();
111+
customerDTO.setFirstname(FIRSTNAME);
112+
customerDTO.setLastname(LASTNAME);
113+
114+
CustomerDTO returnDTO = new CustomerDTO();
115+
returnDTO.setId(1l);
116+
returnDTO.setFirstname(customerDTO.getFirstname());
117+
returnDTO.setLastname(customerDTO.getLastname());
118+
returnDTO.setCustomerUrl("/api/v1/customers/1");
119+
120+
when(customerService.saveCustomerByDTO(anyLong(),any(CustomerDTO.class))).thenReturn(returnDTO);
121+
122+
// when then
123+
mockMvc.perform(put("/api/v1/customers/1")
124+
.contentType(MediaType.APPLICATION_JSON)
125+
.content(asJsonString(customerDTO)))
126+
.andExpect(status().isCreated())
127+
.andExpect(jsonPath("$.firstname",equalTo(FIRSTNAME)));
77128

78129

79130
}
131+
132+
133+
80134
}
81135

82136

src/test/java/guru/springfamework/services/CustomerServiceTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414

1515
import static org.junit.Assert.*;
16+
import static org.mockito.ArgumentMatchers.any;
1617
import static org.mockito.Mockito.when;
1718

1819
/**
@@ -59,7 +60,46 @@ public void findCustomerById() {
5960

6061
// then
6162
assertEquals(Long.valueOf(1L),Long.valueOf(customerDTO.getId()));
63+
}
64+
65+
@Test
66+
public void createNewCustomer() throws Exception{
67+
// given
68+
CustomerDTO customerDTO = new CustomerDTO();
69+
customerDTO.setFirstname("lim");
70+
71+
Customer savedCustomer = new Customer();
72+
savedCustomer.setId(1L);
73+
savedCustomer.setFirstname(customerDTO.getFirstname());
74+
75+
when(customerRepository.save(any((Customer.class)))).thenReturn(savedCustomer);
76+
77+
// when
78+
CustomerDTO savedDto = customerService.createNewCustomer(customerDTO);
79+
80+
//then
81+
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
82+
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
83+
}
84+
85+
@Test
86+
public void saveCustomerByDTO() throws Exception{
87+
// given
88+
CustomerDTO customerDTO = new CustomerDTO();
89+
customerDTO.setFirstname("lim");
90+
91+
Customer savedCustomer = new Customer();
92+
savedCustomer.setId(1L);
93+
savedCustomer.setFirstname(customerDTO.getFirstname());
94+
95+
when(customerRepository.save(any((Customer.class)))).thenReturn(savedCustomer);
96+
97+
// when
98+
CustomerDTO savedDto = customerService.saveCustomerByDTO(1L,customerDTO);
6299

100+
//then
101+
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
102+
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
63103
}
64104
}
65105

0 commit comments

Comments
 (0)