+
+ Welcome {{ authStore.user.username }} !
+
+
+ Welcome to PersonalLibraryManager. Please login ou sign in to continue.
+
+
+
+
+
diff --git a/front/src/api.js b/front/src/api.js
index cb1957e..39608d3 100644
--- a/front/src/api.js
+++ b/front/src/api.js
@@ -24,8 +24,8 @@ function useFetch(url) {
return { data, error }
}
-export function getBooks() {
- return useFetch(baseUrl + '/books');
+export function getMyBooks() {
+ return useFetch(baseUrl + '/mybooks');
}
export function postBook(book) {
diff --git a/front/src/main.js b/front/src/main.js
index 99a86a0..cdbf9b8 100644
--- a/front/src/main.js
+++ b/front/src/main.js
@@ -6,10 +6,12 @@ import BooksBrowser from './BooksBrowser.vue'
import AddBook from './AddBook.vue'
import SignUp from './SignUp.vue'
import LogIn from './LogIn.vue'
+import Home from './Home.vue'
const routes = [
- { path: '/', component: BooksBrowser },
+ { path: '/', component: Home },
+ { path: '/books', component: BooksBrowser },
{ path: '/add', component: AddBook },
{ path: '/signup', component: SignUp },
{ path: '/login', component: LogIn },
diff --git a/internal/api/dto.go b/internal/api/dto.go
index 0d65765..a80cb46 100644
--- a/internal/api/dto.go
+++ b/internal/api/dto.go
@@ -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"`
+}
diff --git a/internal/api/mapper.go b/internal/api/mapper.go
index a2ca2c9..000c1eb 100644
--- a/internal/api/mapper.go
+++ b/internal/api/mapper.go
@@ -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,
}
}
diff --git a/internal/api/routes.go b/internal/api/routes.go
index d2881f2..48bc88d 100644
--- a/internal/api/routes.go
+++ b/internal/api/routes.go
@@ -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) {
diff --git a/internal/db/init.go b/internal/db/init.go
index d729392..d28e026 100644
--- a/internal/db/init.go
+++ b/internal/db/init.go
@@ -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 != "" {
diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go
index fe950b1..5d25fbe 100644
--- a/internal/middleware/auth.go
+++ b/internal/middleware/auth.go
@@ -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 {
diff --git a/internal/model/book.go b/internal/model/book.go
index d7dc7a9..4701116 100644
--- a/internal/model/book.go
+++ b/internal/model/book.go
@@ -6,5 +6,4 @@ type Book struct {
gorm.Model
Title string `json:"title" gorm:"not null"`
Author string `json:"author"`
- Rating int `json:"rating"`
}
diff --git a/internal/model/user.go b/internal/model/user.go
index 0f5ae43..9bde43e 100644
--- a/internal/model/user.go
+++ b/internal/model/user.go
@@ -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
}
diff --git a/internal/model/userbook.go b/internal/model/userbook.go
new file mode 100644
index 0000000..4a435ec
--- /dev/null
+++ b/internal/model/userbook.go
@@ -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
+}
diff --git a/main.go b/main.go
index 0084601..087c406 100644
--- a/main.go
+++ b/main.go
@@ -26,8 +26,8 @@ func setup(config *config.Config) *gin.Engine {
r := gin.Default()
r.Use(cors.New(configureCors())) // All origins allowed by default
r.Use(middleware.Auth())
- r.GET("/books", func(c *gin.Context) {
- api.GetBooksHanderl(c, db)
+ r.GET("/mybooks", func(c *gin.Context) {
+ api.GetMyBooksHanderl(c, db)
})
r.POST("/book", func(c *gin.Context) {
api.PostBookHandler(c, db)