Skip to content
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAO.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.manumiguezz.crudapplication.rest;

import com.manumiguezz.crudapplication.dao.EmployeeDAO;
import com.manumiguezz.crudapplication.entity.Employee;
import com.manumiguezz.crudapplication.service.EmployeeService;
import com.manumiguezz.crudapplication.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -12,19 +10,24 @@
@RequestMapping("/api")
public class EmployeeRestController {

private EmployeeService employeeService;
private Employee employeeService;

public EmployeeRestController (EmployeeService theEmployeeService) {
@Autowired
public EmployeeRestController(EmployeeService theEmployeeService) {
employeeService = theEmployeeService;
}

// expose "/employees" and return a list of employees
@GetMapping("/employees")
public List<Employee> findAll(){
public List<Employee> findAll() {
return employeeService.findAll();
}

// add mapping for GET /employees/{employeeId}

@GetMapping("/employees/{employeeId}")
public Employee getEmployee(@PathVariable int employeeId) {

Employee theEmployee = employeeService.findById(employeeId);

if (theEmployee == null) {
Expand All @@ -34,35 +37,61 @@ public Employee getEmployee(@PathVariable int employeeId) {
return theEmployee;
}

@PostMapping ("/employees")
// add mapping for POST /employees - add new employee

@PostMapping("/employees")
public Employee addEmployee(@RequestBody Employee theEmployee) {

// also just in case they pass an id in JSON ... set id to 0
// this is to force a save of new item ... instead of update

theEmployee.setId(0);

Employee dbEmployee = employeeService.save(theEmployee);

return dbEmployee;
}

// add mapping for PUT /employees - update existing employee

@PutMapping("/employees")
public Employee updateEmployee(@RequestBody Employee theEmployee) {

Employee dbEmployee = employeeService.save(theEmployee);

return dbEmployee;
}

@DeleteMapping("/employees/{employeeId} ")
// add mapping for DELETE /employees/{employeeId} - delete employee

@DeleteMapping("/employees/{employeeId}")
public String deleteEmployee(@PathVariable int employeeId) {

Employee tempEmployee = employeeService.findById(employeeId);

// throw exception if null

if (tempEmployee == null) {
throw new RuntimeException("Employee id: " + employeeId + " not found");
throw new RuntimeException("Employee id not found - " + employeeId);
}

employeeService.deleteByID(employeeId);
employeeService.deleteById(employeeId);

return "Employee with id: " + employeeId + " was deleted";
return "Deleted employee id - " + employeeId;
}

}














}

This file was deleted.

This file was deleted.

5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory
spring.datasource.username=springstudent
spring.datasource.password=springstudent
spring.datasource.password=springstudent

spring.data.rest.base-path=/company-api
spring.data.rest.default-page-size=40
5 changes: 4 additions & 1 deletion target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory
spring.datasource.username=springstudent
spring.datasource.password=springstudent
spring.datasource.password=springstudent

spring.data.rest.base-path=/company-api
spring.data.rest.default-page-size=40
Binary file not shown.
Binary file not shown.