diff --git a/README.md b/README.md index 442b8d4..e70b00e 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,9 @@ ## Structure Directory https://studygyaan.com/spring-boot/spring-boot-project-folder-structure-and-best-practices + + +## HECHO POR: +## LAURA VALENTINA RODRIGUEZ, JUAN PABLO FERNANDEZ + +## EL proyecto realizado esta en la rama juanito diff --git a/pom.xml b/pom.xml index 6cef405..42fe890 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,11 @@ 1.18.30 compile + + com.h2database + h2 + runtime + diff --git a/src/main/java/co/edu/escuelaing/cvds/lab7/Lab7Application.java b/src/main/java/co/edu/escuelaing/cvds/lab7/Lab7Application.java index 7ef0c6f..e8b4457 100644 --- a/src/main/java/co/edu/escuelaing/cvds/lab7/Lab7Application.java +++ b/src/main/java/co/edu/escuelaing/cvds/lab7/Lab7Application.java @@ -3,8 +3,10 @@ import co.edu.escuelaing.cvds.lab7.model.Configuration; import co.edu.escuelaing.cvds.lab7.model.User; import co.edu.escuelaing.cvds.lab7.model.UserRole; +import co.edu.escuelaing.cvds.lab7.repository.EmployeeRepository; import co.edu.escuelaing.cvds.lab7.repository.UserRepository; import co.edu.escuelaing.cvds.lab7.service.ConfigurationService; +import co.edu.escuelaing.cvds.lab7.service.EmployeeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; @@ -19,16 +21,19 @@ @Slf4j public class Lab7Application { private final ConfigurationService configurationService; - + private final EmployeeService employeeService; private final UserRepository userRepository; @Autowired public Lab7Application( ConfigurationService configurationService, - UserRepository userRepository + UserRepository userRepository, + EmployeeService employeeService + ) { this.configurationService = configurationService; this.userRepository = userRepository; + this.employeeService = employeeService; } public static void main(String[] args) { @@ -38,7 +43,7 @@ public static void main(String[] args) { @Bean public CommandLineRunner run() { return (args) -> { - log.info("Adding Configurations...."); + /*log.info("Adding Configurations...."); configurationService.addConfiguration(new Configuration("premio", "810000")); configurationService.addConfiguration(new Configuration("descuento", "0.1")); configurationService.addConfiguration(new Configuration("app-name", "Miraculous: Las Aventuras de Ladybug")); @@ -48,6 +53,7 @@ public CommandLineRunner run() { log.info("\nAdding admin@site.org user with Password: admin"); userRepository.save(new User("admin@site.org", "admin", Arrays.asList(UserRole.ADMINISTRADOR, UserRole.CLIENTE))); + */ }; } diff --git a/src/main/java/co/edu/escuelaing/cvds/lab7/controller/EmployeeController.java b/src/main/java/co/edu/escuelaing/cvds/lab7/controller/EmployeeController.java new file mode 100644 index 0000000..1bd5f85 --- /dev/null +++ b/src/main/java/co/edu/escuelaing/cvds/lab7/controller/EmployeeController.java @@ -0,0 +1,67 @@ +package co.edu.escuelaing.cvds.lab7.controller; + +import co.edu.escuelaing.cvds.lab7.model.Configuration; +import co.edu.escuelaing.cvds.lab7.model.Employee; +import co.edu.escuelaing.cvds.lab7.service.ConfigurationService; +import co.edu.escuelaing.cvds.lab7.service.EmployeeService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import java.util.List; + +@Controller +@RequestMapping(value = "/employee") +public class EmployeeController { + + private final EmployeeService employeeService; + + @Autowired + public EmployeeController(EmployeeService employeeService){ + this.employeeService = employeeService; + } + + @GetMapping("/toList") + public String toList(Model model){ + List employees = employeeService.getAllEmployees(); + model.addAttribute("employees", employees); + for (Employee e : employees){ + System.out.println(e.toString()); + } + return "list"; //aqui debemos cambiarlo si queremos. + } + + @GetMapping("/create") + public String createEmployee(Model model){ + model.addAttribute("employees",new Employee()); + return "create"; + } + + @GetMapping("/delete/{id}") + public String deleteEmployee(@PathVariable String id){ + employeeService.deleteEmployee(id); + return "redirect:/employee/toList"; + } + + @GetMapping("/api/employees") + @ResponseBody + public List exampleApiEmployees() { + return employeeService.getAllEmployees(); + } + + + @PostMapping("/api/employees") + @ResponseBody + public List exampleApiEmployees(@RequestBody Employee employee) { + employeeService.addEmployee(employee); + return employeeService.getAllEmployees(); + } + + @PostMapping("/save") + public String save(String idEmployee, String firstName, String lastName, String role, double salary) { + employeeService.addEmployee(new Employee(idEmployee, firstName, lastName, role, salary)); + return "redirect:/employee/toList"; + } + +} diff --git a/src/main/java/co/edu/escuelaing/cvds/lab7/model/Employee.java b/src/main/java/co/edu/escuelaing/cvds/lab7/model/Employee.java new file mode 100644 index 0000000..7fd4e30 --- /dev/null +++ b/src/main/java/co/edu/escuelaing/cvds/lab7/model/Employee.java @@ -0,0 +1,87 @@ +package co.edu.escuelaing.cvds.lab7.model; + + + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +@Entity +@Table(name = "EMPLOYEES") + +public class Employee { + @Id + @Column(name ="EMPLOYE_ID") + private String employeeId; + @Column(name = "FIRST_NAME") + private String firstName; + @Column(name = "LAST_NAME") + private String lastName; + @Column(name = "ROLE") + private String role; + @Column(name = "Salary") + private double salary; + + public Employee(){ + + } + + public Employee(String employeeId, String firstName, String lastName, String role, double salary) { + this.employeeId = employeeId; + this.firstName = firstName; + this.lastName = lastName; + this.role = role; + this.salary = salary; + } + + @Override + public String toString() { + return "Employee{" + + "employeeId='" + employeeId + '\'' + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", role='" + role + '\'' + + ", salary=" + salary + + '}'; + } + + public String getEmployeeId() { + return employeeId; + } + + public void setEmployeeId(String employeeId) { + this.employeeId = employeeId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } +} diff --git a/src/main/java/co/edu/escuelaing/cvds/lab7/repository/EmployeeRepository.java b/src/main/java/co/edu/escuelaing/cvds/lab7/repository/EmployeeRepository.java new file mode 100644 index 0000000..b2fa310 --- /dev/null +++ b/src/main/java/co/edu/escuelaing/cvds/lab7/repository/EmployeeRepository.java @@ -0,0 +1,17 @@ +package co.edu.escuelaing.cvds.lab7.repository; + +import org.springframework.stereotype.Repository; + +import co.edu.escuelaing.cvds.lab7.model.Configuration; +import co.edu.escuelaing.cvds.lab7.model.Employee; +import co.edu.escuelaing.cvds.lab7.model.Configuration; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EmployeeRepository extends JpaRepository { + public List findByEmployeeId(String Id); +} diff --git a/src/main/java/co/edu/escuelaing/cvds/lab7/service/EmployeeService.java b/src/main/java/co/edu/escuelaing/cvds/lab7/service/EmployeeService.java new file mode 100644 index 0000000..9234a17 --- /dev/null +++ b/src/main/java/co/edu/escuelaing/cvds/lab7/service/EmployeeService.java @@ -0,0 +1,43 @@ +package co.edu.escuelaing.cvds.lab7.service; + +import co.edu.escuelaing.cvds.lab7.model.Configuration; +import co.edu.escuelaing.cvds.lab7.model.Employee; +import co.edu.escuelaing.cvds.lab7.repository.ConfigurationRepository; +import co.edu.escuelaing.cvds.lab7.repository.EmployeeRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class EmployeeService { + private final EmployeeRepository employeeRepository; + + @Autowired + public EmployeeService(EmployeeRepository employeeRepository){ + this.employeeRepository = employeeRepository; + } + + public Employee addEmployee(Employee employee){ + return employeeRepository.save(employee); + } + + public Optional getEmployee(String id){ + return employeeRepository.findById(id); + } + + public List getAllEmployees(){ + return employeeRepository.findAll(); + } + + public void deleteEmployee(String id){ + employeeRepository.deleteById(id); + } + + public String getName(){ + return employeeRepository.findByEmployeeId("1").get(0).getFirstName(); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index aebf29a..3b8d83c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,13 +7,33 @@ spring.groovy.template.cache = false # ORM # next line deletes the database on startup or shutdown -# spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=create-drop + # next line updates the database on startup -spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=${MYSQLCONNSTR_MyDatabase:jdbc:mysql://localhost:3306/cvds?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&user=root&password=my-secret-pw} -#spring.datasource.username=root -#spring.datasource.password=my-secret-pw +#spring.jpa.hibernate.ddl-auto=create + + +spring.datasource.url=jdbc:mysql://localhost:3306/cvds?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&user=root&password=my-secret-pw +spring.datasource.username=root +spring.datasource.password=my-secret-pw spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver +#spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect spring.jpa.show-sql=true -# +#H2 +#spring.datasource.url=jdbc:h2:mem:testdb +#spring.datasource.driverClassName=org.h2.Driver +#spring.datasource.username=sa +#spring.datasource.password=password +#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +#spring.jpa.defer-datasource-initialization=true +#spring.jpa.hibernate.ddl-auto=create-drop +# File data storage +#spring.datasource.url=jdbc:h2:file:/Users/smileit/it/data2 +# Enable h2 gui console - access through: http://localhost:8080/h2-console +#spring.h2.console.enabled=true + +spring.jpa.defer-datasource-initialization=true +spring.sql.init.mode=always + + diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css new file mode 100644 index 0000000..d07baa6 --- /dev/null +++ b/src/main/resources/static/css/style.css @@ -0,0 +1,43 @@ +.container { + margin: 0 auto; /* Centra el contenido */ + max-width: 600px; /* Máximo ancho del contenedor */ + padding: 20px; /* Espacio interno */ + text-align: left; /* Alineación de texto */ +} + +.title { + text-align: center; /* Centra el título */ + color: #333; /* Color del texto */ +} + +.rows { + margin-bottom: 10px; /* Espacio entre filas */ +} + +.boxes { + width: 100%; /* Ancho completo */ + padding: 10px; /* Espacio interno */ + border: 1px solid #ccc; /* Borde */ + border-radius: 5px; /* Bordes redondeados */ + font-size: 16px; /* Tamaño de la fuente */ +} + +.boxes:focus { + outline: none; /* Quita el borde azul por defecto */ + border-color: #007bff; /* Color del borde al estar en foco */ + background-color: #f0f8ff; /* Color de fondo al estar en foco */ +} + +.links { + text-decoration: none; /* Sin subrayado */ + color: white; /* Color del texto */ + background-color: #007bff; /* Color de fondo */ + padding: 10px 20px; /* Espacio interno */ + border-radius: 5px; /* Bordes redondeados */ + cursor: pointer; /* Cambia el cursor al pasar */ + margin: 20px; +} + +.links:hover { + background-color: #0056b3; /* Color al pasar */ +} diff --git a/src/main/resources/static/css/style1.css b/src/main/resources/static/css/style1.css new file mode 100644 index 0000000..cce0599 --- /dev/null +++ b/src/main/resources/static/css/style1.css @@ -0,0 +1,79 @@ +/* Estilo para la estructura principal de la página */ +body { + font-family: Arial, sans-serif; /* Fuente para el texto */ + background-color: #f9f9f9; /* Color de fondo de la página */ + color: #333; /* Color del texto */ + margin: 0; /* Sin margen en el cuerpo */ + padding: 0; /* Sin relleno en el cuerpo */ +} + +/* Estilo para el contenedor principal */ +.container { + max-width: 1000px; /* Ancho máximo del contenedor */ + margin: 30px auto; /* Centra el contenedor */ + padding: 20px; /* Espacio interno */ + background-color: white; /* Fondo blanco */ + border-radius: 10px; /* Bordes redondeados */ + box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Sombra */ +} + +/* Estilo para el título */ +h1 { + text-align: center; /* Centra el título */ + font-size: 2em; /* Tamaño de fuente */ + color: #007bff; /* Color azul */ +} + +/* Estilo para la tabla */ +.employee-table { + width: 100%; /* Tabla ocupa todo el ancho */ + border-collapse: collapse; /* Sin espacio entre celdas */ + margin-top: 20px; /* Espacio arriba de la tabla */ +} + +/* Estilo para las celdas de la tabla */ +.employee-table th, .employee-table td { + border: 1px solid #ddd; /* Bordes de las celdas */ + padding: 10px; /* Espacio interno */ + text-align: center; /* Alinea el texto */ +} + +/* Estilo para las cabeceras de la tabla */ +.employee-table th { + background-color: #f2f2f2; /* Color de fondo */ + font-weight: bold; /* Texto en negrita */ +} + +/* Estilo para las filas de la tabla */ +.employee-table tr:hover { + background-color: #f5f5f5; /* Color al pasar */ +} + +/* Estilo para la sección de acciones */ +.actions { + text-align: center; /* Centra las acciones */ + margin-top: 20px; /* Espacio antes de las acciones */ +} + +/* Estilo para los enlaces */ +.links { + color: #007bff; /* Color del enlace */ + text-decoration: none; /* Sin subrayado */ + padding: 10px 20px; /* Espacio interno */ + border-radius: 5px; /* Bordes redondeados */ + background-color: #e0e0e0; /* Fondo del enlace */ + border: 1px solid #007bff; /* Borde del enlace */ + transition: background-color 0.3s; /* Efecto de transición */ +} + +/* Estilo para el efecto hover en los enlaces */ +.links:hover { + background-color: #007bff; /* Color de fondo al pasar */ + color: white; /* Color del texto al pasar */ + text-decoration: none; /* Sin subrayado */ +} + +/* Estilo para el espaciado entre enlaces */ +.actions .links { + margin: 10px; /* Margen entre enlaces */ +} diff --git a/src/main/resources/templates/create.html b/src/main/resources/templates/create.html new file mode 100644 index 0000000..ece8fff --- /dev/null +++ b/src/main/resources/templates/create.html @@ -0,0 +1,45 @@ + + + + + Crear Empleado + + + + +
+

Crear Empleado

+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+ +
+ Volver a la lista de empleados + + diff --git a/src/main/resources/templates/list.html b/src/main/resources/templates/list.html new file mode 100644 index 0000000..5285650 --- /dev/null +++ b/src/main/resources/templates/list.html @@ -0,0 +1,40 @@ + + + + + Lista de Empleados + + + +
+

Lista de Empleados

+ + + + + + + + + + + + + + + + + + + + + +
IDNombreApellidoRolSalarioAcciones
+ Eliminar +
+ +
+ +