mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(http): implement missing findByID methods (#1635)
* feat(http): implement missing methods * general cleanup * unify param handling * handle not found errors * unify err handlers * fix(http): fmt type
This commit is contained in:
parent
accc875960
commit
acb91e8709
15 changed files with 379 additions and 422 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
@ -19,6 +20,7 @@ import (
|
|||
type releaseService interface {
|
||||
Find(ctx context.Context, query domain.ReleaseQueryParams) (res []*domain.Release, nextCursor int64, count int64, err error)
|
||||
FindRecent(ctx context.Context) (res []*domain.Release, err error)
|
||||
Get(ctx context.Context, req *domain.GetReleaseRequest) (*domain.Release, error)
|
||||
GetIndexerOptions(ctx context.Context) ([]string, error)
|
||||
Stats(ctx context.Context) (*domain.ReleaseStats, error)
|
||||
Delete(ctx context.Context, req *domain.DeleteReleaseRequest) error
|
||||
|
@ -45,19 +47,19 @@ func (h releaseHandler) Routes(r chi.Router) {
|
|||
r.Get("/indexers", h.getIndexerOptions)
|
||||
r.Delete("/", h.deleteReleases)
|
||||
|
||||
r.Post("/process", h.retryAction)
|
||||
//r.Post("/process", h.retryAction)
|
||||
|
||||
r.Route("/{releaseId}", func(r chi.Router) {
|
||||
r.Post("/actions/{actionStatusId}/retry", h.retryAction)
|
||||
r.Route("/{releaseID}", func(r chi.Router) {
|
||||
r.Get("/", h.getReleaseByID)
|
||||
r.Post("/actions/{actionStatusID}/retry", h.retryAction)
|
||||
})
|
||||
}
|
||||
|
||||
func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
limitP := r.URL.Query().Get("limit")
|
||||
limit, err := strconv.Atoi(limitP)
|
||||
if err != nil && limitP != "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "limit parameter is invalid",
|
||||
})
|
||||
|
@ -70,7 +72,7 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
offsetP := r.URL.Query().Get("offset")
|
||||
offset, err := strconv.Atoi(offsetP)
|
||||
if err != nil && offsetP != "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "offset parameter is invalid",
|
||||
})
|
||||
|
@ -82,7 +84,7 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
if cursorP != "" {
|
||||
cursor, err = strconv.Atoi(cursorP)
|
||||
if err != nil && cursorP != "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "cursor parameter is invalid",
|
||||
})
|
||||
|
@ -92,7 +94,7 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
u, err := url.Parse(r.URL.String())
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "indexer parameter is invalid",
|
||||
})
|
||||
|
@ -104,7 +106,7 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
pushStatus := r.URL.Query().Get("push_status")
|
||||
if pushStatus != "" {
|
||||
if !domain.ValidReleasePushStatus(pushStatus) {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": fmt.Sprintf("push_status parameter is of invalid type: %v", pushStatus),
|
||||
})
|
||||
|
@ -128,7 +130,7 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
releases, nextCursor, count, err := h.service.Find(r.Context(), query)
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]any{
|
||||
"code": "INTERNAL_SERVER_ERROR",
|
||||
"message": err.Error(),
|
||||
})
|
||||
|
@ -149,10 +151,9 @@ func (h releaseHandler) findReleases(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h releaseHandler) findRecentReleases(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
releases, err := h.service.FindRecent(r.Context())
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]any{
|
||||
"code": "INTERNAL_SERVER_ERROR",
|
||||
"message": err.Error(),
|
||||
})
|
||||
|
@ -168,10 +169,31 @@ func (h releaseHandler) findRecentReleases(w http.ResponseWriter, r *http.Reques
|
|||
h.encoder.StatusResponse(w, http.StatusOK, ret)
|
||||
}
|
||||
|
||||
func (h releaseHandler) getReleaseByID(w http.ResponseWriter, r *http.Request) {
|
||||
releaseID, err := strconv.Atoi(chi.URLParam(r, "releaseID"))
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
release, err := h.service.Get(r.Context(), &domain.GetReleaseRequest{Id: releaseID})
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, errors.New("could not find release with id %d", releaseID))
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(w, http.StatusOK, release)
|
||||
}
|
||||
|
||||
func (h releaseHandler) getIndexerOptions(w http.ResponseWriter, r *http.Request) {
|
||||
stats, err := h.service.GetIndexerOptions(r.Context())
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]any{
|
||||
"code": "INTERNAL_SERVER_ERROR",
|
||||
"message": err.Error(),
|
||||
})
|
||||
|
@ -182,10 +204,9 @@ func (h releaseHandler) getIndexerOptions(w http.ResponseWriter, r *http.Request
|
|||
}
|
||||
|
||||
func (h releaseHandler) getStats(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
stats, err := h.service.Stats(r.Context())
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusInternalServerError, map[string]any{
|
||||
"code": "INTERNAL_SERVER_ERROR",
|
||||
"message": err.Error(),
|
||||
})
|
||||
|
@ -202,7 +223,7 @@ func (h releaseHandler) deleteReleases(w http.ResponseWriter, r *http.Request) {
|
|||
if olderThanParam != "" {
|
||||
duration, err := strconv.Atoi(olderThanParam)
|
||||
if err != nil {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "olderThan parameter is invalid",
|
||||
})
|
||||
|
@ -227,7 +248,7 @@ func (h releaseHandler) deleteReleases(w http.ResponseWriter, r *http.Request) {
|
|||
if _, valid := validStatuses[status]; valid {
|
||||
filteredStatuses = append(filteredStatuses, status)
|
||||
} else {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "INVALID_RELEASE_STATUS",
|
||||
"message": "releaseStatus contains invalid value",
|
||||
})
|
||||
|
@ -246,7 +267,6 @@ func (h releaseHandler) deleteReleases(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func (h releaseHandler) process(w http.ResponseWriter, r *http.Request) {
|
||||
var req *domain.ReleaseProcessReq
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
|
@ -254,14 +274,14 @@ func (h releaseHandler) process(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
if req.IndexerIdentifier == "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "VALIDATION_ERROR",
|
||||
"message": "field indexer_identifier empty",
|
||||
})
|
||||
}
|
||||
|
||||
if len(req.AnnounceLines) == 0 {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]any{
|
||||
"code": "VALIDATION_ERROR",
|
||||
"message": "field announce_lines empty",
|
||||
})
|
||||
|
@ -277,47 +297,29 @@ func (h releaseHandler) process(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h releaseHandler) retryAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
req *domain.ReleaseActionRetryReq
|
||||
err error
|
||||
)
|
||||
|
||||
releaseIdParam := chi.URLParam(r, "releaseId")
|
||||
if releaseIdParam == "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "parameter releaseId missing",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
releaseId, err := strconv.Atoi(releaseIdParam)
|
||||
releaseID, err := strconv.Atoi(chi.URLParam(r, "releaseID"))
|
||||
if err != nil {
|
||||
h.encoder.StatusError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
actionStatusIdParam := chi.URLParam(r, "actionStatusId")
|
||||
if actionStatusIdParam == "" {
|
||||
h.encoder.StatusResponse(w, http.StatusBadRequest, map[string]interface{}{
|
||||
"code": "BAD_REQUEST_PARAMS",
|
||||
"message": "parameter actionStatusId missing",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
actionStatusId, err := strconv.Atoi(actionStatusIdParam)
|
||||
actionStatusId, err := strconv.Atoi(chi.URLParam(r, "actionStatusID"))
|
||||
if err != nil {
|
||||
h.encoder.StatusError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
req = &domain.ReleaseActionRetryReq{
|
||||
ReleaseId: releaseId,
|
||||
req := &domain.ReleaseActionRetryReq{
|
||||
ReleaseId: releaseID,
|
||||
ActionStatusId: actionStatusId,
|
||||
}
|
||||
|
||||
if err := h.service.Retry(r.Context(), req); err != nil {
|
||||
if errors.Is(err, domain.ErrRecordNotFound) {
|
||||
h.encoder.NotFoundErr(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue