Integrate users and books

- new table userbook linking users and book
- moved rating to users book
- updated demo data and tests
- updated front to hide routes from non connected users
This commit is contained in:
2025-10-05 16:14:53 +02:00
parent f18e6a3ba8
commit cb1f974f02
16 changed files with 143 additions and 73 deletions

View File

@@ -3,7 +3,6 @@ package api
type bookPostCreate struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
}
type userSignup struct {
@@ -15,3 +14,9 @@ type userLogin struct {
Username string `json:"username" binding:"required,min=2,max=20"`
Password string `json:"password" binding:"required,min=6,max=100"`
}
type bookUserGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
}

View File

@@ -9,6 +9,13 @@ func (b bookPostCreate) toBook() model.Book {
return model.Book{
Title: b.Title,
Author: b.Author,
}
}
func fromUserBookDb(b *model.UserBook) bookUserGet {
return bookUserGet{
Title: b.Book.Title,
Author: b.Book.Author,
Rating: b.Rating,
}
}

View File

@@ -13,10 +13,14 @@ import (
"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 GetMyBooksHanderl(c *gin.Context, db *gorm.DB) {
var userbooks []model.UserBook
db.Preload("Book").Find(&userbooks)
var booksDto []bookUserGet
for _, userbook := range userbooks {
booksDto = append(booksDto, fromUserBookDb(&userbook))
}
c.JSON(http.StatusOK, booksDto)
}
func PostBookHandler(c *gin.Context, db *gorm.DB) {