Skip to content

JS Functions implemented #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.itacademy</groupId>
<artifactId>CrudEmpleats</artifactId>
Expand All @@ -17,6 +18,10 @@
<java.version>15</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Employee> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

}
6 changes: 6 additions & 0 deletions src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java
Original file line number Diff line number Diff line change
@@ -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 ;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/itacademy/CrudEmpleats/domain/Jobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ public class EmployeeRepository implements IEmployeeRepository{
// Els objectes persistents únicament en memòria

private static int idcount=0;
/*
private List<Employee> repository = new ArrayList<>(
Arrays.asList(
new Employee("Joan", "Director_Projectes"),
new Employee("Gerard", "Programador_Junior"),
new Employee("Maria", "Administrativa")
)
);
*/

private List<Employee> 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")
)
);

Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Empty file.
107 changes: 107 additions & 0 deletions src/main/resources/static/index.js
Original file line number Diff line number Diff line change
@@ -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 += "<tr>" +
"<td>" + employee.id + "</td>" +
"<td>" + employee.name + "</td>" +
"<td>" + employee.job + "</td>" +
"<td>" + employee.salary + "</td>" +
"<td>" +
"<button type='button'onclick='editEmployee(" + employee.id + ")'>Modificar</button>" +
"<button type='button' onclick='deleteEmployee(" + employee.id + ")'>Eliminar</button>" +
"</td>"
});
$('#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";
}
}
5 changes: 0 additions & 5 deletions src/main/resources/templates/Fragments/BodyHeader.html

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/resources/templates/Fragments/head.html

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/resources/templates/ModificarEmpleat.html

This file was deleted.

Loading