Skip to content

Commit 69a6eb5

Browse files
authored
JS Functions implemented
2 parents 6499f8a + 604fc35 commit 69a6eb5

File tree

13 files changed

+223
-85
lines changed

13 files changed

+223
-85
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
67
<groupId>org.springframework.boot</groupId>
78
<artifactId>spring-boot-starter-parent</artifactId>
89
<version>2.4.3</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10+
<relativePath /> <!-- lookup parent from repository -->
1011
</parent>
1112
<groupId>com.itacademy</groupId>
1213
<artifactId>CrudEmpleats</artifactId>
@@ -17,6 +18,10 @@
1718
<java.version>15</java.version>
1819
</properties>
1920
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-validation</artifactId>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.springframework.boot</groupId>
2227
<artifactId>spring-boot-starter-thymeleaf</artifactId>

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.itacademy.CrudEmpleats.controller;
22

3-
import java.io.IOException;
43
import java.util.List;
54

6-
import javax.servlet.http.HttpServletResponse;
5+
import javax.validation.Valid;
76

87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.http.HttpStatus;
@@ -20,44 +19,37 @@ public class ControllerEmployees {
2019

2120
@Autowired
2221
private EmployeeRepository repositori;
23-
24-
25-
// Temporalment redirigim a la llista d'empleats
26-
@GetMapping("/")
27-
void start(HttpServletResponse reponse) throws IOException {
28-
reponse.sendRedirect("/Empleat");
29-
}
3022

3123
// Crear nou empleat
32-
@PostMapping( "/Empleat")
24+
@PostMapping( "/EmployeeList")
3325
@ResponseStatus(HttpStatus.CREATED) // 201
34-
public Employee addEmployee(@RequestBody Employee employee) {
26+
public void addEmployee(@Valid @RequestBody Employee employee) {
3527
repositori.addEmployee(employee);
36-
return employee;
28+
3729
}
3830

3931
// Retorna una llista JSon d'empleats
40-
@GetMapping("/Empleat")
32+
@GetMapping("/EmployeeList")
4133
public List<Employee> allEmployees() {
4234
return repositori.getAllEmployees();
4335
}
4436

4537
// Busca un empleat per Id
46-
@GetMapping("/Empleat/{id}")
38+
@GetMapping("/EmployeeList/{id}")
4739
public Employee getFirstEmployee(@PathVariable("id") int id) {
4840
return repositori.getEmployeeById(id);
4941
}
5042

5143
// Actualitzar Empleat
52-
@PutMapping("/Empleat/{id}")
44+
@PutMapping("/EmployeeList/{id}")
5345
@ResponseStatus(HttpStatus.ACCEPTED) // 202
54-
public Employee updateEmployee(@RequestBody Employee employee, @PathVariable("id") int id) {
46+
public Employee updateEmployee(@Valid @RequestBody Employee employee, @PathVariable("id") int id) {
5547
repositori.updateEmployee(employee,id);
5648
return repositori.getEmployeeById(id);
5749
}
5850

5951
// Eliminar Empleat
60-
@DeleteMapping("/Empleat/{id}")
52+
@DeleteMapping("/EmployeeList/{id}")
6153
@ResponseStatus(HttpStatus.ACCEPTED) // 202
6254
public String deleteEmployee(@PathVariable("id") int id) {
6355
repositori.deleteEmployee(id);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.itacademy.CrudEmpleats.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class ControllerWeb {
8+
/*
9+
* Controlador d'accés html
10+
*/
11+
12+
13+
@GetMapping("/index")
14+
public String goIndex() {
15+
return "index";
16+
}
17+
18+
@GetMapping("/ModificarEmpleat")
19+
public String bodyHeader() {
20+
return "ModificarEmpleat";
21+
}
22+
23+
}

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

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

3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotNull;
5+
36
public class Employee {
47

8+
@NotNull
59
private int id;
10+
@NotBlank
611
private String name;
12+
@NotBlank
713
private String job;
814
private double salary ;
915

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public enum Jobs {
88
Programador_Senior("Programador Senior",35000),
99
Programador_Mid("Programador Mid-Level",29000),
1010
Programador_Junior("Programador Junior",21000),
11-
Administrativa("Administrativa",25000);
11+
Administratiu("Administratiu",25000);
1212

1313
private String name;
1414
private double salary;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,12 @@ public class EmployeeRepository implements IEmployeeRepository{
1313
// Els objectes persistents únicament en memòria
1414

1515
private static int idcount=0;
16-
/*
17-
private List<Employee> repository = new ArrayList<>(
18-
Arrays.asList(
19-
new Employee("Joan", "Director_Projectes"),
20-
new Employee("Gerard", "Programador_Junior"),
21-
new Employee("Maria", "Administrativa")
22-
)
23-
);
24-
*/
16+
2517
private List<Employee> repository = new ArrayList<>(
2618
Arrays.asList(
2719
new Employee(idcount++,"Joan", "Director_Projectes"),
2820
new Employee(idcount++,"Gerard", "Programador_Junior"),
29-
new Employee(idcount++,"Maria", "Administrativa")
21+
new Employee(idcount++,"Maria", "Administratiu")
3022
)
3123
);
3224

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

src/main/resources/static/index.css

Whitespace-only changes.

src/main/resources/static/index.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
$(document).ready(updateTable());
3+
4+
//Read employee List & Update the table
5+
function updateTable() {
6+
$.get("/EmployeeList", function (data) {
7+
$('#employeeTable > tbody').empty();
8+
var table_data;
9+
$.each(data, function (i, employee) {
10+
table_data += "<tr>" +
11+
"<td>" + employee.id + "</td>" +
12+
"<td>" + employee.name + "</td>" +
13+
"<td>" + employee.job + "</td>" +
14+
"<td>" + employee.salary + "</td>" +
15+
"<td>" +
16+
"<button type='button'onclick='editEmployee(" + employee.id + ")'>Modificar</button>" +
17+
"<button type='button' onclick='deleteEmployee(" + employee.id + ")'>Eliminar</button>" +
18+
"</td>"
19+
});
20+
$('#employeeTable > tbody').append(table_data);
21+
});
22+
};
23+
24+
//Create & update
25+
function sendEmployee() {
26+
//create employee
27+
if ($('#action').val() == 'Afegir') {
28+
$.ajax({
29+
url: '/EmployeeList',
30+
type: 'POST',
31+
contentType: 'application/Json',
32+
data: JSON.stringify({
33+
"name": $('#name').val(),
34+
"job": $('#job').val()
35+
}),
36+
success: function (data, textStatus, jQxhr) {
37+
updateTable();
38+
},
39+
error: function (xhr, textStatus, errorThrown) {
40+
console.log(textStatus);
41+
}
42+
});
43+
//Update employee
44+
} if ($('#action').val()=='Actualitzar') {
45+
$.ajax({
46+
url: '/EmployeeList/' + $('#idEmployee').val(),
47+
type: 'PUT',
48+
contentType: 'application/Json',
49+
data: JSON.stringify({
50+
"id": $('#idEmployee').val(),
51+
"name": $('#name').val(),
52+
"job": $('#job').val()
53+
}),
54+
success: function (data, textStatus, jQxhr) {
55+
updateTable();
56+
},
57+
error: function (xhr, textStatus, errorThrown) {
58+
console.log(textStatus);
59+
}
60+
});
61+
}
62+
$('#EmployeeForm').trigger('reset');
63+
$('#action').val('Afegir')
64+
};
65+
66+
//Delete employee
67+
function deleteEmployee(id) {
68+
if (confirm("Desitja eliminar a l'Empleat?")) {
69+
$.ajax({
70+
type: "DELETE",
71+
url: "/EmployeeList/" + id,
72+
success: function (data, textStatus, jQxhr) {
73+
updateTable();
74+
},
75+
error: function (xhr, textStatus, errorThrown) {
76+
console.log(textStatus);
77+
}
78+
});
79+
};
80+
};
81+
82+
//Read one employee info and put in the modifier form.
83+
function editEmployee(id) {
84+
$.get("/EmployeeList/" + id, function (data) {
85+
$('#idEmployee').val(data.id);
86+
$('#name').val(data.name);
87+
$('#job').val(getJobCode(data.job));
88+
});
89+
$('#action').val('Actualitzar');
90+
};
91+
92+
//Take the enum codes for Employee Jobs.
93+
function getJobCode(job) {
94+
if (job == 'Director de projectes') {
95+
return 'Director_Projectes';
96+
} if (job == 'Programador Senior') {
97+
return 'Programador_Senior';
98+
} if (job == 'Programador_Mid') {
99+
return 'Programador_Mid';
100+
} if (job == 'Programador Junior') {
101+
return 'Programador_Junior';
102+
} if (job == 'Administratiu') {
103+
return 'Administratiu';
104+
} else {
105+
return "Default";
106+
}
107+
}

src/main/resources/templates/Fragments/BodyHeader.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/resources/templates/Fragments/head.html

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/resources/templates/ModificarEmpleat.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)