Skip to content

Commit cb2b0bb

Browse files
committed
2 parents f12143e + b3f5cd2 commit cb2b0bb

29 files changed

+962
-48
lines changed

Application.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.context.annotation.Bean;
1010

1111
@SpringBootApplication
12+
@EnableSwagger2
1213
public class Application {
1314

1415
public static void main(String[] args) {

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Work In Progress
12
# Learning sandbox
23

34
This is a learning project aimed at familiarizing myself with a new technology stack that includes Java, Spring Boot, and PostgreSQL.
@@ -17,4 +18,16 @@ This is a learning project aimed at familiarizing myself with a new technology s
1718
In combination, Java, Spring Boot, and PostgreSQL provide a powerful stack for building scalable and reliable applications. Java serves as the programming language, Spring Boot provides a streamlined development framework, and PostgreSQL offers a robust and feature-rich database solution. Together, they enable developers to create efficient, maintainable, and scalable applications for various use cases.
1819

1920

20-
Architecture description in Architecture.md
21+
# How toi start
22+
23+
Start project
24+
25+
'./mvnw spring-boot:run'
26+
27+
Architecture description in [Architecture.md](/Architecture.md)
28+
29+
In this project is implemented the use case described here: [Usecase](usecase.md)
30+
31+
32+
33+
Swagger in app: http://localhost:8080/swagger-ui/index.html

UTILS.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
https://docs.spring.io/
1212
https://docs.spring.io/spring-security/
1313
https://www.tutorialspoint.com/postgresql/
14+
15+
Maven Repository: https://mvnrepository.com/
16+
API docu - swagger: https://springdoc.org/v2/

pom.xml

+35-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<parent>
67
<groupId>org.springframework.boot</groupId>
78
<artifactId>spring-boot-starter-parent</artifactId>
89
<version>3.0.6</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10+
<relativePath /> <!-- lookup parent from repository -->
1011
</parent>
1112
<groupId>com.skia</groupId>
1213
<artifactId>lab</artifactId>
@@ -40,18 +41,18 @@
4041
<artifactId>jjwt-api</artifactId>
4142
<version>0.11.5</version>
4243
</dependency>
43-
<dependency>
44-
<groupId>io.jsonwebtoken</groupId>
45-
<artifactId>jjwt-impl</artifactId>
46-
<version>0.11.5</version>
47-
<scope>runtime</scope>
48-
</dependency>
49-
<dependency>
50-
<groupId>io.jsonwebtoken</groupId>
51-
<artifactId>jjwt-jackson</artifactId>
52-
<version>0.11.5</version>
53-
<scope>runtime</scope>
54-
</dependency>
44+
<dependency>
45+
<groupId>io.jsonwebtoken</groupId>
46+
<artifactId>jjwt-impl</artifactId>
47+
<version>0.11.5</version>
48+
<scope>runtime</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.jsonwebtoken</groupId>
52+
<artifactId>jjwt-jackson</artifactId>
53+
<version>0.11.5</version>
54+
<scope>runtime</scope>
55+
</dependency>
5556
<dependency>
5657
<groupId>org.postgresql</groupId>
5758
<artifactId>postgresql</artifactId>
@@ -69,18 +70,26 @@
6970
<groupId>org.springframework.boot</groupId>
7071
<artifactId>spring-boot-starter-security</artifactId>
7172
</dependency>
72-
<!-- <dependency>
73-
<groupId>io.springfox</groupId>
74-
<artifactId>springfox-swagger2</artifactId>
75-
<version>2.4.0</version>
73+
<dependency>
74+
<groupId>org.springdoc</groupId>
75+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
76+
<version>2.1.0</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.springdoc</groupId>
80+
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
81+
<version>2.1.0</version>
7682
</dependency>
77-
7883
<dependency>
79-
<groupId>io.springfox</groupId>
80-
<artifactId>springfox-boot-starter</artifactId>
81-
<version>3.0.0</version>
82-
</dependency> -->
83-
84+
<groupId>org.springdoc</groupId>
85+
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
86+
<version>2.1.0</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>com.github.javafaker</groupId>
90+
<artifactId>javafaker</artifactId>
91+
<version>1.0.2</version>
92+
</dependency>
8493
</dependencies>
8594
<build>
8695
<plugins>
@@ -90,5 +99,5 @@
9099
</plugin>
91100
</plugins>
92101
</build>
93-
94-
</project>
102+
103+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.skia.lab.Components;
2+
3+
import org.springframework.boot.context.event.ApplicationReadyEvent;
4+
import org.springframework.context.ApplicationListener;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class ApplicationStartupListener implements ApplicationListener<ApplicationReadyEvent> {
9+
private final MockDataGenerator mockDataGenerator;
10+
11+
public ApplicationStartupListener(MockDataGenerator mockDataGenerator) {
12+
this.mockDataGenerator = mockDataGenerator;
13+
}
14+
15+
@Override
16+
public void onApplicationEvent(ApplicationReadyEvent event) {
17+
mockDataGenerator.generateMockData();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.skia.lab.Components;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalTime;
5+
6+
import org.springframework.stereotype.Component;
7+
8+
import com.github.javafaker.Faker;
9+
import com.skia.lab.models.usecase.AbsenceType;
10+
import com.skia.lab.models.usecase.Attendance;
11+
import com.skia.lab.models.usecase.Department;
12+
import com.skia.lab.models.usecase.Employee;
13+
import com.skia.lab.models.usecase.Hire;
14+
import com.skia.lab.repository.usecase.AttendanceRepository;
15+
import com.skia.lab.repository.usecase.DepartmentRepository;
16+
import com.skia.lab.repository.usecase.EmployeeRepository;
17+
import com.skia.lab.repository.usecase.HireRepository;
18+
import com.skia.lab.repository.usecase.PerformanceEvaluationRepository;
19+
import com.skia.lab.repository.usecase.TrainingRepository;
20+
import com.skia.lab.repository.usecase.WageRepository;
21+
22+
@Component
23+
public class MockDataGenerator {
24+
private final EmployeeRepository employeeRepository;
25+
private final DepartmentRepository departmentRepository;
26+
private final HireRepository hireRepository;
27+
private final AttendanceRepository attendanceRepository;
28+
private final PerformanceEvaluationRepository performanceEvaluationRepository;
29+
private final TrainingRepository trainingRepository;
30+
private final WageRepository wageRepository;
31+
private final Faker faker;
32+
33+
public MockDataGenerator(EmployeeRepository employeeRepository,
34+
DepartmentRepository departmentRepository,
35+
HireRepository hireRepository,
36+
AttendanceRepository attendanceRepository,
37+
PerformanceEvaluationRepository performanceEvaluationRepository,
38+
TrainingRepository trainingRepository,
39+
WageRepository wageRepository
40+
) {
41+
this.employeeRepository = employeeRepository;
42+
this.departmentRepository = departmentRepository;
43+
this.hireRepository = hireRepository;
44+
this.attendanceRepository = attendanceRepository;
45+
this.performanceEvaluationRepository = performanceEvaluationRepository;
46+
this.trainingRepository = trainingRepository;
47+
this.wageRepository = wageRepository;
48+
this.faker = new Faker();
49+
}
50+
51+
public void generateMockData() {
52+
53+
long eIdx = 0;
54+
55+
if(departmentRepository.count() != 0) departmentRepository.deleteAll();
56+
if(departmentRepository.count() == 0){
57+
for (int i = 0; i < 5; i++) {
58+
var d = new Department(faker.animal().name());
59+
d.setId((long)i);
60+
departmentRepository.save(d);
61+
}
62+
}
63+
64+
65+
if(employeeRepository.count() != 0) employeeRepository.deleteAll();
66+
67+
Employee[] employees = new Employee[40];
68+
if(employeeRepository.count() == 0){
69+
70+
71+
for (int i = 0; i < 40; i++) {
72+
employees[i] = new Employee();
73+
employees[i].setFirstName(faker.name().firstName());
74+
employees[i].setLastName(faker.name().lastName());
75+
employees[i].setBirthDate(LocalDate.now());
76+
employees[i].setAddress(faker.address().fullAddress());
77+
employees[i].setPhoneNumber(faker.name().fullName());
78+
employees[i].setEmail(faker.internet().emailAddress());
79+
long a = 4;
80+
if(i>10 && i<20) a = 1;
81+
if(i>20 && i<30) a = 2;
82+
if(i>30 && i<40) a = 3;
83+
if(departmentRepository.existsById(a))
84+
employees[i].setDepartment(departmentRepository.findById(a).get());
85+
86+
if(i>10) employees[i].setManager(employees[10]);
87+
if(i<10 && i >0) employees[i].setManager(employees[0]);
88+
89+
// employee.setDepartment(i<10 ? department1 : department2);
90+
employeeRepository.save(employees[i]);
91+
if(i == 0) eIdx = employees[i].getId();
92+
}
93+
}
94+
if(hireRepository.count() != 0) hireRepository.deleteAll();
95+
if(hireRepository.count() == 0){
96+
97+
var e0 =employeeRepository.findById((long)eIdx);
98+
Hire hire0 = new Hire(LocalDate.now(), faker.name().title(), 50000, "commercio", e0.get());
99+
hireRepository.save(hire0);
100+
for (int i = 1; i < 20; i++) {
101+
var e =employeeRepository.findById((long)employees[i].getId());
102+
Hire hire = new Hire(LocalDate.now(), "Sales", 30000, "commercio", e.get());
103+
104+
hireRepository.save(hire);
105+
}
106+
107+
}
108+
109+
if(attendanceRepository.count() != 0) attendanceRepository.deleteAll();
110+
if(attendanceRepository.count() == 0){
111+
112+
var e0 =employeeRepository.findById((long)eIdx);
113+
Attendance att0 = new Attendance(LocalDate.of(2023,2,1), LocalTime.of(8,0,0),LocalTime.of(18,0,0), null, e0.get());
114+
attendanceRepository.save(att0);
115+
116+
for (int i = 1; i < 28; i++){
117+
118+
Attendance attX = new Attendance(LocalDate.of(2023,1,i), LocalTime.of(8,0,0),LocalTime.of(18,0,0), null , e0.get());
119+
attX.setAbsenceType(AbsenceType.sickness.ordinal());
120+
attendanceRepository.save(attX);
121+
}
122+
123+
for (int j = 1; j < employeeRepository.count(); j++) {
124+
var e =employeeRepository.findById((long)employees[j].getId());
125+
126+
for (int i = 1; i < 28; i++) { for (int k = 1; k < 12; k++) attendanceRepository.save( new Attendance(LocalDate.of(2023,k,i), LocalTime.of(8,0,0),LocalTime.of(18,0,0), null, e.get())); }
127+
128+
}
129+
}
130+
131+
}
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.skia.lab.controllers.usecase;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import com.skia.lab.models.usecase.Attendance;
9+
import com.skia.lab.repository.usecase.AttendanceRepository;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Optional;
14+
15+
//@CrossOrigin(origins = "http://localhost:8081")
16+
@RestController
17+
@RequestMapping("/api/attendance")
18+
public class AttendanceController {
19+
20+
// associated employee ID, date, time of entry, time of exit, and type of absence (vacation, sickness, leave)
21+
@Autowired
22+
AttendanceRepository attendanceRepository;
23+
24+
// @Autowired
25+
// public AttendanceController(AttendanceRepository attendanceRepository) {
26+
// this.attendanceRepository = attendanceRepository;
27+
// }
28+
29+
@PostMapping
30+
public ResponseEntity<Attendance> createAttendance(@RequestBody Attendance attendance) {
31+
try {
32+
Attendance _attendance = attendanceRepository
33+
.save(attendance);
34+
return new ResponseEntity<>(_attendance, HttpStatus.CREATED);
35+
} catch (Exception e) {
36+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
37+
}
38+
}
39+
40+
@GetMapping("/{id}")
41+
public ResponseEntity<Attendance> getAttendanceById(@PathVariable Long id) {
42+
Optional<Attendance> attendanceData = attendanceRepository.findById(id);
43+
if (attendanceData.isPresent()) {
44+
return new ResponseEntity<>(attendanceData.get(), HttpStatus.OK);
45+
} else {
46+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
47+
}
48+
}
49+
50+
@GetMapping("/")
51+
public ResponseEntity<List<Attendance>> getAllAttendances(@RequestParam(required = false) Long employeeId) {
52+
53+
try {
54+
List<Attendance> attendance = new ArrayList<Attendance>();
55+
56+
if (employeeId != null)
57+
attendanceRepository.findByEmployee(employeeId).forEach(attendance::add);
58+
else
59+
attendanceRepository.findAll().forEach(attendance::add);
60+
61+
if (attendance.isEmpty()) {
62+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
63+
}
64+
65+
return new ResponseEntity<>(attendance, HttpStatus.OK);
66+
} catch (Exception e) {
67+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
68+
}
69+
70+
}
71+
72+
@PutMapping("/{id}")
73+
public ResponseEntity<Attendance> updateAttendance(@PathVariable Long id, @RequestBody Attendance attendance) {
74+
Optional<Attendance> attendanceData = attendanceRepository.findById(id);
75+
76+
if (attendanceData.isPresent()) {
77+
Attendance _attendance = attendanceData.get();
78+
79+
_attendance.setEntryTime(attendance.getEntryTime());
80+
_attendance.setExitTime(attendance.getExitTime());
81+
_attendance.setAbsenceType(attendance.getAbsenceType().ordinal());
82+
_attendance.setEmployee(attendance.getEmployee()); //TODO get employee from employeeId
83+
84+
return new ResponseEntity<>(attendanceRepository.save(_attendance), HttpStatus.OK);
85+
} else {
86+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
87+
}
88+
}
89+
90+
@DeleteMapping("/{id}")
91+
public ResponseEntity<Void> deleteAttendance(@PathVariable Long id) {
92+
try {
93+
attendanceRepository.deleteById(id);
94+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
95+
} catch (Exception e) {
96+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
97+
}
98+
}
99+
100+
// @GetMapping("/search")
101+
// public ResponseEntity<List<Attendance>> findAttendances(@RequestParam String query) {
102+
// List<Attendance> attendances = attendanceRepository.findAttendances(query);
103+
// return ResponseEntity.ok(attendances);
104+
// }
105+
106+
//setmanager
107+
108+
//setDepartment
109+
110+
}

0 commit comments

Comments
 (0)