Second commit
added few test, first api to add book
This commit is contained in:
27
internal/api/routes.go
Normal file
27
internal/api/routes.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GetBooksHanderl(c *gin.Context, db *gorm.DB) {
|
||||
var books []model.Book
|
||||
db.Model(&model.Book{}).Find(&books)
|
||||
c.JSON(http.StatusOK, books)
|
||||
}
|
||||
|
||||
func PostBookHandler(c *gin.Context, db *gorm.DB) {
|
||||
var book bookPostCreate
|
||||
err := c.ShouldBindJSON(&book)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
bookDb := book.toBook()
|
||||
db.Model(&model.Book{}).Save(&bookDb)
|
||||
c.String(200, "Success")
|
||||
}
|
||||
Reference in New Issue
Block a user