Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/taste"
|
|
)
|
|
|
|
type tasteHandlers struct{ svc tasteService }
|
|
|
|
type tasteArtistJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Preference float64 `json:"preference"`
|
|
Listens int `json:"listens"`
|
|
LastListenedAt string `json:"last_listened_at"`
|
|
}
|
|
|
|
type tasteTagJSON struct {
|
|
Tag string `json:"tag"`
|
|
Weight float64 `json:"weight"`
|
|
}
|
|
|
|
type tasteJSON struct {
|
|
Artists []tasteArtistJSON `json:"artists"`
|
|
Tags []tasteTagJSON `json:"tags"`
|
|
}
|
|
|
|
// taste shows what Sonda has learned about the signed-in user's taste. It reads
|
|
// local data only and contacts nobody.
|
|
func (h *tasteHandlers) taste(c *gin.Context) {
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return
|
|
}
|
|
p, err := h.svc.Taste(c.Request.Context(), currentUserID(c), clampInt(limit))
|
|
switch {
|
|
case errors.Is(err, taste.ErrInvalidQuery):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
out := tasteJSON{Artists: make([]tasteArtistJSON, len(p.Artists)), Tags: make([]tasteTagJSON, len(p.Tags))}
|
|
for i, a := range p.Artists {
|
|
out.Artists[i] = tasteArtistJSON{
|
|
ID: a.ID, Name: a.Name, Preference: a.Preference, Listens: a.Listens,
|
|
LastListenedAt: a.LastListened.UTC().Format(time.RFC3339),
|
|
}
|
|
}
|
|
for i, t := range p.Tags {
|
|
out.Tags[i] = tasteTagJSON{Tag: t.Tag, Weight: t.Weight}
|
|
}
|
|
c.JSON(http.StatusOK, out)
|
|
}
|