0% found this document useful (0 votes)
26 views3 pages

Crudportal

The document defines an Employee entity class with ID, name and location fields and corresponding getters and setters. It also defines an EmpService class with methods to get, add, update and delete Employee objects from a static map. An EmpController class exposes these service methods as REST endpoints.

Uploaded by

manasvm27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Crudportal

The document defines an Employee entity class with ID, name and location fields and corresponding getters and setters. It also defines an EmpService class with methods to get, add, update and delete Employee objects from a static map. An EmpController class exposes these service methods as REST endpoints.

Uploaded by

manasvm27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.demo.

entity;

public class Employee{


@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", location=" +
location + "]";
}
private int id;
private String name;
private String location;
public Employee() {
super();
}
public Employee(int id, String name, String location) {
super();
this.id = id;
this.name = name;
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
//Employee.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CrudexApplication{
public static void main(String[] args) {
SpringApplication.run(CrudexApplication.class, args);
}
}
//SpringBootApplication
package com.example.demo.service;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import com.example.demo.entity.Employee;

public class EmpService{


private static Map<Integer, Employee> employees = new HashMap<>();
private static int index = (int) 2L;
static {
Employee employee1 = new Employee((int) 1L,"manas", "cbe");
Employee employee2 = new Employee((int) 2L, "abey", "blr");
Employee employee3 = new Employee((int) 3L, "kumar", "hyd");
employees.put((int) 1L, employee1);
employees.put((int) 2L, employee2);
employees.put((int) 3L, employee3);
}
public static List<Employee> getAllEmployees(){
return new ArrayList<>(employees.values());
}
public static Employee getEmployeeDetails(int employeeId)
{
return employees.get(employeeId);
}
public static Employee addEmployee(Employee employee)
{
index+=1;
employee.setId(index);
employees.put(index, employee);
return employee;
}
public static Employee updateEmployee(int employeeId, Employee employee)
{
employee.setId(employeeId);
employees.put(employeeId, employee);
return employee;
}
public static Employee dEmployee(int employeeId)
{
return employees.remove(employeeId);
}
}
//EmployeeService

package com.example.demo.controller;

import java.util.List;

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.RestController;

import com.example.demo.entity.Employee;
import com.example.demo.service.EmpService;

@RestController
public class EmpController{
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return EmpService.getAllEmployees();
}
@GetMapping("/employee/{employeeId}")
public Employee getEmployeeDetails(@PathVariable int employeeId)
{
return EmpService.getEmployeeDetails(employeeId);
}
@PostMapping("/addEmployee")
public Employee addEmployee(@RequestBody Employee employee)
{
return EmpService.addEmployee(employee);
}
@PutMapping("/updateEmployee/{employeeId}")
public Employee updateEmployee(@PathVariable int employeeId, @RequestBody
Employee employee)
{
return EmpService.updateEmployee(employeeId, employee);
}
@DeleteMapping("/deleteEmployee/{employeeId}")
public Employee dEmploye(@PathVariable int employeeId)
{
return EmpService.dEmployee(employeeId);
}
}
//EmployeeController.java

You might also like