Implement a RESTful API for a book management system using Go. The API should allow users to perform CRUD operations on books, with data persistence using an in-memory database. This challenge tests your ability to design and implement a complete web service, handle HTTP requests and responses, and manage data persistence.
-
Implement a RESTful API with the following endpoints:
GET /api/books: Get all booksGET /api/books/{id}: Get a specific book by IDPOST /api/books: Create a new bookPUT /api/books/{id}: Update an existing bookDELETE /api/books/{id}: Delete a bookGET /api/books/search?author={author}: Search books by authorGET /api/books/search?title={title}: Search books by title
-
Implement a
Bookstruct with the following fields:ID: Unique identifier for the bookTitle: Title of the bookAuthor: Author of the bookPublishedYear: Year the book was publishedISBN: International Standard Book NumberDescription: Brief description of the book
-
Implement an in-memory database (using Go data structures) to store books.
-
Implement proper error handling and status codes:
- 200 OK: Successful GET, PUT, DELETE
- 201 Created: Successful POST
- 400 Bad Request: Invalid input
- 404 Not Found: Resource not found
- 500 Internal Server Error: Server-side error
-
Implement input validation for all endpoints.
-
The API should return responses in JSON format.
// Book represents a book in the database
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
PublishedYear int `json:"published_year"`
ISBN string `json:"isbn"`
Description string `json:"description"`
}
// BookRepository defines the operations for book data access
type BookRepository interface {
GetAll() ([]*Book, error)
GetByID(id string) (*Book, error)
Create(book *Book) error
Update(id string, book *Book) error
Delete(id string) error
SearchByAuthor(author string) ([]*Book, error)
SearchByTitle(title string) ([]*Book, error)
}
// BookService defines the business logic for book operations
type BookService interface {
GetAllBooks() ([]*Book, error)
GetBookByID(id string) (*Book, error)
CreateBook(book *Book) error
UpdateBook(id string, book *Book) error
DeleteBook(id string) error
SearchBooksByAuthor(author string) ([]*Book, error)
SearchBooksByTitle(title string) ([]*Book, error)
}
// BookHandler handles HTTP requests for book operations
type BookHandler struct {
Service BookService
}
// Implement appropriate methods on BookHandler to handle HTTP requestsYour solution should follow a clean architecture with separation of concerns:
challenge-9/
├── submissions/
│ └── yourusername/
│ └── solution-template.go
├── api/
│ ├── handlers/
│ │ └── book_handler.go
│ └── middleware/
│ └── logger.go
├── domain/
│ └── models/
│ └── book.go
├── repository/
│ └── book_repository.go
├── service/
│ └── book_service.go
└── main.go
Your solution should handle the following test scenarios:
- Get all books when the database is empty
- Create a new book with valid data
- Create a new book with invalid data (missing required fields)
- Get a specific book by ID when it exists
- Get a specific book by ID when it doesn't exist
- Update a book with valid data
- Update a book that doesn't exist
- Delete a book that exists
- Delete a book that doesn't exist
- Search for books by author with results
- Search for books by title with no results
- Fork the repository.
- Clone your fork to your local machine.
- Create a directory named after your GitHub username inside
challenge-9/submissions/. - Copy the
solution-template.gofile into your submission directory. - Implement the required components.
- Test your solution locally by running the test file.
- Commit and push your code to your fork.
- Create a pull request to submit your solution.
Run the following command in the challenge-9/ directory:
go test -vFor those seeking extra challenges:
- Add authentication and authorization using JWT
- Implement pagination for the
GET /api/booksendpoint - Add rate limiting middleware
- Implement filtering and sorting options for book queries
- Add swagger documentation for the API