Files
book-comic-library/backend/internal/api/progress.go
T

58 lines
1.3 KiB
Go

package api
import (
"encoding/json"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func (a *api) putProgress(c *gin.Context) {
b, ok := a.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 := a.st.UpsertProgress(c, uid(c), b.LibraryID, b.Path, req.Locator, req.Percent); e != nil {
dbErr(c, e)
return
}
c.Status(http.StatusNoContent)
}
func (a *api) listProgress(c *gin.Context) {
rows, e := a.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)
}