Repository: ddofka/Worker-API
Pull request #1: Employee entities & service
Author: login: ddofka
Created: 2025-03-19T17:11:43Z | Merged: 2025-03-19T18:36:06Z



Diff:
diff --git a/src/main/java/org/example/workerapi/WorkerApiApplication.java b/src/main/java/org/example/workerapi/WorkerApiApplication.java
index e417871..376175b 100644
--- a/src/main/java/org/example/workerapi/WorkerApiApplication.java
+++ b/src/main/java/org/example/workerapi/WorkerApiApplication.java
@@ -1,13 +1,26 @@
 package org.example.workerapi;
 
+import lombok.RequiredArgsConstructor;
+import org.example.workerapi.service.EmployeeService;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.event.EventListener;
 
+@RequiredArgsConstructor
 @SpringBootApplication
 public class WorkerApiApplication {
 
+	private final EmployeeService employeeService;
+
 	public static void main(String[] args) {
 		SpringApplication.run(WorkerApiApplication.class, args);
 	}
 
+	@EventListener(ApplicationReadyEvent.class)
+	public void test(){
+		employeeService.addTestEmployees();
+		employeeService.printAllEmployees();
+	}
+
 }
diff --git a/src/main/java/org/example/workerapi/entity/Department.java b/src/main/java/org/example/workerapi/entity/Department.java
new file mode 100644
index 0000000..98861c1
--- /dev/null
+++ b/src/main/java/org/example/workerapi/entity/Department.java
@@ -0,0 +1,25 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Getter
+@Setter
+@ToString(exclude = "employee")
+public class Department {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+    private String title;
+    private int managerPersonalCode;
+    @OneToMany(mappedBy = "department",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+    private List<Employee> employee = new ArrayList<>();
+
+}
diff --git a/src/main/java/org/example/workerapi/entity/Employee.java b/src/main/java/org/example/workerapi/entity/Employee.java
new file mode 100644
index 0000000..f5d65b6
--- /dev/null
+++ b/src/main/java/org/example/workerapi/entity/Employee.java
@@ -0,0 +1,30 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.time.LocalDate;
+
+@Entity
+@Getter
+@Setter
+@ToString
+public class Employee {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+    private int personalCode;
+    private String name;
+    private String lastName;
+    private LocalDate birthDate;
+    private LocalDate worksFrom;
+    private String position;
+    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+    private Department department;
+    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+    private Project project;
+
+}
diff --git a/src/main/java/org/example/workerapi/entity/Project.java b/src/main/java/org/example/workerapi/entity/Project.java
new file mode 100644
index 0000000..9942bcd
--- /dev/null
+++ b/src/main/java/org/example/workerapi/entity/Project.java
@@ -0,0 +1,24 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Getter
+@Setter
+@ToString(exclude = "employees")
+public class Project {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+    private String title;
+    @OneToMany(mappedBy = "project", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
+    private List<Employee> employees = new ArrayList<>();
+
+}
diff --git a/src/main/java/org/example/workerapi/repository/EmployeeRepository.java b/src/main/java/org/example/workerapi/repository/EmployeeRepository.java
new file mode 100644
index 0000000..0b312a4
--- /dev/null
+++ b/src/main/java/org/example/workerapi/repository/EmployeeRepository.java
@@ -0,0 +1,10 @@
+package org.example.workerapi.repository;
+
+import org.example.workerapi.entity.Employee;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface EmployeeRepository extends JpaRepository<Employee, Long> {
+
+}
diff --git a/src/main/java/org/example/workerapi/service/EmployeeService.java b/src/main/java/org/example/workerapi/service/EmployeeService.java
new file mode 100644
index 0000000..ecfda40
--- /dev/null
+++ b/src/main/java/org/example/workerapi/service/EmployeeService.java
@@ -0,0 +1,81 @@
+package org.example.workerapi.service;
+
+import jakarta.transaction.Transactional;
+import lombok.RequiredArgsConstructor;
+import org.example.workerapi.entity.Department;
+import org.example.workerapi.entity.Employee;
+import org.example.workerapi.entity.Project;
+import org.example.workerapi.repository.EmployeeRepository;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDate;
+import java.util.List;
+import java.util.Random;
+
+@RequiredArgsConstructor
+@Service
+public class EmployeeService {
+    private final EmployeeRepository employeeRepository;
+
+    public List<Employee> findAllEmployees() {
+        return employeeRepository.findAll();
+    }
+
+    public void printAllEmployees() {
+        findAllEmployees().forEach(System.out::println);
+    }
+
+    public Employee addEmployee(Employee employee) {
+        return employeeRepository.saveAndFlush(employee);
+    }
+
+    @Transactional
+    public void addTestEmployees(){
+        Random random = new Random();
+        Project project1 = new Project();
+        project1.setTitle("Project Fire");
+        Project project2 = new Project();
+        project2.setTitle("Project Water");
+        Project project3 = new Project();
+        project3.setTitle("Project Earth");
+
+        Department department1 = new Department();
+        department1.setTitle("Department Ice");
+        Department department2 = new Department();
+        department2.setTitle("Department Wind");
+        Department department3 = new Department();
+        department3.setTitle("Department Stone");
+
+        for (int i = 0; i < 12; i++) {
+            int birthYearOffset = 18 + random.nextInt(47);
+            LocalDate birthDate = LocalDate.now()
+                    .minusYears(birthYearOffset)
+                    .minusDays(random.nextInt(365));
+
+            Employee employee = new Employee();
+            employee.setPersonalCode(1234567891 + i);
+            employee.setName("Employee " + i);
+            employee.setLastName("Last Name " + i);
+            employee.setBirthDate(birthDate);
+            employee.setWorksFrom(LocalDate.now().minusYears(random.nextInt(10)));
+            employee.setPosition("Position " + i);
+
+            if (i < 4) {
+                employee.setDepartment(department1);
+                employee.setProject(project1);
+                department1.setManagerPersonalCode(employee.getPersonalCode());
+            } else if (i < 8) {
+                employee.setDepartment(department2);
+                employee.setProject(project2);
+                department2.setManagerPersonalCode(employee.getPersonalCode());
+            } else {
+                employee.setDepartment(department3);
+                employee.setProject(project3);
+                department3.setManagerPersonalCode(employee.getPersonalCode());
+            }
+
+            addEmployee(employee);
+        }
+    }
+
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 0260a7e..22b3a11 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1 +1,9 @@
 spring.application.name=Worker-API
+spring.jpa.hibernate.ddl-auto=create-drop
+spring.datasource.url=jdbc:mysql://localhost:3306/office?createDatabaseIfNotExist=true
+spring.datasource.username=root
+spring.datasource.password=12345678
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
+spring.jpa.show-sql=true
+spring.jpa.properties.hibernate.hbm2ddl.create_namespaces=true
+spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
\ No newline at end of file


Reviews:
user: login: Jurkenas
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-19T17:25:00Z
user: login: Jurkenas
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-19T17:25:22Z
user: login: Jurkenas
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-19T17:25:35Z

Review comments:
diff_hunk: @@ -0,0 +1,23 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Getter
+@Setter
+@RequiredArgsConstructor
+public class Department {
+
+    private String title;
+    @Id
+    private Long managerPersonalCode;
+    @OneToMany(mappedBy = "department",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
path: src/main/java/org/example/workerapi/entity/Department.java
user: login: Jurkenas
body: Šitoj vietoj rekomenduočiau padaryti paprastą generuojamą ID, o šitą managerPersonalCode kaip paprastą lauką
created_at: 2025-03-19T17:25:00Z
author_association: NONE
diff_hunk: @@ -0,0 +1,32 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.time.LocalDate;
+
+@Entity
+@Getter
+@Setter
+@RequiredArgsConstructor
path: src/main/java/org/example/workerapi/entity/Employee.java
user: login: Jurkenas
body: Šitas nenaudojamas
created_at: 2025-03-19T17:25:22Z
author_association: NONE
diff_hunk: @@ -0,0 +1,24 @@
+package org.example.workerapi.entity;
+
+import jakarta.persistence.*;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Entity
+@Getter
+@Setter
+@RequiredArgsConstructor
path: src/main/java/org/example/workerapi/entity/Project.java
user: login: Jurkenas
body: Irgi nenaudojamas
created_at: 2025-03-19T17:25:35Z
author_association: NONE

PR comments:
user: login: Jurkenas
created_at: 2025-03-19T17:26:36Z
body: apart šitų, viskas tvarkoj, gali patvarkyti ir eiti prie sekančios dalies:)
author_association: NONE
user: login: Jurkenas
created_at: 2025-03-19T17:26:56Z
body: Kai pabaigsi - sumergink ir su sekančia dalim sukurk naują PR
author_association: NONE
user: login: ddofka
created_at: 2025-03-19T18:29:15Z
body: mete error kol galiausiai atradau fixa tai to string pakeisti department ir project entities: 
@ToString(exclude = "employee")
public class Department {

author_association: OWNER