trigger reviews from tui
This commit is contained in:
parent
86b45fa480
commit
ea33c03a9d
|
@ -25,17 +25,6 @@ func NewEMDB(baseURL string, apiKey string) *EMDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EMDB) GetMovies() ([]moviestore.Movie, error) {
|
func (e *EMDB) GetMovies() ([]moviestore.Movie, error) {
|
||||||
//var movies []model.Movie
|
|
||||||
//for i := 0; i < 5; i++ {
|
|
||||||
// movies = append(movies, model.Movie{
|
|
||||||
// ID: fmt.Sprintf("id-%d", i),
|
|
||||||
// TMDBID: int64(i),
|
|
||||||
// IMDBID: fmt.Sprintf("tt%07d", i),
|
|
||||||
// Title: fmt.Sprintf("Movie %d", i),
|
|
||||||
// })
|
|
||||||
//}
|
|
||||||
//return movies, nil
|
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/movie", e.baseURL)
|
url := fmt.Sprintf("%s/movie", e.baseURL)
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -215,3 +204,36 @@ func (e *EMDB) UpdateReview(review moviestore.Review) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *EMDB) CreateJob(movieID, action string) error {
|
||||||
|
j := struct {
|
||||||
|
MovieID string
|
||||||
|
Action string
|
||||||
|
}{
|
||||||
|
MovieID: movieID,
|
||||||
|
Action: action,
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := json.Marshal(j)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/job", e.baseURL)
|
||||||
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("Authorization", e.apiKey)
|
||||||
|
|
||||||
|
resp, err := e.c.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||||
|
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package job
|
package job
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,3 +31,11 @@ type Job struct {
|
||||||
Created time.Time
|
Created time.Time
|
||||||
Updated time.Time
|
Updated time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Valid(action Action) bool {
|
||||||
|
if slices.Contains(validActions, action) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"ewintr.nl/emdb/cmd/api-service/moviestore"
|
"ewintr.nl/emdb/cmd/api-service/moviestore"
|
||||||
|
@ -25,8 +24,8 @@ func NewJobQueue(db *moviestore.SQLite, logger *slog.Logger) *JobQueue {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jq *JobQueue) Add(movieID string, action Action) error {
|
func (jq *JobQueue) Add(movieID string, action Action) error {
|
||||||
if !slices.Contains(validActions, action) {
|
if !Valid(action) {
|
||||||
return errors.New("unknown action")
|
return errors.New("invalid action")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := jq.db.Exec(`INSERT INTO job_queue (movie_id, action, status)
|
_, err := jq.db.Exec(`INSERT INTO job_queue (movie_id, action, status)
|
||||||
|
@ -48,7 +47,7 @@ func (jq *JobQueue) Run() {
|
||||||
SELECT id, movie_id, action
|
SELECT id, movie_id, action
|
||||||
FROM job_queue
|
FROM job_queue
|
||||||
WHERE status='todo'
|
WHERE status='todo'
|
||||||
ORDER BY id DESC
|
ORDER BY id ASC
|
||||||
LIMIT 1`)
|
LIMIT 1`)
|
||||||
|
|
||||||
var job Job
|
var job Job
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"ewintr.nl/emdb/client"
|
"ewintr.nl/emdb/client"
|
||||||
|
"ewintr.nl/emdb/cmd/api-service/job"
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"github.com/charmbracelet/bubbles/list"
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
@ -130,6 +131,9 @@ func (m *tabTMDB) ImportMovieCmd(movie Movie) tea.Cmd {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := m.emdb.CreateJob(newMovie.ID, string(job.ActionRefreshIMDBReviews)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return NewMovie(Movie{m: newMovie})
|
return NewMovie(Movie{m: newMovie})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue