Second commit
added few test, first api to add book
This commit is contained in:
7
internal/api/dto.go
Normal file
7
internal/api/dto.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package api
|
||||
|
||||
type bookPostCreate struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Author string `json:"author"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
}
|
||||
9
internal/api/mapper.go
Normal file
9
internal/api/mapper.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import "git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
|
||||
func (b bookPostCreate) toBook() model.Book {
|
||||
return model.Book{Title: b.Title,
|
||||
Author: b.Author,
|
||||
Rating: b.Rating}
|
||||
}
|
||||
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