Skip to content

Commit dd1f106

Browse files
committed
create micro-services
implement connection between employee and leave management services
1 parent 5c7242c commit dd1f106

11 files changed

+66
-21
lines changed

LeaveManagement.sqlite

0 Bytes
Binary file not shown.

src/main/java/com/erpm/leaveManagementService/controllers/LeaveController.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
public class LeaveController {
2525

2626
@Autowired
27-
LeaveService leaveService;
27+
private LeaveService leaveService;
2828

2929
@PostMapping
30-
public ResponseEntity<LeaveRequestDto> leaveRequest(@RequestBody LeaveRequestDto leaveRequest) throws LeaveTypeNotFound, LeaveStatusNotFound {
30+
public ResponseEntity<LeaveRequestDto> leaveRequest(@RequestBody LeaveRequestDto leaveRequest)
31+
throws LeaveTypeNotFound, LeaveStatusNotFound {
3132
LeaveRequestDto savedLeaveRequest = leaveService.leaveRequest(leaveRequest);
3233
return ResponseEntity.ok(savedLeaveRequest);
3334
}
@@ -46,19 +47,32 @@ public ResponseEntity<List<LeaveRequestDto>> getLeaveRequests() {
4647
}
4748
return ResponseEntity.ok(leaveRequests);
4849
}
49-
50+
5051
@PatchMapping
51-
public ResponseEntity<LeaveRequestDto> updateLeaveRequest(@RequestBody LeaveRequestDto leaveRequest) throws LeaveRequestNotFound, LeaveStatusNotFound{
52-
LeaveRequestDto updatedleaveRequest=leaveService.updateLeaveRequest(leaveRequest);
53-
if(updatedleaveRequest==null) {
52+
public ResponseEntity<LeaveRequestDto> updateLeaveRequest(@RequestBody LeaveRequestDto leaveRequest)
53+
throws LeaveRequestNotFound, LeaveStatusNotFound {
54+
LeaveRequestDto updatedleaveRequest = leaveService.updateLeaveRequest(leaveRequest);
55+
if (updatedleaveRequest == null) {
5456
return ResponseEntity.noContent().build();
5557
}
5658
return ResponseEntity.ok(updatedleaveRequest);
5759
}
58-
60+
5961
@DeleteMapping("/{leaveId}")
60-
public ResponseEntity<String> deleteLeaveRequest(@PathVariable int leaveId){
62+
public ResponseEntity<String> deleteLeaveRequest(@PathVariable int leaveId) {
6163
leaveService.deleteLeaveRequest(leaveId);
6264
return ResponseEntity.ok("");
6365
}
66+
67+
@GetMapping("/employee/{employeeId}")
68+
public ResponseEntity<List<LeaveRequestDto>> getLeaveRequestByEmployeeId(@PathVariable int employeeId) {
69+
List<LeaveRequestDto> leaves = leaveService.getLeaveRequestByEmployeeId(employeeId);
70+
return ResponseEntity.ok(leaves);
71+
}
72+
73+
@GetMapping("/approver/{approvarId}")
74+
public ResponseEntity<List<LeaveRequestDto>> getLeaveRequestByApprovalId(@PathVariable int approvarId) {
75+
List<LeaveRequestDto> leaves = leaveService.getLeaveRequestByApprovalId(approvarId);
76+
return ResponseEntity.ok(leaves);
77+
}
6478
}

src/main/java/com/erpm/leaveManagementService/controllers/LeaveStatusController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class LeaveStatusController {
2222

2323
@Autowired
24-
LeaveStatusService leaveStatusService;
24+
private LeaveStatusService leaveStatusService;
2525

2626
@PostMapping
2727
public ResponseEntity<LeaveStatus> addLeaveStatus(@RequestBody LeaveStatus leaveStatus) {

src/main/java/com/erpm/leaveManagementService/controllers/LeaveTypeController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class LeaveTypeController {
2222

2323
@Autowired
24-
LeaveTypeService leaveTypeService;
24+
private LeaveTypeService leaveTypeService;
2525

2626
@PostMapping
2727
public ResponseEntity<LeaveType> addLeaveType(@RequestBody LeaveType newleaveType) {

src/main/java/com/erpm/leaveManagementService/dtos/LeaveRequestDto.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public class LeaveRequestDto {
1515
private String reason;
1616
private String additionalComments;
1717
private int approverId;
18-
18+
1919
public LeaveRequestDto() {
20-
20+
2121
}
22-
22+
2323
public LeaveRequestDto(LeaveRequest leaveRequest) {
2424
this.id = leaveRequest.getId();
2525
this.employeeId = leaveRequest.getEmployeeId();
@@ -31,7 +31,7 @@ public LeaveRequestDto(LeaveRequest leaveRequest) {
3131
this.additionalComments = leaveRequest.getAdditionalComments();
3232
this.approverId = leaveRequest.getApproverId();
3333
}
34-
34+
3535
public int getId() {
3636
return id;
3737
}

src/main/java/com/erpm/leaveManagementService/entitys/LeaveRequest.java

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.time.LocalDate;
44

5+
import com.fasterxml.jackson.annotation.JsonManagedReference;
6+
57
import jakarta.persistence.CascadeType;
68
import jakarta.persistence.Entity;
79
import jakarta.persistence.GeneratedValue;
@@ -18,8 +20,10 @@ public class LeaveRequest {
1820
private int employeeId;
1921
private LocalDate startDate;
2022
private LocalDate endDate;
23+
@JsonManagedReference
2124
@ManyToOne(cascade = CascadeType.ALL)
2225
private LeaveType leaveType;
26+
@JsonManagedReference
2327
@ManyToOne(cascade = CascadeType.ALL)
2428
private LeaveStatus status;
2529
private String reason;

src/main/java/com/erpm/leaveManagementService/entitys/LeaveStatus.java

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Set;
44

5+
import com.fasterxml.jackson.annotation.JsonBackReference;
6+
57
import jakarta.persistence.Entity;
68
import jakarta.persistence.GeneratedValue;
79
import jakarta.persistence.GenerationType;
@@ -14,6 +16,7 @@ public class LeaveStatus {
1416
@GeneratedValue(strategy = GenerationType.AUTO)
1517
private int id;
1618
private String leaveStatus;
19+
@JsonBackReference
1720
@OneToMany(mappedBy = "status")
1821
private Set<LeaveRequest> leaveRequests;
1922

src/main/java/com/erpm/leaveManagementService/entitys/LeaveType.java

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Set;
44

5+
import com.fasterxml.jackson.annotation.JsonBackReference;
6+
57
import jakarta.persistence.Entity;
68
import jakarta.persistence.GeneratedValue;
79
import jakarta.persistence.GenerationType;
@@ -14,6 +16,7 @@ public class LeaveType {
1416
@GeneratedValue(strategy = GenerationType.AUTO)
1517
private int id;
1618
private String leaveType;
19+
@JsonBackReference
1720
@OneToMany(mappedBy = "leaveType")
1821
private Set<LeaveRequest> leaveRequests;
1922

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.erpm.leaveManagementService.repositorys;
22

3+
import java.util.List;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
46
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
57
import org.springframework.stereotype.Repository;
@@ -8,6 +10,9 @@
810

911
@Repository
1012
@EnableJpaRepositories
11-
public interface LeaveRepository extends JpaRepository<LeaveRequest,Integer>{
13+
public interface LeaveRepository extends JpaRepository<LeaveRequest, Integer> {
14+
15+
public List<LeaveRequest> findByEmployeeId(int employeeId);
1216

17+
public List<LeaveRequest> findByApproverId(int approverId);
1318
}

src/main/java/com/erpm/leaveManagementService/services/LeaveService.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
public class LeaveService {
1919

2020
@Autowired
21-
LeaveRepository leaveRepository;
21+
private LeaveRepository leaveRepository;
2222
@Autowired
23-
LeaveTypeService leaveTypeService;
23+
private LeaveTypeService leaveTypeService;
2424
@Autowired
25-
LeaveStatusService leaveStatusService;
25+
private LeaveStatusService leaveStatusService;
2626

2727
public LeaveRequestDto leaveRequest(LeaveRequestDto newleaveRequest) throws LeaveTypeNotFound, LeaveStatusNotFound {
2828
LeaveRequestDto leaveRequest = null;
@@ -52,7 +52,8 @@ public List<LeaveRequestDto> getLeaveRequests() {
5252
return leaveRepository.findAll().stream().map(leaveRequest -> new LeaveRequestDto(leaveRequest)).toList();
5353
}
5454

55-
public LeaveRequestDto updateLeaveRequest(LeaveRequestDto newleaveRequest) throws LeaveRequestNotFound, LeaveStatusNotFound {
55+
public LeaveRequestDto updateLeaveRequest(LeaveRequestDto newleaveRequest)
56+
throws LeaveRequestNotFound, LeaveStatusNotFound {
5657
LeaveRequestDto leaveRequestDto = null;
5758
try {
5859
LeaveRequest leaveRequest = leaveRepository.findById(newleaveRequest.getId()).get();
@@ -70,7 +71,7 @@ public void deleteLeaveRequest(int leaveId) {
7071
}
7172

7273
public LeaveRequest getLeaveRequest(LeaveRequestDto leaveRequestDto) throws LeaveTypeNotFound, LeaveStatusNotFound {
73-
LeaveRequest leaveRequest= new LeaveRequest();
74+
LeaveRequest leaveRequest = new LeaveRequest();
7475
leaveRequest.setEmployeeId(leaveRequestDto.getEmployeeId());
7576
leaveRequest.setStartDate(leaveRequestDto.getStartDate());
7677
leaveRequest.setEndDate(leaveRequestDto.getEndDate());
@@ -81,4 +82,16 @@ public LeaveRequest getLeaveRequest(LeaveRequestDto leaveRequestDto) throws Leav
8182
leaveRequest.setApproverId(leaveRequestDto.getApproverId());
8283
return leaveRequest;
8384
}
85+
86+
public List<LeaveRequestDto> getLeaveRequestByEmployeeId(int employeeId) {
87+
List<LeaveRequestDto> leaves = leaveRepository.findByEmployeeId(employeeId).stream()
88+
.map(leave -> new LeaveRequestDto(leave)).toList();
89+
return leaves;
90+
}
91+
92+
public List<LeaveRequestDto> getLeaveRequestByApprovalId(int approvarId) {
93+
List<LeaveRequestDto> leaves = leaveRepository.findByApproverId(approvarId).stream()
94+
.map(leave -> new LeaveRequestDto(leave)).toList();
95+
return leaves;
96+
}
8497
}

src/main/resources/application.properties

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDi
1919
eureka.instance.prefer-ip-address=true
2020
eureka.client.fetch-registry=true
2121
eureka.client.register-with-eureka=true
22-
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
22+
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
23+
#eureka helth configuration
24+
eureka.instance.leaseRenewalIntervalInSeconds=30
25+
eureka.instance.leaseExpirationDurationInSeconds=90

0 commit comments

Comments
 (0)