add end read date
This commit is contained in:
@@ -19,6 +19,7 @@ type fetchedBook struct {
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
StartReadDate string `json:"startReadDate"`
|
||||
EndReadDate string `json:"endReadDate"`
|
||||
}
|
||||
|
||||
func TestGetBook_Ok(t *testing.T) {
|
||||
|
||||
@@ -20,6 +20,44 @@ func TestPutReadUserBooks_NewReadOk(t *testing.T) {
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_NewReadDateOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "2025-10-20"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "2025-10-20", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetEndDate(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "null"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": false
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, false, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func testPutReadUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/book/"+bookId+"/read")
|
||||
}
|
||||
|
||||
@@ -16,4 +16,5 @@ type UserBook struct {
|
||||
Read bool
|
||||
WantRead bool
|
||||
StartReadDate *time.Time
|
||||
EndReadDate *time.Time
|
||||
}
|
||||
|
||||
@@ -16,13 +16,19 @@ type BookGet struct {
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
StartReadDate string `json:"startReadDate"`
|
||||
EndReadDate string `json:"endReadDate"`
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
|
||||
var book BookGet
|
||||
query := db.Model(&model.Book{})
|
||||
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())
|
||||
selectQueryString := "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, " +
|
||||
"DATE(user_books.end_read_date) AS end_read_date, " +
|
||||
selectStaticFilesPath()
|
||||
query = query.Select(selectQueryString)
|
||||
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.Where("books.id = ?", bookId)
|
||||
|
||||
@@ -33,11 +33,25 @@ func PutReadUserBookHandler(ac appcontext.AppContext) {
|
||||
|
||||
userbook.Read = read.Read
|
||||
|
||||
if read.EndDate != "" {
|
||||
d, err := parseDate(read.EndDate)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.EndReadDate = d
|
||||
}
|
||||
|
||||
//remove the book from "wanted" list when it is marked as read.
|
||||
if userbook.Read {
|
||||
userbook.WantRead = false
|
||||
}
|
||||
|
||||
//clear the date when unread
|
||||
if !userbook.Read {
|
||||
userbook.EndReadDate = nil
|
||||
}
|
||||
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
@@ -84,17 +98,12 @@ func PutStartReadUserBookHandler(ac appcontext.AppContext) {
|
||||
return
|
||||
}
|
||||
|
||||
//string equal to "null" to unset value
|
||||
if startDateToParse.StartDate == "null" {
|
||||
userbook.StartReadDate = nil
|
||||
} else {
|
||||
startDate, err := time.Parse(time.DateOnly, startDateToParse.StartDate)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.StartReadDate = &startDate
|
||||
d, err := parseDate(startDateToParse.StartDate)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.StartReadDate = d
|
||||
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
@@ -130,7 +139,8 @@ func PutRateUserBookHandler(ac appcontext.AppContext) {
|
||||
}
|
||||
|
||||
type userbookPutRead struct {
|
||||
Read bool `json:"read"`
|
||||
Read bool `json:"read"`
|
||||
EndDate string `json:"endDate"`
|
||||
}
|
||||
|
||||
type userbookPutWantRead struct {
|
||||
@@ -150,6 +160,16 @@ type apiCallData struct {
|
||||
User model.User
|
||||
}
|
||||
|
||||
func parseDate(dateToParse string) (*time.Time, error) {
|
||||
//string equal to "null" to unset value
|
||||
if dateToParse == "null" {
|
||||
return nil, nil
|
||||
} else {
|
||||
startDate, err := time.Parse(time.DateOnly, dateToParse)
|
||||
return &startDate, err
|
||||
}
|
||||
}
|
||||
|
||||
func retrieveDataFromContext(ac appcontext.AppContext) (apiCallData, error) {
|
||||
bookId64, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
||||
bookId := uint(bookId64)
|
||||
|
||||
Reference in New Issue
Block a user