diff --git a/pom.xml b/pom.xml index bf17321..52ade09 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,13 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.3 - + com.itacademy CrudEmpleats @@ -17,6 +18,10 @@ 15 + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java b/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java index a7447f2..80d04ff 100644 --- a/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java +++ b/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java @@ -1,9 +1,8 @@ package com.itacademy.CrudEmpleats.controller; -import java.io.IOException; import java.util.List; -import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -20,44 +19,37 @@ public class ControllerEmployees { @Autowired private EmployeeRepository repositori; - - - // Temporalment redirigim a la llista d'empleats - @GetMapping("/") - void start(HttpServletResponse reponse) throws IOException { - reponse.sendRedirect("/Empleat"); - } // Crear nou empleat - @PostMapping( "/Empleat") + @PostMapping( "/EmployeeList") @ResponseStatus(HttpStatus.CREATED) // 201 - public Employee addEmployee(@RequestBody Employee employee) { + public void addEmployee(@Valid @RequestBody Employee employee) { repositori.addEmployee(employee); - return employee; + } // Retorna una llista JSon d'empleats - @GetMapping("/Empleat") + @GetMapping("/EmployeeList") public List allEmployees() { return repositori.getAllEmployees(); } // Busca un empleat per Id - @GetMapping("/Empleat/{id}") + @GetMapping("/EmployeeList/{id}") public Employee getFirstEmployee(@PathVariable("id") int id) { return repositori.getEmployeeById(id); } // Actualitzar Empleat - @PutMapping("/Empleat/{id}") + @PutMapping("/EmployeeList/{id}") @ResponseStatus(HttpStatus.ACCEPTED) // 202 - public Employee updateEmployee(@RequestBody Employee employee, @PathVariable("id") int id) { + public Employee updateEmployee(@Valid @RequestBody Employee employee, @PathVariable("id") int id) { repositori.updateEmployee(employee,id); return repositori.getEmployeeById(id); } // Eliminar Empleat - @DeleteMapping("/Empleat/{id}") + @DeleteMapping("/EmployeeList/{id}") @ResponseStatus(HttpStatus.ACCEPTED) // 202 public String deleteEmployee(@PathVariable("id") int id) { repositori.deleteEmployee(id); diff --git a/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerWeb.java b/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerWeb.java new file mode 100644 index 0000000..261ba7d --- /dev/null +++ b/src/main/java/com/itacademy/CrudEmpleats/controller/ControllerWeb.java @@ -0,0 +1,23 @@ +package com.itacademy.CrudEmpleats.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class ControllerWeb { + /* + * Controlador d'accés html + */ + + + @GetMapping("/index") + public String goIndex() { + return "index"; + } + + @GetMapping("/ModificarEmpleat") + public String bodyHeader() { + return "ModificarEmpleat"; + } + +} diff --git a/src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java b/src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java index f345441..04e8718 100644 --- a/src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java +++ b/src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java @@ -1,9 +1,15 @@ package com.itacademy.CrudEmpleats.domain; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + public class Employee { + @NotNull private int id; + @NotBlank private String name; + @NotBlank private String job; private double salary ; diff --git a/src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java b/src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java index b89cdce..01ce1d5 100644 --- a/src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java +++ b/src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java @@ -8,7 +8,7 @@ public enum Jobs { Programador_Senior("Programador Senior",35000), Programador_Mid("Programador Mid-Level",29000), Programador_Junior("Programador Junior",21000), - Administrativa("Administrativa",25000); + Administratiu("Administratiu",25000); private String name; private double salary; diff --git a/src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java b/src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java index a646adc..7ee7057 100644 --- a/src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java +++ b/src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java @@ -13,20 +13,12 @@ public class EmployeeRepository implements IEmployeeRepository{ // Els objectes persistents únicament en memòria private static int idcount=0; - /* - private List repository = new ArrayList<>( - Arrays.asList( - new Employee("Joan", "Director_Projectes"), - new Employee("Gerard", "Programador_Junior"), - new Employee("Maria", "Administrativa") - ) - ); - */ + private List repository = new ArrayList<>( Arrays.asList( new Employee(idcount++,"Joan", "Director_Projectes"), new Employee(idcount++,"Gerard", "Programador_Junior"), - new Employee(idcount++,"Maria", "Administrativa") + new Employee(idcount++,"Maria", "Administratiu") ) ); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..e69de29 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +0,0 @@ - diff --git a/src/main/resources/static/index.css b/src/main/resources/static/index.css new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/static/index.js b/src/main/resources/static/index.js new file mode 100644 index 0000000..0b19143 --- /dev/null +++ b/src/main/resources/static/index.js @@ -0,0 +1,107 @@ + +$(document).ready(updateTable()); + +//Read employee List & Update the table +function updateTable() { + $.get("/EmployeeList", function (data) { + $('#employeeTable > tbody').empty(); + var table_data; + $.each(data, function (i, employee) { + table_data += "" + + "" + employee.id + "" + + "" + employee.name + "" + + "" + employee.job + "" + + "" + employee.salary + "" + + "" + + "" + + "" + + "" + }); + $('#employeeTable > tbody').append(table_data); + }); +}; + +//Create & update +function sendEmployee() { + //create employee + if ($('#action').val() == 'Afegir') { + $.ajax({ + url: '/EmployeeList', + type: 'POST', + contentType: 'application/Json', + data: JSON.stringify({ + "name": $('#name').val(), + "job": $('#job').val() + }), + success: function (data, textStatus, jQxhr) { + updateTable(); + }, + error: function (xhr, textStatus, errorThrown) { + console.log(textStatus); + } + }); + //Update employee + } if ($('#action').val()=='Actualitzar') { + $.ajax({ + url: '/EmployeeList/' + $('#idEmployee').val(), + type: 'PUT', + contentType: 'application/Json', + data: JSON.stringify({ + "id": $('#idEmployee').val(), + "name": $('#name').val(), + "job": $('#job').val() + }), + success: function (data, textStatus, jQxhr) { + updateTable(); + }, + error: function (xhr, textStatus, errorThrown) { + console.log(textStatus); + } + }); + } + $('#EmployeeForm').trigger('reset'); + $('#action').val('Afegir') +}; + +//Delete employee +function deleteEmployee(id) { + if (confirm("Desitja eliminar a l'Empleat?")) { + $.ajax({ + type: "DELETE", + url: "/EmployeeList/" + id, + success: function (data, textStatus, jQxhr) { + updateTable(); + }, + error: function (xhr, textStatus, errorThrown) { + console.log(textStatus); + } + }); + }; +}; + +//Read one employee info and put in the modifier form. +function editEmployee(id) { + $.get("/EmployeeList/" + id, function (data) { + $('#idEmployee').val(data.id); + $('#name').val(data.name); + $('#job').val(getJobCode(data.job)); + }); + $('#action').val('Actualitzar'); +}; + +//Take the enum codes for Employee Jobs. +function getJobCode(job) { + if (job == 'Director de projectes') { + return 'Director_Projectes'; + } if (job == 'Programador Senior') { + return 'Programador_Senior'; + } if (job == 'Programador_Mid') { + return 'Programador_Mid'; + } if (job == 'Programador Junior') { + return 'Programador_Junior'; + } if (job == 'Administratiu') { + return 'Administratiu'; + } else { + return "Default"; + } +} \ No newline at end of file diff --git a/src/main/resources/templates/Fragments/BodyHeader.html b/src/main/resources/templates/Fragments/BodyHeader.html deleted file mode 100755 index 4523c23..0000000 --- a/src/main/resources/templates/Fragments/BodyHeader.html +++ /dev/null @@ -1,5 +0,0 @@ -
-
-

Spring - CRUD de Empleats

-
-
\ No newline at end of file diff --git a/src/main/resources/templates/Fragments/head.html b/src/main/resources/templates/Fragments/head.html deleted file mode 100755 index 606f40f..0000000 --- a/src/main/resources/templates/Fragments/head.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - -Spring - Crud delegacions - diff --git a/src/main/resources/templates/ModificarEmpleat.html b/src/main/resources/templates/ModificarEmpleat.html deleted file mode 100644 index 935ff1c..0000000 --- a/src/main/resources/templates/ModificarEmpleat.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - -
-
- - -

- -
- Nom:

- Cognom:

- - - - -
-
- - \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..18ccc9d --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,68 @@ + + + + + + Spring - Crud Empleats + + + + + + + +
+
+

Empleats

+
+
+ + + + + + + + + + + +
#NomCàrregSalariAccions
+
+
+
+

Crear Empleat

+
+
+
+
+
+
+ +
+
+ Nom: +
+
+ Càrreg: + +
+
+

+ + +
+
+
+
+ + + + \ No newline at end of file