Skip to content

pathakanu/student-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Student API Documentation

Overview

The Student API is a RESTful service for managing student records. It allows you to create, retrieve, update, and delete student data stored in a SQLite database. The API is built using Go and follows best practices for configuration, error handling, and validation.


Folder Structure

student-api/
├── cmd/
│   └── students-api/
│       └── main.go
├── internal/
│   ├── config/
│   ├── http/
│   │   └── handlers/
│   │       └── student/
│   │           └── students.go
│   ├── response/
│   ├── storage/
│   │   └── sqlite/
│   └── types/
└── go.mod / go.sum

Main Components

1. Entry Point

File: cmd/students-api/main.go

  • Loads configuration.
  • Initializes SQLite storage.
  • Sets up HTTP routes for student operations.
  • Handles graceful shutdown on OS signals.

2. Configuration

File: internal/config/config.go

  • Loads environment variables and configures the server/database.

3. HTTP Handlers

File: internal/http/handlers/student/students.go

Defines handlers for:

  • POST /api/students: Create a new student.
  • GET /api/students/{id}: Get a student by ID.
  • GET /api/students: List all students.
  • PUT /api/students/{id}: Update a student.
  • DELETE /api/students/{id}: Delete a student.

Each handler:

  • Logs the request.
  • Validates input using Go's validator package.
  • Returns JSON responses with appropriate status codes.

4. Storage Layer

File: internal/storage/sqlite/sqlite.go

  • Implements CRUD operations for students using SQLite.
  • Provides methods like New, GetStudentById, GetStudents, UpdateStudent, DeleteStudent.

5. Types

File: internal/types/types.go

Defines the Student struct:

type Student struct {
    ID    string `json:"id" validate:"required"`
    Name  string `json:"name" validate:"required,min=2,max=100"`
    Age   int    `json:"age" validate:"required,min=1,max=150"`
    Grade string `json:"grade" validate:"required,oneof=A B C D E F"`
}
  • Validation: Only grades A, B, C, D, E, F are accepted.

6. Response Utilities

File: internal/response/response.go

  • Utility functions for writing JSON responses and error messages.

API Endpoints

Create Student

  • POST /api/students
  • Body:
    {
      "id": "1",
      "name": "Anurag",
      "age": 27,
      "grade": "A"
    }
  • Validation: All fields required. Grade must be one of A B C D E F.

Get Student by ID

  • GET /api/students/{id}

List Students

  • GET /api/students

Update Student

  • PUT /api/students/{id}
  • Body: Same as create.

Delete Student

  • DELETE /api/students/{id}

Error Handling

  • All errors are returned as JSON:
    {
      "status": "error",
      "error": "Error message"
    }
  • Validation errors specify the field and reason.

Graceful Shutdown

  • The server listens for SIGINT/SIGTERM and shuts down gracefully, closing the database connection.

Logging

  • Uses log/slog for structured logging of requests and errors.

Notes

  • Grade Validation: Only A, B, C, D, E, F are accepted. To allow A+, update the validation tag in types.Student.
  • Database: Uses SQLite for persistence.
  • Extensibility: The modular structure allows easy addition of new features.

Getting Started

  1. Configure environment variables (see internal/config).
  2. Run the server:
    go run cmd/students-api/main.go
  3. Use any HTTP client (e.g., curl, Postman) to interact with the API.

Contribution

  • Follow Go best practices.
  • Write unit tests for new features.
  • Document new endpoints and types.

For further details, review the code in each folder as described above.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages