This is a full stack application with complete functionality that is required to manage a employee through creating an emloyee in database, view an employe from the database, list of employees in the database, updating an employee detail in the dataase and deleting an employee from the database. The technologies usd in this application are Java SpringBoot for backend and Angular for frontend.
@RestController
@RequestMapping("/home")
@CrossOrigin(origins = "http://localhost:4200")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
//get all employees
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return this.employeeRepository.findAll();
}
//create employee rest api
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee emp) {
return this.employeeRepository.save(emp);
}
//get employee by id rest api
@GetMapping("/employees/{empId}")
public ResponseEntity<Employee> getEmployee(@PathVariable long empId) {
Employee emp=this.employeeRepository.findById(empId).orElseThrow(()-> new ResourceNotFoundException("Employee Not Found with Id: "+empId));
return ResponseEntity.ok(emp);
}
//update emloyee rest api
@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable long id, @RequestBody Employee emp) {
Employee employee=this.employeeRepository.findById(id).orElseThrow(()-> new ResourceNotFoundException("Employee Not Found with Id: "+id));
employee.setFirstName(emp.getFirstName());
employee.setLastname(emp.getLastname());
employee.setEmailId(emp.getEmailId());
Employee updatedEmployee=employeeRepository.save(employee);
return ResponseEntity.ok(updatedEmployee);
}
//delete employee rest api
@DeleteMapping("/employees/{id}")
public ResponseEntity<Map<String, Boolean>> deleteEmployee(@PathVariable long id){
Employee employee=this.employeeRepository.findById(id).orElseThrow(()-> new ResourceNotFoundException("Employee Not Found with Id: "+id));
employeeRepository.delete(employee);
Map<String,Boolean> response=new HashMap<>();
response.put("deleted", Boolean.TRUE);
return ResponseEntity.ok(response);
}
}
`
Entity for storing the entity in database
Create a subpackage and create a class Employee.java for storing the employee in database
Use the necessary dependencies in the pom.xml file to allow maven to automatically download all the dependencies and do the necessary configuration automatically
Starting Point Of SpringBoot Application
Create A Package And Create A Class EmployeemanagementApplication Class
`package com.employee.management;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class EmployeemanagementApplication {
} `
Controller Class For Handling Web Requests
Create a subpackage and create a Controller Class
`package com.employee.management.controller;
import java.util.HashMap; import java.util.List; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import com.employee.management.exception.ResourceNotFoundException; import com.employee.management.model.Employee; import com.employee.management.repository.EmployeeRepository;
@RestController @RequestMapping("/home") @CrossOrigin(origins = "http://localhost:4200") public class EmployeeController {
} `
Entity for storing the entity in database
Create a subpackage and create a class Employee.java for storing the employee in database
`package com.employee.management.model;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
import org.springframework.web.bind.annotation.RequestMapping;
@Entity @Table(name="employee_details") public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id;
} `
Exception class for handling the exception if any occurs
Create a subpackage and create a class ResourceNotFoundException.java
`package com.employee.management.exception;
import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.NOT_FOUND) public class ResourceNotFoundException extends RuntimeException {
} `
Repository Interface that extends the JPARepository to implement the basic CRUD( Create, Read, Update and Delete) operations
Create a subpackage and create a interface EmployeeRepository.java
`package com.employee.management.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;
import com.employee.management.model.Employee;
@Repository public interface EmployeeRepository extends JpaRepository<Employee, Long> {
} `
Do the necessary database and hibernate configurations in the application.properties file to connect to the database
spring.datasource.url=jdbc:mysql://localhost:3306/employee_management spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.jpa.hibernate.ddl-auto=update
Use the necessary dependencies in the pom.xml file to allow maven to automatically download all the dependencies and do the necessary configuration automatically
`<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
`
FRONT-END PART STARTS
app.component.html
`
`
Create a module home-page that includes the component employee-list
home-page.module.ts
`import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { HomePageRoutingModule } from './home-page-routing.module'; import { EmployeeListComponent } from './employee-list/employee-list.component';
@NgModule({ declarations: [ EmployeeListComponent ], imports: [ CommonModule, HomePageRoutingModule, FormsModule ] }) export class HomePageModule { } `
home-page-routing.module.ts
`import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { EmployeeListComponent } from './employee-list/employee-list.component';
const routes: Routes = [ { path: '', component: EmployeeListComponent }, ];
@NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class HomePageRoutingModule { } `
Inside the employee-list component
employee-list.component.html
`
Employee List
Add Employee
Update Employee
Employee Details