Skip to content

Files

Latest commit

1ca343c Β· Nov 6, 2024

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 6, 2024
Nov 4, 2024
Oct 20, 2024
Nov 4, 2024
Aug 30, 2024
Aug 30, 2024
Oct 20, 2024
Oct 20, 2024
Aug 29, 2024
Aug 29, 2024
Oct 21, 2024

Employee Management System - Backend

Overview

The backend of the Employee Management System is built using Spring Boot, a powerful Java framework designed to simplify the development of production-ready applications. This backend provides a RESTful API for managing employee and department data, connecting to both MySQL and MongoDB databases.

Features

  • RESTful API: Provides endpoints for CRUD operations on employees and departments.
  • Data Initialization: Includes sample data for departments and employees.
  • Integration: Connects to both MySQL and MongoDB databases.
  • Exception Handling: Custom error handling for not found resources.

Technologies

  • Spring Boot: Framework for building production-ready applications with Java.
  • MySQL: Relational database for structured data storage.
  • MongoDB: NoSQL database for non-relational data storage.

File Structure

employee-management-app
β”‚
β”œβ”€β”€ docker-compose.yaml
β”‚
β”œβ”€β”€ backend
β”‚   β”œβ”€β”€ src
β”‚   β”‚   β”œβ”€β”€ main
β”‚   β”‚   β”‚   β”œβ”€β”€ java
β”‚   β”‚   β”‚   β”‚   └── com
β”‚   β”‚   β”‚   β”‚       └── example
β”‚   β”‚   β”‚   β”‚           └── employeemanagement
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ EmployeeManagementApplication.java
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ config
β”‚   β”‚   β”‚   β”‚               β”‚   └── CorsConfig.java
β”‚   β”‚   β”‚   β”‚               β”‚   └── DataInitializer.java
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ controller
β”‚   β”‚   β”‚   β”‚               β”‚   β”œβ”€β”€ DepartmentController.java
β”‚   β”‚   β”‚   β”‚               β”‚   └── EmployeeController.java
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ model
β”‚   β”‚   β”‚   β”‚               β”‚   β”œβ”€β”€ Department.java
β”‚   β”‚   β”‚   β”‚               β”‚   └── Employee.java
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ repository
β”‚   β”‚   β”‚   β”‚               β”‚   β”œβ”€β”€ DepartmentRepository.java
β”‚   β”‚   β”‚   β”‚               β”‚   └── EmployeeRepository.java
β”‚   β”‚   β”‚   β”‚               β”œβ”€β”€ service
β”‚   β”‚   β”‚   β”‚               β”‚   └── DataInitializer.java
β”‚   β”‚   β”‚   β”‚               └── exception
β”‚   β”‚   β”‚   β”‚                   └── ResourceNotFoundException.java
β”‚   β”‚   β”‚   └── resources
β”‚   β”‚   β”‚       β”œβ”€β”€ application.properties
β”‚   β”‚   β”‚       └── data.sql
β”‚   β”‚   └── test
β”‚   β”‚       └── java
β”‚   β”‚           └── com
β”‚   β”‚               └── example
β”‚   β”‚                   └── employeemanagement
β”‚   β”‚                       └── EmployeeManagementApplicationTests.java
β”‚   β”œβ”€β”€ .gitignore
β”‚   └── pom.xml
β”‚   └── compose.yaml
β”‚
└── frontend
    └── (frontend code)

Setup Instructions

Important: Java 11 is required to run this project.

1. Clone the Repository

git clone https://github.com/hoangsonww/Employee-Management-Fullstack-App.git
cd Employee-Management/backend

2. Install Dependencies

Ensure you have Maven and Java JDK installed. Run the following command to install the required dependencies:

mvn install -DskipTests

3. Configure the Application

Update src/main/resources/application.properties with your database configuration:

# MySQL Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/employee_management
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

# MongoDB Configuration
spring.data.mongodb.uri=mongodb://localhost:27017/employee_management

Ensure the databases are set up as expected and the URLs, usernames, and passwords match your local or remote database setup.

4. Start the Backend Server

Run the following command to start the Spring Boot application:

mvn spring-boot:run

The backend server will be available at http://localhost:8080.

5. Access the API Endpoints

Here are some example API endpoints you can use to interact with the backend:

  • Get All Employees:

    curl -X GET http://localhost:8080/api/employees
  • Get Employee by ID:

    curl -X GET http://localhost:8080/api/employees/1
  • Create a New Employee:

    curl -X POST http://localhost:8080/api/employees -H "Content-Type: application/json" -d '{"firstName": "John", "lastName": "Doe", "email": "[email protected]", "departmentId": 1}'
  • Update an Employee:

    curl -X PUT http://localhost:8080/api/employees/1 -H "Content-Type: application/json" -d '{"firstName": "John", "lastName": "Doe", "email": "[email protected]", "departmentId": 1}'
  • Delete an Employee:

    curl -X DELETE http://localhost:8080/api/employees/1
  • Get All Departments:

    curl -X GET http://localhost:8080/api/departments
  • Get Department by ID:

    curl -X GET http://localhost:8080/api/departments/1

6. Data Initialization

The DataInitializer.java class is used to preload sample data into the database. This is particularly useful for development and testing.

7. Running Tests

To run the unit and integration tests, use:

mvn test

Detailed File Descriptions

EmployeeManagementApplication.java

The main class that serves as the entry point for the Spring Boot application.

DepartmentController.java and EmployeeController.java

REST controllers for handling HTTP requests related to departments and employees, respectively.

Department.java and Employee.java

Entity classes representing the departments and employees tables in the MySQL database.

DepartmentRepository.java and EmployeeRepository.java

Repository interfaces for performing CRUD operations on the departments and employees entities.

DataInitializer.java

A service class that initializes the database with sample data upon application startup.

ResourceNotFoundException.java

A custom exception class used for handling cases where requested resources are not found.

application.properties

Configuration file for Spring Boot, including database connection settings.

data.sql

SQL script for preloading sample data into the MySQL database.

Swagger API Documentation

The backend API is documented using Swagger, which provides a user-friendly interface for exploring the available endpoints. To access the Swagger UI, navigate to http://localhost:8080/swagger-ui.html after starting the backend server.

If you have everything set up correctly, you should see the Swagger UI with a list of available endpoints and the ability to test them directly from the browser:

Swagger UI

Troubleshooting

Common Issues

  • Could not autowire Errors: Ensure all Spring Boot components (controllers, services, repositories) are correctly annotated and located in the appropriate package structure.

  • Exception opening socket for MongoDB: Verify that MongoDB is running and accessible at localhost:27017. Check MongoDB logs for connection issues.

  • SQLSyntaxErrorException: Check the data.sql script and ensure the MySQL database schema matches the expected structure.

  • Port Already in Use: If the default port 8080 is already in use, change the port in application.properties or terminate the conflicting process.

  • CORS Error: If you encounter CORS issues, ensure that the CorsConfig.java class is correctly configured.

  • Build failed: If the Maven build fails, check if you are using Java 11 and have the necessary dependencies installed. Also, check the error logs for more details.

Contributing

If you'd like to contribute to the backend development, please fork the repository and submit a pull request with your changes. Ensure that you follow the project's coding standards and include relevant tests for new features.

License

This project is licensed under the MIT License. See the LICENSE file for details.

More Information

For more information about this project, please refer to the comprehensive documentation.

Contact

For any questions or issues, please contact [email protected].