refactor(server/api): migrate requests/quarantine/me_token/listenbrainz/admin_invites preludes (A2 3/N)

This commit is contained in:
2026-05-08 08:26:43 -04:00
parent 8cd8825efc
commit dde3dd2804
5 changed files with 18 additions and 44 deletions
+2 -4
View File
@@ -57,9 +57,8 @@ type createInviteReq struct {
} }
func (h *handlers) handleCreateInvite(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleCreateInvite(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.Unauthorized("auth_required", ""))
return return
} }
var req createInviteReq var req createInviteReq
@@ -100,9 +99,8 @@ func (h *handlers) handleCreateInvite(w http.ResponseWriter, r *http.Request) {
} }
func (h *handlers) handleDeleteInvite(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleDeleteInvite(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.Unauthorized("auth_required", ""))
return return
} }
token := chi.URLParam(r, "token") token := chi.URLParam(r, "token")
+3 -8
View File
@@ -2,7 +2,6 @@ package api
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"net/http" "net/http"
"time" "time"
@@ -11,7 +10,6 @@ import (
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror" "git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
) )
@@ -25,9 +23,8 @@ type LBStatus struct {
} }
func (h *handlers) handleGetListenBrainz(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleGetListenBrainz(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
resp, err := h.buildLBStatus(r.Context(), user.ID) resp, err := h.buildLBStatus(r.Context(), user.ID)
@@ -45,14 +42,12 @@ type putLBBody struct {
} }
func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request) { func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
var body putLBBody var body putLBBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil { if !decodeBody(w, r, &body) {
writeErr(w, apierror.BadRequest("bad_request", "invalid json"))
return return
} }
+2 -4
View File
@@ -15,9 +15,8 @@ type apiTokenResp struct {
// handleGetMyAPIToken implements GET /api/me/api-token. // handleGetMyAPIToken implements GET /api/me/api-token.
func (h *handlers) handleGetMyAPIToken(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleGetMyAPIToken(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.Unauthorized("auth_required", ""))
return return
} }
q := dbq.New(h.pool) q := dbq.New(h.pool)
@@ -32,9 +31,8 @@ func (h *handlers) handleGetMyAPIToken(w http.ResponseWriter, r *http.Request) {
// handleRegenerateMyAPIToken implements POST /api/me/api-token. // handleRegenerateMyAPIToken implements POST /api/me/api-token.
func (h *handlers) handleRegenerateMyAPIToken(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleRegenerateMyAPIToken(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.Unauthorized("auth_required", ""))
return return
} }
newToken, err := auth.MintSessionToken() newToken, err := auth.MintSessionToken()
+5 -13
View File
@@ -1,15 +1,12 @@
package api package api
import ( import (
"encoding/json"
"errors" "errors"
"net/http" "net/http"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror" "git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrquarantine" "git.fabledsword.com/bvandeusen/minstrel/internal/lidarrquarantine"
) )
@@ -44,14 +41,12 @@ type flagBody struct {
// handleFlag implements POST /api/quarantine. Upserts via Service.Flag. // handleFlag implements POST /api/quarantine. Upserts via Service.Flag.
func (h *handlers) handleFlag(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleFlag(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
var body flagBody var body flagBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil { if !decodeBody(w, r, &body) {
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return return
} }
trackID, ok := parseUUID(body.TrackID) trackID, ok := parseUUID(body.TrackID)
@@ -77,14 +72,12 @@ func (h *handlers) handleFlag(w http.ResponseWriter, r *http.Request) {
// handleUnflag implements DELETE /api/quarantine/{track_id}. // handleUnflag implements DELETE /api/quarantine/{track_id}.
func (h *handlers) handleUnflag(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleUnflag(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
id, ok := parseUUID(chi.URLParam(r, "track_id")) id, ok := requireURLUUID(w, r, "track_id")
if !ok { if !ok {
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
return return
} }
if err := h.lidarrQuarantine.Unflag(r.Context(), user.ID, id); err != nil { if err := h.lidarrQuarantine.Unflag(r.Context(), user.ID, id); err != nil {
@@ -118,9 +111,8 @@ type quarantineMineView struct {
// handleListMyQuarantine implements GET /api/quarantine/mine. Returns the // handleListMyQuarantine implements GET /api/quarantine/mine. Returns the
// caller's quarantines with track/album/artist detail. // caller's quarantines with track/album/artist detail.
func (h *handlers) handleListMyQuarantine(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleListMyQuarantine(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
rows, err := h.lidarrQuarantine.ListMine(r.Context(), user.ID) rows, err := h.lidarrQuarantine.ListMine(r.Context(), user.ID)
+6 -15
View File
@@ -2,15 +2,12 @@ package api
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"net/http" "net/http"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror" "git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests" "git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests"
) )
@@ -123,15 +120,13 @@ type createRequestBody struct {
// handleCreateRequest implements POST /api/requests. // handleCreateRequest implements POST /api/requests.
func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
var body createRequestBody var body createRequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil { if !decodeBody(w, r, &body) {
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return return
} }
@@ -185,9 +180,8 @@ func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) {
// handleListRequests implements GET /api/requests — returns caller's own requests. // handleListRequests implements GET /api/requests — returns caller's own requests.
func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
@@ -211,9 +205,8 @@ func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) {
// handleGetRequest implements GET /api/requests/:id. // handleGetRequest implements GET /api/requests/:id.
// The caller must own the request, or be an admin. // The caller must own the request, or be an admin.
func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
@@ -237,15 +230,13 @@ func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) {
// handleCancelRequest implements DELETE /api/requests/:id. // handleCancelRequest implements DELETE /api/requests/:id.
// Cancels the caller's own pending request. // Cancels the caller's own pending request.
func (h *handlers) handleCancelRequest(w http.ResponseWriter, r *http.Request) { func (h *handlers) handleCancelRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context()) user, ok := requireUser(w, r)
if !ok { if !ok {
writeErr(w, apierror.ErrUnauthorized)
return return
} }
id, ok := parseUUID(chi.URLParam(r, "id")) id, ok := requireURLUUID(w, r, "id")
if !ok { if !ok {
writeErr(w, apierror.BadRequest("bad_request", "invalid request id"))
return return
} }