Add in progress

This commit is contained in:
Kasper Juul Hermansen 2021-12-22 02:10:01 +01:00
parent 23ca1168df
commit a24d39d657
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
7 changed files with 53 additions and 1 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}

View File

@ -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{

View File

@ -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)
}

View File

@ -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)
}