Skip to content

Commit 76d3cb0

Browse files
committed
Correction Spring
2 parents 604fc35 + 6998047 commit 76d3cb0

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<java.version>15</java.version>
1919
</properties>
2020
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
</dependency>
2125
<dependency>
2226
<groupId>org.springframework.boot</groupId>
2327
<artifactId>spring-boot-starter-validation</artifactId>
@@ -30,7 +34,6 @@
3034
<groupId>org.springframework.boot</groupId>
3135
<artifactId>spring-boot-starter-web</artifactId>
3236
</dependency>
33-
3437
<dependency>
3538
<groupId>org.springframework.boot</groupId>
3639
<artifactId>spring-boot-devtools</artifactId>

src/main/java/com/itacademy/CrudEmpleats/CrudEmpleatsApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
56

6-
@SpringBootApplication
7+
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
78
public class CrudEmpleatsApplication {
89

910
public static void main(String[] args) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.springframework.web.bind.annotation.*;
1010

1111
import com.itacademy.CrudEmpleats.domain.Employee;
12-
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
12+
import com.itacademy.CrudEmpleats.service.EmployeeServices;
1313

1414
@RestController
1515
public class ControllerEmployees {
@@ -18,13 +18,14 @@ public class ControllerEmployees {
1818
*/
1919

2020
@Autowired
21-
private EmployeeRepository repositori;
21+
private EmployeeServices repositori;
2222

2323
// Crear nou empleat
2424
@PostMapping( "/EmployeeList")
2525
@ResponseStatus(HttpStatus.CREATED) // 201
26-
public void addEmployee(@Valid @RequestBody Employee employee) {
26+
public Employee addEmployee(@Valid @RequestBody Employee employee) {
2727
repositori.addEmployee(employee);
28+
return repositori.getEmployeeById(employee.getId());
2829

2930
}
3031

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.itacademy.CrudEmpleats.domain;
22

3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
35
import javax.validation.constraints.NotBlank;
46
import javax.validation.constraints.NotNull;
57

8+
@Entity
69
public class Employee {
710

11+
@Id
812
@NotNull
913
private int id;
1014
@NotBlank

src/main/java/com/itacademy/CrudEmpleats/exceptions/IdNotFound.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public class IdNotFound extends RuntimeException {
44

5+
private static final long serialVersionUID = 1L;
6+
57
private int id;
68

79
public IdNotFound(int id) {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package com.itacademy.CrudEmpleats.persistence;
22

3+
34
import java.util.*;
45

5-
import org.springframework.stereotype.Service;
6+
import org.springframework.stereotype.Repository;
67

78
import com.itacademy.CrudEmpleats.domain.Employee;
89
import com.itacademy.CrudEmpleats.exceptions.IdNotFound;
910

10-
@Service
11+
@Repository
1112
public class EmployeeRepository implements IEmployeeRepository{
1213

1314
// Els objectes persistents únicament en memòria
1415

1516
private static int idcount=0;
1617

17-
private List<Employee> repository = new ArrayList<>(
18+
private static List<Employee> repository = new ArrayList<>(
1819
Arrays.asList(
19-
new Employee(idcount++,"Joan", "Director_Projectes"),
20-
new Employee(idcount++,"Gerard", "Programador_Junior"),
21-
new Employee(idcount++,"Maria", "Administratiu")
20+
new Employee(idcount++,"Joan Gordi", "Director_Projectes"),
21+
new Employee(idcount++,"Gerard Puig", "Programador_Mid"),
22+
new Employee(idcount++,"Maria Camps", "Administratiu"),
23+
new Employee(idcount++,"Anaïs Iglesias", "Programador_Mid"),
24+
new Employee(idcount++,"Pere Masnou", "Administratiu"),
25+
new Employee(idcount++,"Laura Klaus", "Programador_Senior"),
26+
new Employee(idcount++,"Jhon Smith", "Administratiu"),
27+
new Employee(idcount++,"Ramón Dalmau", "Programador_Mid"),
28+
new Employee(idcount++,"Joana Gracia", "Programador_Junior"),
29+
new Employee(idcount++,"Quique Mota", "Administratiu"),
30+
new Employee(idcount++,"Kevin Lohan", "Director_Projectes")
2231
)
2332
);
2433

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.itacademy.CrudEmpleats.domain.Employee;
66

7+
78
public interface IEmployeeRepository {
89

910
public List<Employee> getAllEmployees();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.itacademy.CrudEmpleats.service;
2+
3+
import java.util.*;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import com.itacademy.CrudEmpleats.domain.Employee;
9+
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
10+
11+
@Service
12+
public class EmployeeServices {
13+
14+
@Autowired
15+
private EmployeeRepository repositori;
16+
17+
public List<Employee> getAllEmployees() {
18+
return repositori.getAllEmployees();
19+
}
20+
21+
public Employee getEmployeeById(int id){
22+
return repositori.getEmployeeById(id);
23+
}
24+
25+
public void addEmployee(Employee employee) {
26+
repositori.addEmployee(employee);
27+
}
28+
29+
public void updateEmployee(Employee employee, int id) {
30+
repositori.updateEmployee(employee, id);
31+
}
32+
33+
public void deleteEmployee(int id) {
34+
repositori.deleteEmployee(id);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)