diff --git a/.gitignore b/.gitignore index 549e00a..fc4cf7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ HELP.md -target/ +#target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4d53534 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# BasicSpringBootAPI + +Efficient REST API crafted using Core Java and Spring Boot, enabling smooth CRUD operations on an employee database. diff --git a/src/main/java/com/manumiguezz/crudapplication/CrudapplicationApplication.java b/src/main/java/com/manumiguezz/crudapplication/CrudApplication.java similarity index 68% rename from src/main/java/com/manumiguezz/crudapplication/CrudapplicationApplication.java rename to src/main/java/com/manumiguezz/crudapplication/CrudApplication.java index bd36360..3861a83 100644 --- a/src/main/java/com/manumiguezz/crudapplication/CrudapplicationApplication.java +++ b/src/main/java/com/manumiguezz/crudapplication/CrudApplication.java @@ -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); } } diff --git a/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAO.java b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAO.java new file mode 100644 index 0000000..e6f13fe --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAO.java @@ -0,0 +1,15 @@ +//package com.manumiguezz.crudapplication.dao; +// +//import com.manumiguezz.crudapplication.entity.Employee; +// +//import java.util.List; +// +//public interface EmployeeDAO { +// List findAll(); +// +// Employee findById(int theId); +// +// Employee save(Employee theEmployee); +// +// void deleteByID(int theId); +//} diff --git a/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAOJpaImpl.java b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAOJpaImpl.java new file mode 100644 index 0000000..6008827 --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeDAOJpaImpl.java @@ -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 findAll() { +// +// TypedQuery theQuery = entityManager.createQuery("from Employee", Employee.class); +// +// List 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); +// } +// +//} diff --git a/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeRepository.java b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeRepository.java new file mode 100644 index 0000000..1d7d654 --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/dao/EmployeeRepository.java @@ -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 { +} diff --git a/src/main/java/com/manumiguezz/crudapplication/entity/Employee.java b/src/main/java/com/manumiguezz/crudapplication/entity/Employee.java new file mode 100644 index 0000000..75200c1 --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/entity/Employee.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/com/manumiguezz/crudapplication/rest/EmployeeRestController.java b/src/main/java/com/manumiguezz/crudapplication/rest/EmployeeRestController.java new file mode 100644 index 0000000..13a5acd --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/rest/EmployeeRestController.java @@ -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 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"; + } + + + + +} diff --git a/src/main/java/com/manumiguezz/crudapplication/service/EmployeeService.java b/src/main/java/com/manumiguezz/crudapplication/service/EmployeeService.java new file mode 100644 index 0000000..ff56fdb --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/service/EmployeeService.java @@ -0,0 +1,16 @@ +package com.manumiguezz.crudapplication.service; + +import com.manumiguezz.crudapplication.entity.Employee; + +import java.util.List; + +public interface EmployeeService { + List findAll(); + + Employee findById(int theId); + + Employee save(Employee theEmployee); + + void deleteByID(int theId); + +} diff --git a/src/main/java/com/manumiguezz/crudapplication/service/EmployeeServiceImpl.java b/src/main/java/com/manumiguezz/crudapplication/service/EmployeeServiceImpl.java new file mode 100644 index 0000000..6acf772 --- /dev/null +++ b/src/main/java/com/manumiguezz/crudapplication/service/EmployeeServiceImpl.java @@ -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 findAll() { + return employeeRepository.findAll(); + } + + @Override + public Employee findById(int theId) { + Optional 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); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 33b3c40..4a4b39a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,4 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory spring.datasource.username=springstudent spring.datasource.password=springstudent \ No newline at end of file diff --git a/target/classes/application.properties b/target/classes/application.properties new file mode 100644 index 0000000..4a4b39a --- /dev/null +++ b/target/classes/application.properties @@ -0,0 +1,4 @@ + +spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory +spring.datasource.username=springstudent +spring.datasource.password=springstudent \ No newline at end of file diff --git a/target/classes/com/manumiguezz/crudapplication/CrudApplication.class b/target/classes/com/manumiguezz/crudapplication/CrudApplication.class new file mode 100644 index 0000000..f384997 Binary files /dev/null and b/target/classes/com/manumiguezz/crudapplication/CrudApplication.class differ diff --git a/target/classes/com/manumiguezz/crudapplication/entity/Employee.class b/target/classes/com/manumiguezz/crudapplication/entity/Employee.class new file mode 100644 index 0000000..a21d932 Binary files /dev/null and b/target/classes/com/manumiguezz/crudapplication/entity/Employee.class differ diff --git a/target/classes/com/manumiguezz/crudapplication/service/EmployeeService.class b/target/classes/com/manumiguezz/crudapplication/service/EmployeeService.class new file mode 100644 index 0000000..fb6d74a Binary files /dev/null and b/target/classes/com/manumiguezz/crudapplication/service/EmployeeService.class differ diff --git a/target/test-classes/com/manumiguezz/crudapplication/CrudapplicationApplicationTests.class b/target/test-classes/com/manumiguezz/crudapplication/CrudapplicationApplicationTests.class new file mode 100644 index 0000000..1fa5dc4 Binary files /dev/null and b/target/test-classes/com/manumiguezz/crudapplication/CrudapplicationApplicationTests.class differ