|
2 | 2 |
|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.util.List;
|
| 5 | +import java.util.NoSuchElementException; |
5 | 6 |
|
6 | 7 | import javax.servlet.http.HttpServletResponse;
|
7 | 8 |
|
8 | 9 | import org.springframework.beans.factory.annotation.Autowired;
|
| 10 | +import org.springframework.http.HttpStatus; |
9 | 11 | import org.springframework.web.bind.annotation.GetMapping;
|
| 12 | +import org.springframework.web.bind.annotation.PathVariable; |
| 13 | +import org.springframework.web.bind.annotation.ResponseStatus; |
10 | 14 | import org.springframework.web.bind.annotation.RestController;
|
| 15 | +import org.springframework.web.server.ResponseStatusException; |
11 | 16 |
|
12 | 17 | import com.itacademy.CrudEmpleats.domain.Employee;
|
13 | 18 | import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
|
@@ -36,16 +41,25 @@ public class ControllerEmployees {
|
36 | 41 |
|
37 | 42 | //Temporalment redirigim a la llista d'empleats
|
38 | 43 | @GetMapping("/")
|
39 |
| - void start(HttpServletResponse reponse) throws IOException{ |
40 |
| - reponse.sendRedirect("/Empleat");; |
| 44 | + void start(HttpServletResponse reponse) throws IOException { |
| 45 | + reponse.sendRedirect("/Empleat"); |
41 | 46 | }
|
42 |
| - |
43 |
| - //Retorna una llista JSon d'empleats |
| 47 | + |
| 48 | + // Retorna una llista JSon d'empleats |
44 | 49 | @GetMapping("/Empleat")
|
45 |
| - public List<Employee> allEmployees(){ |
| 50 | + public List<Employee> allEmployees() { |
46 | 51 | return repositori.getAllEmployees();
|
47 | 52 | }
|
48 |
| - |
49 |
| - |
| 53 | + |
| 54 | + // Busca un empleat per Id |
| 55 | + @GetMapping("/Empleat/{id}") |
| 56 | + public Employee getEmployee(@PathVariable("id") int id) { |
| 57 | + try { |
| 58 | + return repositori.getEmployeeById(id); |
| 59 | + } catch (NoSuchElementException e) { |
| 60 | + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No s'ha trobat cap empleat amb el id =" + id); |
| 61 | + } |
| 62 | + |
| 63 | + } |
50 | 64 |
|
51 | 65 | }
|
0 commit comments