Skip to content

Commit 02117f9

Browse files
committed
Implement get customer by id method in CustomerController
1 parent 69a78fe commit 02117f9

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
package com.hsbc.simpleapi.controller;
22

3+
import javax.persistence.EntityNotFoundException;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.server.ResponseStatusException;
15+
16+
import com.hsbc.simpleapi.model.Customer;
17+
import com.hsbc.simpleapi.service.CustomerService;
18+
19+
@RestController
20+
@RequestMapping("/api/customer")
321
public class CustomerController {
4-
}
22+
23+
private CustomerService customerService;
24+
25+
@Autowired
26+
public CustomerController(CustomerService customerService) {
27+
this.customerService = customerService;
28+
}
29+
30+
@GetMapping("{id}")
31+
public ResponseEntity<Customer> getCustomerById(@PathVariable("id") long id) {
32+
try {
33+
return new ResponseEntity<>(customerService.getCustomerBy(id), HttpStatus.OK);
34+
} catch (EntityNotFoundException e) {
35+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)