Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Spring-1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

// 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


}

// 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;

@ManyToOne
@JoinColumn(name = "department_id")
private Department department;

// Getters and Setters


}

// 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> {


}
// DepartmentController.java
package com.example.demo.controller;

import com.example.demo.model.Department;
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("/departments")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;

@GetMapping
public List<Department> getAllDepartments() {
return departmentRepository.findAll();
}

@PostMapping
public Department createDepartment(@RequestBody Department department) {
return departmentRepository.save(department);
}
}

// EmployeeController.java
package com.example.demo.controller;

import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;

@GetMapping
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}

@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}
}
# application.properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create

npx expo init EmployeeDepartmentApp


cd EmployeeDepartmentApp

npm install axios

// App.js
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, FlatList } from 'react-native';
import axios from 'axios';

const App = () => {


const [departments, setDepartments] = useState([]);
const [employees, setEmployees] = useState([]);
const [departmentName, setDepartmentName] = useState('');
const [employeeName, setEmployeeName] = useState('');
const [selectedDepartment, setSelectedDepartment] = useState(null);

useEffect(() => {
fetchDepartments();
fetchEmployees();
}, []);

const fetchDepartments = async () => {


const response = await axios.get('http://localhost:8080/departments');
setDepartments(response.data);
};

const fetchEmployees = async () => {


const response = await axios.get('http://localhost:8080/employees');
setEmployees(response.data);
};

const addDepartment = async () => {


await axios.post('http://localhost:8080/departments', { name:
departmentName });
setDepartmentName('');
fetchDepartments();
};

const addEmployee = async () => {


await axios.post('http://localhost:8080/employees', { name: employeeName,
department: { id: selectedDepartment } });
setEmployeeName('');
fetchEmployees();
};

return (
<View style={{ padding: 20 }}>
<Text>Departments</Text>
<FlatList
data={departments}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<Text>{item.name}</Text>
)}
/>
<TextInput
placeholder="Department Name"
value={departmentName}
onChangeText={setDepartmentName}
/>
<Button title="Add Department" onPress={addDepartment} />

<Text>Employees</Text>
<FlatList
data={employees}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<Text>{item.name} - {item.department.name}</Text>
)}
/>
<TextInput
placeholder="Employee Name"
value={employeeName}
onChangeText={setEmployeeName}
/>
<TextInput
placeholder="Select Department ID"
value={selectedDepartment}
onChangeText={setSelectedDepartment}
/>
<Button title="Add Employee" onPress={addEmployee} />
</View>
);
};

export default App;

You might also like