add "reading" filter for books currently being read
This commit is contained in:
@@ -52,3 +52,22 @@ func TestGetWantReadBooksCountHandler_Demo(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, 2, c.Count)
|
||||
}
|
||||
|
||||
func TestGetReadingBooksCountHandler_Demo(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/mybooks/reading/count", nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var c countResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &c)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 2, c.Count)
|
||||
}
|
||||
|
||||
44
internal/apitest/get_reading_user_book_test.go
Normal file
44
internal/apitest/get_reading_user_book_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetReadingBooksHandler_Demo(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
books := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func TestGetReadingBooksHandler_Demo2(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemo2User(router)
|
||||
books := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
||||
assert.Equal(t, 0, len(books))
|
||||
}
|
||||
|
||||
func testGetReadingBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) []bookUserGet {
|
||||
u, err := url.Parse("/mybooks/reading")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if limit != "" {
|
||||
q := u.Query()
|
||||
q.Set("limit", limit)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
if offset != "" {
|
||||
q := u.Query()
|
||||
q.Set("offset", offset)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
return testGetbooksHandler(t, router, userToken, expectedCode, u.String())
|
||||
}
|
||||
@@ -97,11 +97,32 @@ func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) {
|
||||
var books []BookUserGet
|
||||
query := fetchReadingUserBookQuery(db, userId)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchReadingUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
query := fetchReadingUserBookQuery(db, userId)
|
||||
var count int64
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchReadUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := fetchUserBookGet(db, userId)
|
||||
query = query.Where("user_books.read IS TRUE")
|
||||
return query
|
||||
}
|
||||
func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := fetchUserBookGet(db, userId)
|
||||
query = query.Where("user_books.start_read_date IS NOT NULL")
|
||||
return query
|
||||
}
|
||||
|
||||
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) {
|
||||
var books []BookUserGet
|
||||
|
||||
48
internal/routes/userbookreadingget.go
Normal file
48
internal/routes/userbookreadingget.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetMyBooksReadingHandler(ac appcontext.AppContext) {
|
||||
user, err := ac.GetAuthenticatedUser()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
limit, err := ac.GetQueryLimit()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
offset, err := ac.GetQueryOffset()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbooks, err := query.FetchReadingUserBook(ac.Db, user.ID, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, userbooks)
|
||||
}
|
||||
|
||||
func GetMyBooksReadingCountHandler(ac appcontext.AppContext) {
|
||||
user, err := ac.GetAuthenticatedUser()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
count, err := query.FetchReadingUserBookCount(ac.Db, user.ID)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
||||
}
|
||||
@@ -30,6 +30,12 @@ func Setup(config *config.Config) *gin.Engine {
|
||||
r.GET("/mybooks/read/count", func(c *gin.Context) {
|
||||
routes.GetMyBooksReadCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
r.GET("/mybooks/reading", func(c *gin.Context) {
|
||||
routes.GetMyBooksReadingHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
r.GET("/mybooks/reading/count", func(c *gin.Context) {
|
||||
routes.GetMyBooksReadingCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
r.GET("/mybooks/wantread", func(c *gin.Context) {
|
||||
routes.GetMyBooksWantReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user