first commit

This commit is contained in:
2024-12-09 19:52:07 -05:00
commit e092b0d172
7 changed files with 214 additions and 0 deletions

32
handlers/book_handler.go Normal file
View File

@@ -0,0 +1,32 @@
// 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)
}