Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 00d0e90

Browse files
[usecase] implementation CRUD on controllers
1 parent fb74aba commit 00d0e90

24 files changed

+784
-43
lines changed

‎Application.java

Lines changed: 1 addition & 0 deletions
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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
https://docs.spring.io/
33
https://docs.spring.io/spring-security/
44
https://www.tutorialspoint.com/postgresql/
5+
6+
Maven Repository: https://mvnrepository.com/
7+
API docu - swagger: https://springdoc.org/v2/

‎pom.xml

Lines changed: 30 additions & 26 deletions
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,21 @@
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>
7677
</dependency>
77-
7878
<dependency>
79-
<groupId>io.springfox</groupId>
80-
<artifactId>springfox-boot-starter</artifactId>
81-
<version>3.0.0</version>
82-
</dependency> -->
83-
79+
<groupId>org.springdoc</groupId>
80+
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
81+
<version>2.1.0</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.springdoc</groupId>
85+
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
86+
<version>2.1.0</version>
87+
</dependency>
8488
</dependencies>
8589
<build>
8690
<plugins>
@@ -90,5 +94,5 @@
9094
</plugin>
9195
</plugins>
9296
</build>
93-
94-
</project>
97+
98+
</project>
Lines changed: 110 additions & 0 deletions
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+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.Department;
9+
import com.skia.lab.repository.usecase.DepartmentRepository;
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/department")
18+
public class DepartmentController {
19+
20+
@Autowired
21+
DepartmentRepository departmentRepository;
22+
23+
// @Autowired
24+
// public DepartmentController(DepartmentRepository departmentRepository) {
25+
// this.departmentRepository = departmentRepository;
26+
// }
27+
28+
@PostMapping
29+
public ResponseEntity<Department> createDepartment(@RequestBody Department department) {
30+
try {
31+
Department _department = departmentRepository
32+
.save(new Department(department.getName()));
33+
return new ResponseEntity<>(_department, HttpStatus.CREATED);
34+
} catch (Exception e) {
35+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
36+
}
37+
}
38+
39+
@GetMapping("/{id}")
40+
public ResponseEntity<Department> getDepartmentById(@PathVariable Long id) {
41+
Optional<Department> departmentData = departmentRepository.findById(id);
42+
if (departmentData.isPresent()) {
43+
return new ResponseEntity<>(departmentData.get(), HttpStatus.OK);
44+
} else {
45+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
46+
}
47+
}
48+
49+
@GetMapping("/")
50+
public ResponseEntity<List<Department>> getAllDepartments() {
51+
52+
try {
53+
List<Department> department = new ArrayList<Department>();
54+
55+
departmentRepository.findAll().forEach(department::add);
56+
57+
if (department.isEmpty()) {
58+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
59+
}
60+
61+
return new ResponseEntity<>(department, HttpStatus.OK);
62+
} catch (Exception e) {
63+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
64+
}
65+
66+
}
67+
68+
@PutMapping("/{id}")
69+
public ResponseEntity<Department> updateDepartment(@PathVariable Long id, @RequestBody Department department) {
70+
Optional<Department> departmentData = departmentRepository.findById(id);
71+
72+
if (departmentData.isPresent()) {
73+
Department _department = departmentData.get();
74+
_department.setName(department.getName());
75+
return new ResponseEntity<>(departmentRepository.save(_department), HttpStatus.OK);
76+
} else {
77+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
78+
}
79+
}
80+
81+
@DeleteMapping("/{id}")
82+
public ResponseEntity<Void> deleteDepartment(@PathVariable Long id) {
83+
try {
84+
departmentRepository.deleteById(id);
85+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
86+
} catch (Exception e) {
87+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
88+
}
89+
}
90+
91+
// @GetMapping("/search")
92+
// public ResponseEntity<List<Department>> findDepartments(@RequestParam String query) {
93+
// List<Department> departments = departmentRepository.findDepartments(query);
94+
// return ResponseEntity.ok(departments);
95+
// }
96+
97+
//setmanager
98+
99+
//setDepartment
100+
101+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /