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

package com.example.demo.

model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "studentsdets")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name = "firstname")
private String firstname;

@Column(name = "lastname")
private String lastname;

@Column(name = "address")
private String address;

public Employee() {
super();
}
public Employee(int id, String firstname, String lastname, String address) {
super();
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

//Employee

package com.example.demo;

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

@SpringBootApplication
public class SpringbootCrudApplication {

public static void main(String[] args) {


SpringApplication.run(SpringbootCrudApplication.class, args);
}

}
//SpringBootCrudApplication

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.model.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer>{

}
//Repository

package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
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.example.demo.exception.ResourceNotFoundException;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;

@RestController
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;

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

@PostMapping("/student")
public Employee createEmployee(@RequestBody Employee employee){
return employeeRepository.save(employee);
}

@GetMapping("/student/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id")
int employeeId) throws ResourceNotFoundException{
Employee employee =
employeeRepository.findById(employeeId).orElseThrow(() -> new
ResourceNotFoundException("Student not found for this Id : "+employeeId));
return ResponseEntity.ok().body(employee);
}

@PutMapping("/student/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable(value="id") int
employeeId,@RequestBody Employee employeeDetails) throws ResourceNotFoundException{
Employee employee =
employeeRepository.findById(employeeId).orElseThrow(() -> new
ResourceNotFoundException("Student not found for this Id : "+employeeId));
employee.setFirstname(employeeDetails.getFirstname());
employee.setLastname(employeeDetails.getLastname());
employee.setAddress(employeeDetails.getAddress());
employeeRepository.save(employee);
return ResponseEntity.ok().body(employee);
}

@DeleteMapping("/student/{id}")
public ResponseEntity<?> deleteEmployee(@PathVariable(value="id")int
employeeId) throws ResourceNotFoundException{
Employee employee =
employeeRepository.findById(employeeId).orElseThrow(() -> new
ResourceNotFoundException("Student not found for this Id : "+employeeId));
employeeRepository.deleteById(employeeId);
return ResponseEntity.ok().build();
}
}
//EmployeeController

You might also like