zxcvb
package com.example.LeavesProj.Controller;
import com.example.LeavesProj.Dto.LeaveDto;
import com.example.LeavesProj.Enum.Status;
import com.example.LeavesProj.Model.LeaveRequest;
import com.example.LeavesProj.Model.Manager;
import com.example.LeavesProj.Model.Users;
import com.example.LeavesProj.Repo.LeaveRepo;
import com.example.LeavesProj.Repo.ManagerRepo;
import com.example.LeavesProj.Repo.UsersRepo;
import com.example.LeavesProj.Service.LeaveRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.logging.Logger;
@RestController
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("/leave")
public class LeaveController {
@Autowired
private LeaveRequestService leaveRequestService;
Logger logger = Logger.getLogger(LeaveController.class.getName());
@PostMapping("/apply")
public ResponseEntity<LeaveDto> applyleave(@RequestBody LeaveRequest leaveRequest, Authentication authentication){
logger.info("Received the leave lequest");
Users currentUser=leaveRequestService.findByUsername(authentication.getName());
if ((currentUser == null)){
logger.warning("user not found with username: {}");
return ResponseEntity.badRequest().body(null);
}else{
logger.info("user found with username");
}
leaveRequest.setUsers(currentUser);
return ResponseEntity.ok(leaveRequestService.createleaveRequest(leaveRequest));
}
@GetMapping("/user")
public ResponseEntity<List<LeaveDto>>getEmployeeLeaves(Authentication authentication){
logger.info("Entering getEmployeeLeaves method for user:{}");
Users users=leaveRequestService.findByUsername(authentication.getName());
if ((users == null)){
logger.warning("user not found:{}");
return ResponseEntity.notFound().build();
}else {
logger.info(("user found"));
}
return ResponseEntity.ok(leaveRequestService.getEmployeeLeaves(users));
}
@GetMapping("/manager")
public ResponseEntity<List<LeaveDto>>getManagerLeaves(Authentication authentication){
logger.info("Request received to get manager's leave for user:{}");
Manager manager =leaveRequestService.findByUserName(authentication.getName());
if ((manager == null)){
logger.warning("manager not found for username:{}");
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(leaveRequestService.getManagerLeaves(manager.getManagerId()));
}
@PutMapping("/manager/{leaveid}/{status}")
public ResponseEntity<LeaveDto>updateLeaveStatus(Authentication authentication,@PathVariable Long leaveid, @PathVariable Status status) {
logger.info("Enterning UpdateLeaveStatus with leaveid :{} and status:{}");
Manager manager = leaveRequestService.findByUserName(authentication.getName());
if (leaveRequestService.leavemanager(leaveid, manager.getManagerId())) {
return ResponseEntity.ok(leaveRequestService.updateLeaveStatus(leaveid, status));
}
return ResponseEntity.badRequest().build();
}
}
import React, { useState } from 'react';
import './Home.css';
import axios from 'axios';
function EmployeeHome({ userName, onApplyLeave, leaveRequests }) { // Accept leaveRequests as a prop
const getCurrentDate = () => new Date().toISOString().split('T')[0];
const [startDate, setStartDate] = useState(getCurrentDate());
const [endDate, setEndDate] = useState('');
const [leaveType, setLeaveType] = useState('');
const [showPopup, setShowPopup] = useState(false);
const handleApplyLeave=async()=>{
console.log(startDate);
try{
const response=await axios.post('http://localhost:8080/leave/apply',{
startDate,endDate,leaveType
})
const data=await response.data;
console.log(data)
}catch(e){
console.log(e)
}
}
return (
<div className="home-container">
{/* Leave Application Form */}
<div className="leave-form">
<input
type="date"
value={startDate}
onChange={(e) => setStartDate(e.target.value)}
/>
<input
type="date"
value={endDate}
onChange={(e) => setEndDate(e.target.value)}
/>
<select value={leaveType} onChange={(e) => setLeaveType(e.target.value)}>
<option value="">Select Leave Type</option>
<option value="CASUAL">Casual</option>
<option value="SICK">Sick</option>
</select>
<button onClick={handleApplyLeave}>Apply Leave</button>
</div>
{/* Show Popup Confirmation */}
{showPopup && (
<div className="popup-modal">
<div className="popup-content">
<p>Your leave has been submitted successfully.</p>
<button onClick={() => setShowPopup(false)}>Close</button>
</div>
</div>
)}
</div>
);
}
export default EmployeeHome;