autobrr/internal/http/download_client.go
2021-08-11 15:26:17 +02:00

119 lines
2.5 KiB
Go

package http
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/autobrr/autobrr/internal/domain"
)
type downloadClientService interface {
List() ([]domain.DownloadClient, error)
Store(client domain.DownloadClient) (*domain.DownloadClient, error)
Delete(clientID int) error
Test(client domain.DownloadClient) error
}
type downloadClientHandler struct {
encoder encoder
downloadClientService downloadClientService
}
func (h downloadClientHandler) Routes(r chi.Router) {
r.Get("/", h.listDownloadClients)
r.Post("/", h.store)
r.Put("/", h.update)
r.Post("/test", h.test)
r.Delete("/{clientID}", h.delete)
}
func (h downloadClientHandler) listDownloadClients(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
clients, err := h.downloadClientService.List()
if err != nil {
//
}
h.encoder.StatusResponse(ctx, w, clients, http.StatusOK)
}
func (h downloadClientHandler) store(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
data domain.DownloadClient
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
// encode error
return
}
client, err := h.downloadClientService.Store(data)
if err != nil {
// encode error
}
h.encoder.StatusResponse(ctx, w, client, http.StatusCreated)
}
func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
data domain.DownloadClient
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
// encode error
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
return
}
err := h.downloadClientService.Test(data)
if err != nil {
// encode error
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
return
}
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
}
func (h downloadClientHandler) update(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
data domain.DownloadClient
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
// encode error
return
}
client, err := h.downloadClientService.Store(data)
if err != nil {
// encode error
}
h.encoder.StatusResponse(ctx, w, client, http.StatusCreated)
}
func (h downloadClientHandler) delete(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
clientID = chi.URLParam(r, "clientID")
)
// if !clientID return error
id, _ := strconv.Atoi(clientID)
if err := h.downloadClientService.Delete(id); err != nil {
// encode error
}
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
}