Simplify query code to get start read date
This commit is contained in:
@@ -22,7 +22,7 @@ type BookGet struct {
|
|||||||
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
|
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
|
||||||
var book BookGet
|
var book BookGet
|
||||||
query := db.Model(&model.Book{})
|
query := db.Model(&model.Book{})
|
||||||
query = query.Select("books.title, books.author, books.summary, user_books.rating, user_books.read, user_books.want_read, user_books.start_read_date, " + selectStaticFilesPath())
|
query = query.Select("books.title, books.author, books.summary, user_books.rating, user_books.read, user_books.want_read, DATE(user_books.start_read_date) as start_read_date, " + selectStaticFilesPath())
|
||||||
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", userId)
|
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", userId)
|
||||||
query = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
query = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
||||||
query = query.Where("books.id = ?", bookId)
|
query = query.Where("books.id = ?", bookId)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package routes
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
@@ -33,22 +32,5 @@ func GetBookHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = fixBookGetDateFields(&book)
|
|
||||||
if err != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ac.C.JSON(http.StatusOK, book)
|
ac.C.JSON(http.StatusOK, book)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fixBookGetDateFields(book *query.BookGet) error {
|
|
||||||
if book.StartReadDate == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
startDate, err := time.Parse(time.RFC3339, book.StartReadDate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
book.StartReadDate = startDate.Format(time.DateOnly)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user