Skip to content

Latest commit

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

📚 Book Management API

Welcome to the Book Management API! A robust and scalable RESTful API built with Go, following SOLID principles and clean architecture patterns.

GoREST APIClean Architecture

✨ About

This API was designed to demonstrate modern Go development practices with a focus on maintainability, testability, and scalability. The project showcases:

  • Clean Architecture - Separation of concerns with domain-driven design
  • SOLID Principles - Following best practices for object-oriented design
  • Comprehensive Testing - Unit tests with mocks for reliable code
  • Error Handling - Custom error types for better error management
  • Middleware Support - Logging and recovery middleware
  • Type Safety - Leveraging Go's strong typing system

Architecture

cmd/
api/
main.go # Entry point and DI container
internal/
domain/
book.go # Domain entity
repository.go # Repository interface
handler/
book_handler.go # HTTP handlers
middleware/
logger.go # Logging middleware
recovery.go # Panic recovery middleware
repository/
book_repository.go # In-memory implementation
service/
book_service.go # Business rules

SOLID Principles Applied

S - Single Responsibility Principle

Each component has a single well-defined responsibility:

// book_service.go - ONLY business logicfunc (s*BookService) CreateBook(title, author, isbnstring) (*domain.Book, error) {
book, err:=domain.NewBook(title, author, isbn)
iferr!=nil {
returnnil, err
}
returnbook, s.repository.Create(book)
}

O - Open/Closed Principle

Code is open for extension but closed for modification:

// Easy to add new implementationstypePostgreSQLBookRepositorystruct { ... }
typeMongoBookRepositorystruct { ... }
// All implement the same interfacetypeBookRepositoryinterface {
Create(book*Book) errorFindByID(idstring) (*Book, error)
// ...
}

L - Liskov Substitution Principle

Any BookRepository implementation can replace another without breaking the code:

// Both work perfectly in BookServicebookService:=service.NewBookService(repository.NewInMemoryBookRepository())
bookService:=service.NewBookService(repository.NewPostgreSQLRepository())

I - Interface Segregation Principle

Lean interfaces with only necessary methods:

// Specific interface for book operationstypeBookRepositoryinterface {
Create(book*Book) errorFindByID(idstring) (*Book, error)
FindAll() ([]*Book, error)
Update(book*Book) errorDelete(idstring) error
}

D - Dependency Inversion Principle

High-level modules don't depend on low-level modules, both depend on abstractions:

// Service depends on the INTERFACE, not the concrete implementationtypeBookServicestruct {
repository domain.BookRepository// Interface, not concrete struct
}
// Manual dependency injection in main.gobookRepository:=repository.NewInMemoryBookRepository()
bookService:=service.NewBookService(bookRepository)
bookHandler:=handler.NewBookHandler(bookService)

API Endpoints

Create Book

POST /books
Content-Type: application/json
{
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "978-0132350884"
}

List All Books

GET /books

Get Book by ID

GET /books/{id}

Update Book

PUT /books/{id}
Content-Type: application/json
{
"title": "Clean Architecture",
"author": "Robert C. Martin",
"isbn": "978-0134494166"
}

Delete Book

DELETE /books/{id}

The API will be available at http://localhost:8080

Usage Examples

# Create a book
curl -X POST http://localhost:8080/books \
-H "Content-Type: application/json" \
-d '{ "title": "Domain-Driven Design", "author": "Eric Evans", "isbn": "978-0321125215" }'# List all books
curl http://localhost:8080/books
# Get specific book
curl http://localhost:8080/books/{id}
# Update book
curl -X PUT http://localhost:8080/books/{id} \
-H "Content-Type: application/json" \
-d '{ "title": "DDD Distilled", "author": "Vaughn Vernon", "isbn": "978-0134434421" }'# Delete book
curl -X DELETE http://localhost:8080/books/{id}

Response Structure

Success

{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "978-0132350884",
"created_at": "2026-01-10T10:30:00Z",
"updated_at": "2026-01-10T10:30:00Z"
}

Error

{
"error": "book not found"
}

📦 Installation

# Clone repository 
git clone https://github.com/Luuan11/solid.git # Install dependencies
go mod tidy
# Run application
go run cmd/api/main.go

Made with 💜 by Luan Fernando.

About

📘Simple book management following SOLID principles

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages