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:
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -20,6 +20,7 @@ func Initdb(databasePath string, demoDataPath string) *gorm.DB {
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&model.Book{})
|
||||
db.AutoMigrate(&model.User{})
|
||||
db.AutoMigrate(&model.UserBook{})
|
||||
var book model.Book
|
||||
queryResult := db.Limit(1).Find(&book)
|
||||
if queryResult.RowsAffected == 0 && demoDataPath != "" {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -20,7 +19,6 @@ func Auth() gin.HandlerFunc {
|
||||
|
||||
username, err := parseUserFromJwt(c)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized,
|
||||
gin.H{"error": "You must be logged in to access this resource."})
|
||||
} else {
|
||||
|
||||
@@ -6,5 +6,4 @@ type Book struct {
|
||||
gorm.Model
|
||||
Title string `json:"title" gorm:"not null"`
|
||||
Author string `json:"author"`
|
||||
Rating int `json:"rating"`
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"index;uniqueIndex"`
|
||||
Password string
|
||||
Name string `gorm:"index;uniqueIndex"`
|
||||
Password string
|
||||
UserBooks []UserBook
|
||||
}
|
||||
|
||||
12
internal/model/userbook.go
Normal file
12
internal/model/userbook.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// describes the relationship between a user and a book.
|
||||
type UserBook struct {
|
||||
gorm.Model
|
||||
UserID uint
|
||||
BookID uint
|
||||
Book Book
|
||||
Rating int
|
||||
}
|
||||
Reference in New Issue
Block a user