import { Hono } from 'hono';
import { Content } from '../components/Layout';
const book = new Hono();
// Get all books list
book.get('/', (c) => {
const props = {
name: '📚 Book Library',
siteData: {
title: 'Books - Hono App',
},
children: (
Browse our collection of books
A comprehensive guide to software development
Essential JavaScript programming techniques
Writing maintainable and readable code
),
};
return c.html(Content(props));
});
// Get specific book
book.get('/:id', (c) => {
const id = c.req.param('id');
const books: Record = {
'1': { title: 'The Art of Programming', author: 'John Doe', description: 'A comprehensive guide to software development principles and practices.' },
'2': { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford', description: 'Essential JavaScript programming techniques and best practices.' },
'3': { title: 'Clean Code', author: 'Robert C. Martin', description: 'A handbook of agile software craftsmanship.' }
};
const bookData = books[id];
if (!bookData) {
const props = {
name: 'Book Not Found',
siteData: {
title: 'Book Not Found',
},
children: (
The book with ID {id} was not found.
),
};
return c.html(Content(props), 404);
}
const props = {
name: `📖 ${bookData.title}`,
siteData: {
title: `${bookData.title} - Book Details`,
},
children: (
Author: {bookData.author}
Book ID: {id}
Description: {bookData.description}
),
};
return c.html(Content(props));
});
// Create new book (API endpoint)
book.post('/', async (c) => {
const body = await c.req.json();
return c.json({
success: true,
message: 'Book created successfully',
book: {
id: Math.random().toString(36).substr(2, 9),
title: body.title || 'Untitled',
author: body.author || 'Unknown',
createdAt: new Date().toISOString()
}
});
});
export default book;