Manage display of book covers
This commit is contained in:
@@ -7,16 +7,10 @@ import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type bookGet struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
}
|
||||
|
||||
func GetBookHandler(ac appcontext.AppContext) {
|
||||
bookId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
@@ -33,13 +27,8 @@ func GetBookHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
var book bookGet
|
||||
query := ac.Db.Model(&model.Book{})
|
||||
query = query.Select("books.title, books.author, user_books.rating, user_books.read")
|
||||
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", user.ID)
|
||||
query = query.Where("books.id = ?", bookId)
|
||||
res := query.First(&book)
|
||||
if res.Error != nil {
|
||||
book, queryErr := query.FetchBookGet(ac.Db, user.ID, bookId)
|
||||
if queryErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type bookPostCreate struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
CoverID uint `json:"coverId"`
|
||||
}
|
||||
|
||||
func PostBookHandler(ac appcontext.AppContext) {
|
||||
@@ -18,6 +19,11 @@ func PostBookHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
err = myvalidator.ValidateId(ac.Db, book.CoverID, &model.StaticFile{})
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||
if fetchUserErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -33,9 +39,13 @@ func PostBookHandler(ac appcontext.AppContext) {
|
||||
}
|
||||
|
||||
func bookWsToDb(b bookPostCreate, user *model.User) model.Book {
|
||||
return model.Book{
|
||||
book := model.Book{
|
||||
Title: b.Title,
|
||||
Author: b.Author,
|
||||
AddedBy: *user,
|
||||
}
|
||||
if b.CoverID > 0 {
|
||||
book.CoverID = b.CoverID
|
||||
}
|
||||
return book
|
||||
}
|
||||
|
||||
@@ -2,33 +2,18 @@ package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
)
|
||||
|
||||
type bookSearchGet struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
searchterm := ac.C.Param("searchterm")
|
||||
var booksDb []model.Book
|
||||
ac.Db.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(searchterm)+"%").Find(&booksDb)
|
||||
books := make([]bookSearchGet, 0)
|
||||
for _, b := range booksDb {
|
||||
books = append(books, bookDbToWs(&b))
|
||||
books, err := query.FetchBookSearchGet(ac.Db, searchterm)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, books)
|
||||
}
|
||||
|
||||
func bookDbToWs(b *model.Book) bookSearchGet {
|
||||
return bookSearchGet{
|
||||
Title: b.Title,
|
||||
Author: b.Author,
|
||||
ID: b.ID,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,39 +4,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
)
|
||||
|
||||
type bookUserGet struct {
|
||||
BookID uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
Read bool `json:"read" binding:"boolean"`
|
||||
}
|
||||
|
||||
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{
|
||||
BookID: b.BookID,
|
||||
Title: b.Book.Title,
|
||||
Author: b.Book.Author,
|
||||
Rating: b.Rating,
|
||||
Read: b.Read,
|
||||
}
|
||||
userbooks, err := query.FetchBookUserGet(ac.Db, user.ID)
|
||||
ac.C.JSON(http.StatusOK, userbooks)
|
||||
}
|
||||
|
||||
@@ -4,21 +4,33 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type fileInfoPost struct {
|
||||
FileID uint `json:"fileId"`
|
||||
FilePath string `json:"filepath"`
|
||||
}
|
||||
|
||||
func PostUploadBookCoverHandler(ac appcontext.AppContext) {
|
||||
file, err := ac.C.FormFile("file")
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
filepath := file.Filename
|
||||
err = ac.C.SaveUploadedFile(file, ac.Config.ImageFolderPath+"/"+filepath)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
staticFile, saveErr := fileutils.SaveStaticFile(&ac, file)
|
||||
if saveErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, saveErr)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"filepath": "/bookcover/" + filepath})
|
||||
ac.C.JSON(http.StatusOK, staticFileDbToWs(&staticFile))
|
||||
}
|
||||
|
||||
func staticFileDbToWs(f *model.StaticFile) fileInfoPost {
|
||||
return fileInfoPost{
|
||||
FileID: f.ID,
|
||||
FilePath: fileutils.GetWsLinkPrefix() + f.Path,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user