From a24d39d6570f1fbaa9dc1772ca9ec7b0fa2cf19a Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 22 Dec 2021 02:10:01 +0100 Subject: [PATCH] Add in progress --- .../app/api/download/request_download.go | 23 +++++++++++++++++++ api/internal/app/api/download/routes.go | 1 + .../download_request/in_memory/repository.go | 22 ++++++++++++++++++ .../core/ports/download_request/repository.go | 1 + .../core/ports/downloadhandler/yt-dlp.go | 2 +- .../core/services/download/default/service.go | 4 ++++ .../core/services/download/service.go | 1 + 7 files changed, 53 insertions(+), 1 deletion(-) diff --git a/api/internal/app/api/download/request_download.go b/api/internal/app/api/download/request_download.go index b462fa6..0e9d3a5 100644 --- a/api/internal/app/api/download/request_download.go +++ b/api/internal/app/api/download/request_download.go @@ -45,6 +45,21 @@ func (a *api) requestDownload(w http.ResponseWriter, r *http.Request) { _ = 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) { 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 { 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 +} diff --git a/api/internal/app/api/download/routes.go b/api/internal/app/api/download/routes.go index 22bd065..32cbfb2 100644 --- a/api/internal/app/api/download/routes.go +++ b/api/internal/app/api/download/routes.go @@ -16,6 +16,7 @@ func New(service download.Service) *api { func (a *api) SetupDownloadApi(router *chi.Mux) { router.Route("/downloads", func(r chi.Router) { r.Post("/", a.requestDownload) + r.Get("/", a.getDownloads) r.Route("/{downloadId}", func(r chi.Router) { r.Use(Context) r.Get("/", a.getDownloadById) diff --git a/api/internal/core/ports/download_request/in_memory/repository.go b/api/internal/core/ports/download_request/in_memory/repository.go index 5f8202e..d3eddb4 100644 --- a/api/internal/core/ports/download_request/in_memory/repository.go +++ b/api/internal/core/ports/download_request/in_memory/repository.go @@ -5,6 +5,7 @@ import ( "downloader/internal/core/ports/download_request" "errors" "go.uber.org/zap" + "strings" ) 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") } } + +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 +} diff --git a/api/internal/core/ports/download_request/repository.go b/api/internal/core/ports/download_request/repository.go index d727e36..a4a60f4 100644 --- a/api/internal/core/ports/download_request/repository.go +++ b/api/internal/core/ports/download_request/repository.go @@ -6,4 +6,5 @@ type Repository interface { Create(download *entities.Download) (*entities.Download, error) GetById(id string) (*entities.Download, error) Update(download *entities.Download) error + Get(active bool) ([]*entities.Download, error) } diff --git a/api/internal/core/ports/downloadhandler/yt-dlp.go b/api/internal/core/ports/downloadhandler/yt-dlp.go index bc53979..a66e292 100644 --- a/api/internal/core/ports/downloadhandler/yt-dlp.go +++ b/api/internal/core/ports/downloadhandler/yt-dlp.go @@ -32,7 +32,7 @@ func NewYtDlpDownloader(logger *zap.SugaredLogger) DownloadHandler { _, err = exec.Command("yt-dlp", "--version").Output() 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{ diff --git a/api/internal/core/services/download/default/service.go b/api/internal/core/services/download/default/service.go index d9386b9..e8105b9 100644 --- a/api/internal/core/services/download/default/service.go +++ b/api/internal/core/services/download/default/service.go @@ -51,3 +51,7 @@ func (l *localService) Schedule(link string) (*entities.Download, error) { func (l *localService) Get(id string) (*entities.Download, error) { return l.repository.GetById(id) } + +func (l *localService) GetAll(active bool) ([]*entities.Download, error) { + return l.repository.Get(active) +} diff --git a/api/internal/core/services/download/service.go b/api/internal/core/services/download/service.go index 0e07126..5f12af1 100644 --- a/api/internal/core/services/download/service.go +++ b/api/internal/core/services/download/service.go @@ -5,4 +5,5 @@ import "downloader/internal/core/entities" type Service interface { Schedule(link string) (*entities.Download, error) Get(id string) (*entities.Download, error) + GetAll(active bool) ([]*entities.Download, error) }