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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
HELP.md
target/
#target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# BasicSpringBootAPI

Efficient REST API crafted using Core Java and Spring Boot, enabling smooth CRUD operations on an employee database.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CrudapplicationApplication {
public class CrudApplication {

public static void main(String[] args) {
SpringApplication.run(CrudapplicationApplication.class, args);
SpringApplication.run(CrudApplication.class, args);
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//package com.manumiguezz.crudapplication.dao;
//
//import com.manumiguezz.crudapplication.entity.Employee;
//
//import java.util.List;
//
//public interface EmployeeDAO {
// List<Employee> findAll();
//
// Employee findById(int theId);
//
// Employee save(Employee theEmployee);
//
// void deleteByID(int theId);
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//package com.manumiguezz.crudapplication.dao;
//
//import com.manumiguezz.crudapplication.entity.Employee;
//import jakarta.persistence.EntityManager;
//import jakarta.persistence.TypedQuery;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//
//import java.util.List;
//
//@Repository
//public class EmployeeDAOJpaImpl implements EmployeeDAO {
//
// private EntityManager entityManager;
//
// @Autowired
// public EmployeeDAOJpaImpl(EntityManager theEntityManager) {
// entityManager = theEntityManager;
// }
//
// @Override
// public List<Employee> findAll() {
//
// TypedQuery<Employee> theQuery = entityManager.createQuery("from Employee", Employee.class);
//
// List<Employee> employees = theQuery.getResultList();
//
// return employees;
// }
//
// @Override
// public Employee findById(int theId) {
// Employee theEmployee = entityManager.find(Employee.class, theId);
// return theEmployee;
// }
//
// @Override
// public Employee save(Employee theEmployee) {
// // if ID of passed employee is equal to 0, it will insert a new one, else it will just update
// Employee dbEmployee = entityManager.merge(theEmployee);
// return dbEmployee;
// }
//
// @Override
// public void deleteByID(int theId) {
// Employee theEmployee = entityManager.find(Employee.class, theId);
// entityManager.remove(theEmployee);
// }
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.manumiguezz.crudapplication.dao;

import com.manumiguezz.crudapplication.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
74 changes: 74 additions & 0 deletions src/main/java/com/manumiguezz/crudapplication/entity/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.manumiguezz.crudapplication.entity;

import jakarta.persistence.*;

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

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

public Employee () {

}

public Employee(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

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 getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class EmployeeRestController {

private EmployeeService employeeService;

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

@GetMapping("/employees")
public List<Employee> findAll(){
return employeeService.findAll();
}

@GetMapping("/employees/{employeeId}")
public Employee getEmployee(@PathVariable int employeeId) {
Employee theEmployee = employeeService.findById(employeeId);

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

return theEmployee;
}

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

theEmployee.setId(0);

Employee dbEmployee = employeeService.save(theEmployee);
return dbEmployee;
}

@PutMapping("/employees")
public Employee updateEmployee(@RequestBody Employee theEmployee) {
Employee dbEmployee = employeeService.save(theEmployee);
return dbEmployee;
}

@DeleteMapping("/employees/{employeeId} ")
public String deleteEmployee(@PathVariable int employeeId) {
Employee tempEmployee = employeeService.findById(employeeId);

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

employeeService.deleteByID(employeeId);

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




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.manumiguezz.crudapplication.service;

import com.manumiguezz.crudapplication.entity.Employee;

import java.util.List;

public interface EmployeeService {
List<Employee> findAll();

Employee findById(int theId);

Employee save(Employee theEmployee);

void deleteByID(int theId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.manumiguezz.crudapplication.service;

import com.manumiguezz.crudapplication.dao.EmployeeDAO;
import com.manumiguezz.crudapplication.dao.EmployeeRepository;
import com.manumiguezz.crudapplication.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class EmployeeServiceImpl implements EmployeeService{

private EmployeeRepository employeeRepository;

@Autowired
public EmployeeServiceImpl (EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}

@Override
public List<Employee> findAll() {
return employeeRepository.findAll();
}

@Override
public Employee findById(int theId) {
Optional<Employee> result = employeeRepository.findById(theId);

Employee theEmployee;

if (result.isPresent()) {
theEmployee = result.get();
}
else {
throw new RuntimeException("Did not find employee id: " + theId);
}

return theEmployee;
}

@Override
public Employee save(Employee theEmployee) {
return employeeRepository.save(theEmployee);
}

@Override
public void deleteByID(int theId) {
employeeRepository.deleteById(theId);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory
spring.datasource.username=springstudent
spring.datasource.password=springstudent
4 changes: 4 additions & 0 deletions target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory
spring.datasource.username=springstudent
spring.datasource.password=springstudent
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.