library/handlers/book_handler.go
2024-12-09 19:52:07 -05:00

33 lines
736 B
Go

// handlers/book_handler.go
package handlers
import (
"net/http"
"gitea.ghost.tel/knight/library/database"
"gitea.ghost.tel/knight/library/models"
"github.com/gin-gonic/gin"
)
func GetBooks(c *gin.Context) {
var books []models.Book
result := database.DB.Find(&books)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
c.JSON(http.StatusOK, books)
}
func SearchBooks(c *gin.Context) {
title := c.Query("title")
var books []models.Book
result := database.DB.Where("title LIKE ?", "%"+title+"%").Find(&books)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
c.JSON(http.StatusOK, books)
}