Skip to content

Standardized code and Enhance coding style #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion emp_backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
</dependency>



<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>javax.persistence</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name="admin")
public class adminModel {


public adminModel() {}


public adminModel(String adminName, String adminPassword) {
super();
this.adminName = adminName;
this.adminPassword = adminPassword;
}


@Id
Expand All @@ -37,24 +39,6 @@ public adminModel(String adminName, String adminPassword) {



public long getAdminID() {
return adminID;
}
public void setAdminID(long adminID) {
this.adminID = adminID;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminPassword() {
return adminPassword;
}
public void setAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -12,95 +12,63 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.exception.ResourceNotFoundException;
import com.example.demo.dto.EmployeeDto;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import com.example.demo.service.EmployeeService;

import lombok.RequiredArgsConstructor;

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

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {

@Autowired
private EmployeeRepository employeeRepository;

//get all data
private final EmployeeService employeeService;

// get all data
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/employees")
public List <Employee> getAllEmployees(){
return employeeRepository.findAll();
}



//create
public List<Employee> getAllEmployees() {
return employeeService.findAllEmployee();
}

// create
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee employee)
public Employee createEmployee(@RequestBody EmployeeDto employee)

{
return employeeRepository.save(employee);
return employeeService.addEmployee(employee);
}


// get data by id

// get data by id
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/employees/{id}")
public ResponseEntity<Employee> getByID(@PathVariable Long id) {
Employee employee = employeeRepository.findById(id).
orElseThrow(()-> new ResourceNotFoundException("Employee with id "+id+"does not exists"));
return ResponseEntity.ok(employee);
return new ResponseEntity<>(employeeService.getEmployeeById(id), HttpStatus.OK);

}


//update data

// update data
@CrossOrigin(origins = "http://localhost:4200")
@PutMapping ("/employees/{id}")
public ResponseEntity<Employee> updateEmployeeByID(@PathVariable Long id, @RequestBody Employee employeeDetails){
Employee employee = employeeRepository.findById(id).
orElseThrow(()-> new ResourceNotFoundException("Employee with id "+id+"does not exists"));


employee.setFname(employeeDetails.getFname());
employee.setLname(employeeDetails.getLname());
employee.setEmail(employeeDetails.getEmail());
employee.setDepartment(employeeDetails.getDepartment());
employee.setDesignation(employeeDetails.getDesignation());
employee.setJoiningDate(employeeDetails.getJoiningDate());
employee.setSalary(employeeDetails.getSalary());

Employee updatedEmployee=employeeRepository.save(employee);

return ResponseEntity.ok(updatedEmployee);
}



@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployeeByID(@PathVariable Long id,
@RequestBody EmployeeDto employeeDetails) {
return new ResponseEntity<>(employeeService.updateEmployee(id, employeeDetails), HttpStatus.OK);
}

@CrossOrigin(origins = "http://localhost:4200")
@DeleteMapping("/employees/{id}")
public ResponseEntity <Map<String, Boolean> >deleteEmployee(@PathVariable Long id){


Employee employee = employeeRepository.findById(id).
orElseThrow(()-> new ResourceNotFoundException("Employee with id "+id+"does not exists"));

employeeRepository.delete(employee);

Map<String, Boolean> response = new HashMap<>();
public ResponseEntity<Map<String, Boolean>> deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
Map<String, Boolean> response = new HashMap<>();
response.put("Deleted", Boolean.TRUE);
return ResponseEntity.ok(response);

}










return new ResponseEntity<>(response,HttpStatus.OK);

}

}
36 changes: 36 additions & 0 deletions emp_backend/src/main/java/com/example/demo/dto/EmployeeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.demo.dto;

import java.time.LocalDate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
public class EmployeeDto {


private long id;

private String fname;

private String lname;

private String email;

private long salary;

private String department;

private String designation;

private LocalDate joiningDate;
}
74 changes: 13 additions & 61 deletions emp_backend/src/main/java/com/example/demo/model/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import jakarta.persistence.Column;



@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
@Entity
@Table(name="employees_table")
public class Employee {


public Employee() {}

public Employee(String fname, String lname, String email, long salary, String department, String designation,
LocalDate joiningDate) {
super();
this.fname = fname;
this.lname = lname;
this.email = email;
this.salary = salary;
this.department = department;
this.designation = designation;
this.joiningDate = joiningDate;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -53,54 +52,7 @@ public Employee(String fname, String lname, String email, long salary, String de
@Column(name="jd")
private LocalDate joiningDate;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public LocalDate getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(LocalDate joiningDate) {
this.joiningDate = joiningDate;
}




Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.demo.service;

import java.util.List;

import com.example.demo.dto.EmployeeDto;
import com.example.demo.model.Employee;

public interface EmployeeService {

List<Employee> findAllEmployee();

Employee addEmployee(EmployeeDto employee);

Employee updateEmployee(Long id, EmployeeDto employeeDetails);

Employee getEmployeeById(Long id);

Employee deleteEmployee(Long id);


}
Loading