Skip to content

Correction Spring Part #7

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 1 commit into from
Mar 25, 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
14 changes: 11 additions & 3 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,14 @@
<java.version>15</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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 All @@ -25,7 +34,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class CrudEmpleatsApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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;
import org.springframework.web.bind.annotation.*;

import com.itacademy.CrudEmpleats.domain.Employee;
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
import com.itacademy.CrudEmpleats.service.EmployeeServices;

@RestController
public class ControllerEmployees {
Expand All @@ -19,45 +18,39 @@ public class ControllerEmployees {
*/

@Autowired
private EmployeeRepository repositori;


// Temporalment redirigim a la llista d'empleats
@GetMapping("/")
void start(HttpServletResponse reponse) throws IOException {
reponse.sendRedirect("/Empleat");
}
private EmployeeServices repositori;

// Crear nou empleat
@PostMapping( "/Empleat")
@PostMapping( "/EmployeeList")
@ResponseStatus(HttpStatus.CREATED) // 201
public Employee addEmployee(@RequestBody Employee employee) {
public Employee addEmployee(@Valid @RequestBody Employee employee) {
repositori.addEmployee(employee);
return employee;
return repositori.getEmployeeById(employee.getId());

}

// 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
10 changes: 10 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,19 @@
package com.itacademy.CrudEmpleats.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Entity
public class Employee {

@Id
@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 @@ -2,6 +2,8 @@

public class IdNotFound extends RuntimeException {

private static final long serialVersionUID = 1L;

private int id;

public IdNotFound(int id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.itacademy.CrudEmpleats.persistence;


import java.util.*;

import org.springframework.stereotype.Service;
import org.springframework.stereotype.Repository;

import com.itacademy.CrudEmpleats.domain.Employee;
import com.itacademy.CrudEmpleats.exceptions.IdNotFound;

@Service
@Repository
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<>(

private static 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++,"Joan Gordi", "Director_Projectes"),
new Employee(idcount++,"Gerard Puig", "Programador_Mid"),
new Employee(idcount++,"Maria Camps", "Administratiu"),
new Employee(idcount++,"Anaïs Iglesias", "Programador_Mid"),
new Employee(idcount++,"Pere Masnou", "Administratiu"),
new Employee(idcount++,"Laura Klaus", "Programador_Senior"),
new Employee(idcount++,"Jhon Smith", "Administratiu"),
new Employee(idcount++,"Ramón Dalmau", "Programador_Mid"),
new Employee(idcount++,"Joana Gracia", "Programador_Junior"),
new Employee(idcount++,"Quique Mota", "Administratiu"),
new Employee(idcount++,"Kevin Lohan", "Director_Projectes")
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.itacademy.CrudEmpleats.domain.Employee;


public interface IEmployeeRepository {

public List<Employee> getAllEmployees();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.itacademy.CrudEmpleats.service;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itacademy.CrudEmpleats.domain.Employee;
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;

@Service
public class EmployeeServices {

@Autowired
private EmployeeRepository repositori;

public List<Employee> getAllEmployees() {
return repositori.getAllEmployees();
}

public Employee getEmployeeById(int id){
return repositori.getEmployeeById(id);
}

public void addEmployee(Employee employee) {
repositori.addEmployee(employee);
}

public void updateEmployee(Employee employee, int id) {
repositori.updateEmployee(employee, id);
}

public void deleteEmployee(int id) {
repositori.deleteEmployee(id);
}

}