add end read date
This commit is contained in:
@@ -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