Add in progress
This commit is contained in:
parent
23ca1168df
commit
a24d39d657
@ -45,6 +45,21 @@ func (a *api) requestDownload(w http.ResponseWriter, r *http.Request) {
|
|||||||
_ = render.Render(w, r, newRequestDownloadResponse(download))
|
_ = render.Render(w, r, newRequestDownloadResponse(download))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *api) getDownloads(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
active := request.URL.Query().Get("active") == "true"
|
||||||
|
downloads, err := a.drService.GetAll(active)
|
||||||
|
if err != nil {
|
||||||
|
_ = render.Render(writer, request, responses.ErrInvalidRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = render.RenderList(writer, request, newDownloadsResponse(downloads)); err != nil {
|
||||||
|
_ = render.Render(writer, request, responses.ErrInvalidRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (a *api) getDownloadById(w http.ResponseWriter, r *http.Request) {
|
func (a *api) getDownloadById(w http.ResponseWriter, r *http.Request) {
|
||||||
downloadId := r.Context().Value("downloadId").(string)
|
downloadId := r.Context().Value("downloadId").(string)
|
||||||
|
|
||||||
@ -63,3 +78,11 @@ func (a *api) getDownloadById(w http.ResponseWriter, r *http.Request) {
|
|||||||
func newRequestDownloadResponse(download *entities.Download) *requestDownloadResponse {
|
func newRequestDownloadResponse(download *entities.Download) *requestDownloadResponse {
|
||||||
return &requestDownloadResponse{Download: download}
|
return &requestDownloadResponse{Download: download}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newDownloadsResponse(downloads []*entities.Download) []render.Renderer {
|
||||||
|
list := []render.Renderer{}
|
||||||
|
for _, download := range downloads {
|
||||||
|
list = append(list, newRequestDownloadResponse(download))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
@ -16,6 +16,7 @@ func New(service download.Service) *api {
|
|||||||
func (a *api) SetupDownloadApi(router *chi.Mux) {
|
func (a *api) SetupDownloadApi(router *chi.Mux) {
|
||||||
router.Route("/downloads", func(r chi.Router) {
|
router.Route("/downloads", func(r chi.Router) {
|
||||||
r.Post("/", a.requestDownload)
|
r.Post("/", a.requestDownload)
|
||||||
|
r.Get("/", a.getDownloads)
|
||||||
r.Route("/{downloadId}", func(r chi.Router) {
|
r.Route("/{downloadId}", func(r chi.Router) {
|
||||||
r.Use(Context)
|
r.Use(Context)
|
||||||
r.Get("/", a.getDownloadById)
|
r.Get("/", a.getDownloadById)
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"downloader/internal/core/ports/download_request"
|
"downloader/internal/core/ports/download_request"
|
||||||
"errors"
|
"errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inMemoryRepository struct {
|
type inMemoryRepository struct {
|
||||||
@ -54,3 +55,24 @@ func (i *inMemoryRepository) GetById(id string) (*entities.Download, error) {
|
|||||||
return nil, errors.New("download was not found in the database")
|
return nil, errors.New("download was not found in the database")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *inMemoryRepository) Get(active bool) ([]*entities.Download, error) {
|
||||||
|
var list []*entities.Download
|
||||||
|
for _, download := range i.collection {
|
||||||
|
if active {
|
||||||
|
if strings.Contains(download.Status, "in-progress") {
|
||||||
|
list = append(list, download)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !strings.Contains(download.Status, "in-progress") {
|
||||||
|
list = append(list, download)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(list) == 0 {
|
||||||
|
return []*entities.Download{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
@ -6,4 +6,5 @@ type Repository interface {
|
|||||||
Create(download *entities.Download) (*entities.Download, error)
|
Create(download *entities.Download) (*entities.Download, error)
|
||||||
GetById(id string) (*entities.Download, error)
|
GetById(id string) (*entities.Download, error)
|
||||||
Update(download *entities.Download) error
|
Update(download *entities.Download) error
|
||||||
|
Get(active bool) ([]*entities.Download, error)
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ func NewYtDlpDownloader(logger *zap.SugaredLogger) DownloadHandler {
|
|||||||
|
|
||||||
_, err = exec.Command("yt-dlp", "--version").Output()
|
_, err = exec.Command("yt-dlp", "--version").Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Youtube download (youtube-dl) isn't installed on the device")
|
log.Fatal("Youtube download (yt-dlp) isn't installed on the device")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &YtDlpDownloader{
|
return &YtDlpDownloader{
|
||||||
|
@ -51,3 +51,7 @@ func (l *localService) Schedule(link string) (*entities.Download, error) {
|
|||||||
func (l *localService) Get(id string) (*entities.Download, error) {
|
func (l *localService) Get(id string) (*entities.Download, error) {
|
||||||
return l.repository.GetById(id)
|
return l.repository.GetById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *localService) GetAll(active bool) ([]*entities.Download, error) {
|
||||||
|
return l.repository.Get(active)
|
||||||
|
}
|
||||||
|
@ -5,4 +5,5 @@ import "downloader/internal/core/entities"
|
|||||||
type Service interface {
|
type Service interface {
|
||||||
Schedule(link string) (*entities.Download, error)
|
Schedule(link string) (*entities.Download, error)
|
||||||
Get(id string) (*entities.Download, error)
|
Get(id string) (*entities.Download, error)
|
||||||
|
GetAll(active bool) ([]*entities.Download, error)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user