first commit
This commit is contained in:
32
handlers/book_handler.go
Normal file
32
handlers/book_handler.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user