Department.
java
package com.example.demo.model;
import javax.persistence.*;
import java.util.List;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Employee.java
package com.example.demo.model;
import javax.persistence.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String position;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
DepartmentRepository.java
package com.example.demo.repository;
import com.example.demo.model.Department;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DepartmentRepository extends JpaRepository<Department, Long> {
}
EmployeeRepository.java
package com.example.demo.repository;
import com.example.demo.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
EmployeeController.java
package com.example.demo.controller;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import com.example.demo.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:3000")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private DepartmentRepository departmentRepository;
// Get all employees
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
// Add a new employee
@PostMapping("/employee")
public Employee addEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}
// Get all departments
@GetMapping("/departments")
public List<Department> getAllDepartments() {
return departmentRepository.findAll();
}
// Add a new department
@PostMapping("/department")
public Department addDepartment(@RequestBody Department department) {
return departmentRepository.save(department);
}
}
App.js
import React, { useEffect, useState } from 'react';
import axios from 'axios';
const App = () => {
const [departments, setDepartments] = useState([]);
const [employees, setEmployees] = useState([]);
const [employeeData, setEmployeeData] = useState({ name: '', position: '',
departmentId: '' });
const [departmentData, setDepartmentData] = useState({ name: '' });
// Fetch departments and employees on component mount
useEffect(() => {
axios.get('http://localhost:8080/api/departments')
.then(response => setDepartments(response.data))
.catch(error => console.log(error));
axios.get('http://localhost:8080/api/employees')
.then(response => setEmployees(response.data))
.catch(error => console.log(error));
}, []);
const handleEmployeeChange = (e) => {
const { name, value } = e.target;
setEmployeeData({ ...employeeData, [name]: value });
};
const handleDepartmentChange = (e) => {
const { name, value } = e.target;
setDepartmentData({ ...departmentData, [name]: value });
};
const handleEmployeeSubmit = (e) => {
e.preventDefault();
axios.post('http://localhost:8080/api/employee', employeeData)
.then(response => {
setEmployees([...employees, response.data]);
setEmployeeData({ name: '', position: '', departmentId: '' });
})
.catch(error => console.log(error));
};
const handleDepartmentSubmit = (e) => {
e.preventDefault();
axios.post('http://localhost:8080/api/department', departmentData)
.then(response => {
setDepartments([...departments, response.data]);
setDepartmentData({ name: '' });
})
.catch(error => console.log(error));
};
return (
<div>
<h1>Employee and Department Management</h1>
<h2>Departments</h2>
<form onSubmit={handleDepartmentSubmit}>
<input
type="text"
name="name"
value={departmentData.name}
onChange={handleDepartmentChange}
placeholder="Department Name"
/>
<button type="submit">Add Department</button>
</form>
<ul>
{departments.map(department => (
<li key={department.id}>{department.name}</li>
))}
</ul>
<h2>Employees</h2>
<form onSubmit={handleEmployeeSubmit}>
<input
type="text"
name="name"
value={employeeData.name}
onChange={handleEmployeeChange}
placeholder="Employee Name"
/>
<input
type="text"
name="position"
value={employeeData.position}
onChange={handleEmployeeChange}
placeholder="Position"
/>
<select
name="departmentId"
value={employeeData.departmentId}
onChange={handleEmployeeChange}
>
<option value="">Select Department</option>
{departments.map(department => (
<option key={department.id} value={department.id}>
{department.name}
</option>
))}
</select>
<button type="submit">Add Employee</button>
</form>
<ul>
{employees.map(employee => (
<li key={employee.id}>
{employee.name} - {employee.position} ({employee.department?.name})
</li>
))}
</ul>
</div>
);
};
export default App;