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")
|
||||
}
|
||||
@@ -9,16 +9,15 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port string `toml:"port" comment:"The port to listen on for the server."`
|
||||
DemoDataPath string `toml:"demo_data_path" comment:"The path to the sql file to load for demo data."`
|
||||
|
||||
Port string `toml:"port" comment:"The port to listen on for the server."`
|
||||
DatabaseFilePath string `toml:"database_file_path" comment:"Path to sqlite database file."`
|
||||
DemoDataPath string `toml:"demo_data_path" comment:"The path to the sql file to load for demo data."`
|
||||
}
|
||||
|
||||
func defaultConfig() Config {
|
||||
return Config{Port: "8080", DemoDataPath: ""}
|
||||
return Config{Port: "8080", DatabaseFilePath: "plm.db", DemoDataPath: ""}
|
||||
}
|
||||
|
||||
|
||||
func LoadConfig(configPath string) Config {
|
||||
f, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
@@ -11,12 +10,10 @@ import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
)
|
||||
|
||||
func Initdb(databaseDir string, demoDataPath string) *gorm.DB {
|
||||
createDbFolderIfMissing(databaseDir)
|
||||
func Initdb(databasePath string, demoDataPath string) *gorm.DB {
|
||||
db, err := gorm.Open(
|
||||
sqlite.Open(
|
||||
fmt.Sprintf(
|
||||
"%s/plm.db", databaseDir)), &gorm.Config{})
|
||||
databasePath), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -38,20 +35,3 @@ func migrateSchema(db *gorm.DB, demoDataPath string) {
|
||||
}
|
||||
db.Exec(string(data))
|
||||
}
|
||||
|
||||
func createDbFolderIfMissing(databaseDir string) {
|
||||
_, openFileErr := os.Open(databaseDir)
|
||||
if os.IsNotExist(openFileErr) {
|
||||
createNonExistingDbFolder(databaseDir)
|
||||
} else if openFileErr != nil {
|
||||
log.Fatal(openFileErr)
|
||||
}
|
||||
}
|
||||
|
||||
func createNonExistingDbFolder(databaseDir string) {
|
||||
log.Printf("Creating missing folder %s\n", databaseDir)
|
||||
err := os.MkdirAll(databaseDir, 0700)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user