Skip to content

Commit 7bb01de

Browse files
committed
Create new Employee Implemented
1 parent 8941856 commit 7bb01de

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.http.HttpStatus;
11-
import org.springframework.web.bind.annotation.GetMapping;
12-
import org.springframework.web.bind.annotation.PathVariable;
13-
import org.springframework.web.bind.annotation.ResponseStatus;
14-
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.bind.annotation.*;
1512
import org.springframework.web.server.ResponseStatusException;
1613

1714
import com.itacademy.CrudEmpleats.domain.Employee;
@@ -38,13 +35,20 @@ public class ControllerEmployees {
3835

3936
@Autowired
4037
private EmployeeRepository repositori;
38+
4139

42-
//Temporalment redirigim a la llista d'empleats
40+
// Temporalment redirigim a la llista d'empleats
4341
@GetMapping("/")
4442
void start(HttpServletResponse reponse) throws IOException {
4543
reponse.sendRedirect("/Empleat");
4644
}
47-
45+
46+
// Crear nou empleat
47+
@PostMapping( "/Empleat")
48+
public void addEmployee(@RequestBody Employee employee) {
49+
repositori.addEmployee(employee);
50+
}
51+
4852
// Retorna una llista JSon d'empleats
4953
@GetMapping("/Empleat")
5054
public List<Employee> allEmployees() {
@@ -53,13 +57,16 @@ public List<Employee> allEmployees() {
5357

5458
// Busca un empleat per Id
5559
@GetMapping("/Empleat/{id}")
56-
public Employee getEmployee(@PathVariable("id") int id) {
60+
public Employee getFirstEmployee(@PathVariable("id") int id) {
5761
try {
5862
return repositori.getEmployeeById(id);
5963
} catch (NoSuchElementException e) {
6064
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No s'ha trobat cap empleat amb el id =" + id);
6165
}
62-
6366
}
67+
68+
69+
6470

71+
6572
}

src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public class Employee {
1515
private String name;
1616
private String job;
1717

18-
public Employee() {
19-
}
18+
2019

2120
public Employee(String name, String job) {
2221
this.id=idCount++;

src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public Employee getEmployeeById(int id) throws NoSuchElementException{
3030
return repository.stream().filter(employee -> employee.getId()==id).findFirst().get();
3131
}
3232

33+
public void addEmployee(Employee employee) {
34+
repository.add(employee);
35+
36+
}
37+
3338
}

0 commit comments

Comments
 (0)