Go CRUD Application with Fiber and MongoDB
The Go CRUD Application is a simple yet robust project built using the Fiber web framework and MongoDB database. It facilitates basic CRUD operations (Create, Read, Update, Delete) for managing student records. Fork, customize, and extend to meet your specific needs with the flexibility of Go and Fiber.
Table of Contents
- Prerequisites
- Getting Started
- API Endpoints
- Automated Testing
- Sequence Diagram
- UML Diagram
- Contributing
Prerequisites
Before you begin, ensure you have the following installed on your machine:
Getting Started
-
Clone the repository:
git clone https://github.com/Ankurallin1/GOFR-Project cd GOFR-Project
-
Download Dependencies:
go mod download
-
Verify Dependencies:
cat go.sum
-
Run Project
go run main.go
-
Open Server
http://localhost:8080/students
API Endpoints
The application provides RESTful API endpoints for CRUD operations on student records.
Base URL
The base URL for all endpoints is:
List of Endpoints
1. Get All Students
-
Endpoint:
GET /students
-
Description:
- Retrieves a list of all students.
-
Example Response:
[ { "id": "657ad262660b9c2342474c33", "name": "ankur", "email": "ankursri@gmail.com", "collegeName": "JUET", "enrollmentNo": "201B049" }, { "id": "657be72ea13c844c358f83b2", "name": "John Doe", "email": "john.doe@example.com", "collegeName": "Sample College", "enrollmentNo": "12345" } ]
2. Get Student by ID
-
Endpoint:
GET /students/:id
-
Parameters:
id
: The unique identifier for the student.
-
Description:
- Retrieves information about a specific student based on the provided ID.
-
Example Response:
[ { "id": "657ad262660b9c2342474c33", "name": "ankur", "email": "ankursri@gmail.com", "collegeName": "JUET", "enrollmentNo": "201B049" } ]
3. Create a New Student
-
Endpoint:
POST /students
-
Request Body:
[ { "name": "John Doe", "email": "john.doe@example.com", "collegeName": "Sample College", "enrollmentNo": "12345" } ]
-
Description:
- Creates a new student record.
-
Example Response:
{ "id": "generated-student-id", "name": "John Doe", "email": "john.doe@example.com", "collegeName": "Sample College", "enrollmentNo": "12345" }
4. Update Student by ID
-
Endpoint:
PUT /students/:id
-
Parameters:
id
: The unique identifier for the student.
-
Request Body:
[ { "name": "Updated Name", "email": "updated.email@example.com", "collegeName": "Updated College", "enrollmentNo": "54321" } ]
-
Description:
- Updates information about a specific student based on the provided ID.
-
Example Response:
"Student updated successfully"
5. Delete Student by ID
-
Endpoint:
DELETE /students/:id
-
Parameters:
id
: The unique identifier for the student.
-
Description:
- Deletes a specific student record based on the provided ID.
-
Example Response:
"Student deleted successfully"
Automated Testing
The project includes test cases that can be executed using Go's testing framework.
Changes Required
-
Change the
id
in below lines ofmain_test.go
:- Line 79 for
UpdateStudent
req := httptest.NewRequest(http.MethodPut, "/students/{id}", bytes.NewReader(updatedStudentJSON))
- Line 91 for
DeleteStudent
req := httptest.NewRequest(http.MethodDelete, "/students/{id}", nil)
- Line 79 for
Running Tests
-
Use the following command to run the tests:
go test
Sequence Diagram
UML Diagram
Contributing
Feel free to contribute to this project. Fork the repository, make your changes, and submit a pull request.