58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (h *H) PutProgress(c *gin.Context) {
|
|
b, ok := h.bookFromParam(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req struct {
|
|
Locator json.RawMessage `json:"locator"`
|
|
Percent float64 `json:"percent"`
|
|
}
|
|
if e := c.ShouldBindJSON(&req); e != nil {
|
|
err(c, http.StatusBadRequest, "bad_request", "json body required")
|
|
return
|
|
}
|
|
if req.Percent < 0 || req.Percent > 1 {
|
|
err(c, http.StatusBadRequest, "bad_request", "percent must be in [0,1]")
|
|
return
|
|
}
|
|
if len(req.Locator) == 0 {
|
|
req.Locator = []byte("{}")
|
|
}
|
|
if !json.Valid(req.Locator) {
|
|
err(c, http.StatusBadRequest, "bad_request", "locator must be valid json")
|
|
return
|
|
}
|
|
if e := h.st.UpsertProgress(c, uid(c), b.LibraryID, b.Path, req.Locator, req.Percent); e != nil {
|
|
dbErr(c, e)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *H) ListProgress(c *gin.Context) {
|
|
rows, e := h.st.ListProgress(c, uid(c))
|
|
if e != nil {
|
|
dbErr(c, e)
|
|
return
|
|
}
|
|
out := make([]gin.H, 0, len(rows))
|
|
for _, p := range rows {
|
|
out = append(out, gin.H{
|
|
"library_id": p.LibraryID, "library": p.LibraryName, "path": p.BookPath,
|
|
"title": p.Title, "locator": json.RawMessage(p.Locator),
|
|
"percent": p.Percent, "updated_at": p.UpdatedAt.Format(time.RFC3339),
|
|
})
|
|
}
|
|
c.JSON(http.StatusOK, out)
|
|
}
|