39 lines
1000 B
Go
39 lines
1000 B
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
func GetMyBooksHanderl(ac appcontext.AppContext) {
|
|
var userbooks []model.UserBook
|
|
user, err := ac.GetAuthenticatedUser()
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
ac.Db.Preload("Book").Where("user_id = ?", user.ID).Find(&userbooks)
|
|
booksDto := make([]bookUserGet, 0)
|
|
for _, userbook := range userbooks {
|
|
booksDto = append(booksDto, userBookDbToWs(&userbook))
|
|
}
|
|
ac.C.JSON(http.StatusOK, booksDto)
|
|
}
|
|
|
|
func userBookDbToWs(b *model.UserBook) bookUserGet {
|
|
return bookUserGet{
|
|
Title: b.Book.Title,
|
|
Author: b.Book.Author,
|
|
Rating: b.Rating,
|
|
}
|
|
}
|