diff --git a/Makefile b/Makefile index 4411b7f..be3ec8d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ run-server: - go run ./cmd/api/service.go -apikey localOnly + go run ./cmd/api-service/service.go -apikey localOnly run-cli: - go run ./cmd/cli/main.go \ No newline at end of file + go run ./cmd/terminal-client/main.go \ No newline at end of file diff --git a/cmd/api-service/client/client.go b/cmd/api-service/client/client.go new file mode 100644 index 0000000..997c555 --- /dev/null +++ b/cmd/api-service/client/client.go @@ -0,0 +1,49 @@ +package client + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" + + "ewintr.nl/emdb/movie" +) + +type Client struct { + apiKey string + url string + c *http.Client +} + +func New(url, apiKey string) *Client { + return &Client{ + apiKey: apiKey, + url: url, + c: &http.Client{Timeout: 10 * time.Second}, + } +} + +func (c *Client) Store(m *movie.Movie) error { + url := fmt.Sprintf("%s/movie/%s", c.url, m.ID) + bodyJSON, err := json.Marshal(m) + if err != nil { + return err + } + body := bytes.NewBuffer(bodyJSON) + req, err := http.NewRequest(http.MethodPut, url, body) + if err != nil { + return err + } + req.Header.Set("Authorization", c.apiKey) + req.Header.Set("Content-Type", "application/json") + resp, err := c.c.Do(req) + if err != nil { + return err + } + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + return nil +} diff --git a/cmd/api-service/server/sqlite.go b/cmd/api-service/server/sqlite.go index f3b3406..9fc2a8b 100644 --- a/cmd/api-service/server/sqlite.go +++ b/cmd/api-service/server/sqlite.go @@ -27,6 +27,8 @@ var sqliteMigrations = []sqliteMigration{ )`, `CREATE TABLE system ("latest_sync" INTEGER)`, `INSERT INTO system (latest_sync) VALUES (0)`, + `ALTER TABLE movie ADD COLUMN tmdb_id INTEGER NOT NULL DEFAULT 0`, + `ALTER TABLE movie ADD COLUMN summary TEXT NOT NULL DEFAULT ""`, } var ( @@ -69,9 +71,9 @@ func (s *SQLite) Store(m *movie.Movie) error { defer tx.Rollback() directors := strings.Join(m.Directors, ",") - if _, err := s.db.Exec(`REPLACE INTO movie (id, imdb_id, title, english_title, year, directors, watched_on, rating, comment) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, - m.ID, m.IMDBID, m.Title, m.EnglishTitle, m.Year, directors, m.WatchedOn, m.Rating, m.Comment); err != nil { + if _, err := s.db.Exec(`REPLACE INTO movie (id, tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + m.ID, m.IMDBID, m.IMDBID, m.Title, m.EnglishTitle, m.Year, directors, m.Summary, m.WatchedOn, m.Rating, m.Comment); err != nil { return fmt.Errorf("%w: %v", ErrSqliteFailure, err) } @@ -92,7 +94,7 @@ func (s *SQLite) Delete(id string) error { func (s *SQLite) FindOne(id string) (*movie.Movie, error) { row := s.db.QueryRow(` -SELECT imdb_id, title, english_title, year, directors, watched_on, rating, comment +SELECT tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment FROM movie WHERE id=?`, id) if row.Err() != nil { @@ -103,7 +105,7 @@ WHERE id=?`, id) ID: id, } var directors string - if err := row.Scan(&m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.WatchedOn, &m.Rating, &m.Comment); err != nil { + if err := row.Scan(&m.IMDBID, &m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.Summary, &m.WatchedOn, &m.Rating, &m.Comment); err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } m.Directors = strings.Split(directors, ",") @@ -113,7 +115,7 @@ WHERE id=?`, id) func (s *SQLite) FindAll() ([]*movie.Movie, error) { rows, err := s.db.Query(` -SELECT imdb_id, title, english_title, year, directors, watched_on, rating, comment +SELECT tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment FROM movie`) if err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) @@ -124,7 +126,7 @@ FROM movie`) for rows.Next() { m := &movie.Movie{} var directors string - if err := rows.Scan(&m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.WatchedOn, &m.Rating, &m.Comment); err != nil { + if err := rows.Scan(&m.TMDBID, &m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.Summary, &m.WatchedOn, &m.Rating, &m.Comment); err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } m.Directors = strings.Split(directors, ",") diff --git a/cmd/terminal-client/tui/tui.go b/cmd/terminal-client/tui/tui.go index 057504c..f772012 100644 --- a/cmd/terminal-client/tui/tui.go +++ b/cmd/terminal-client/tui/tui.go @@ -43,7 +43,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+c", "q", "esc": return m, tea.Quit case "enter": - m.Search() + switch m.focused { + case "search": + m.Search() + case "result": + m.Log(fmt.Sprintf("selected: %d", m.searchResults.Index())) + } } case tea.WindowSizeMsg: @@ -82,11 +87,7 @@ func (m *model) Search() { items := []list.Item{} for _, res := range movies { items = append(items, Movie{m: res}) - //fmt.Printf("result: %+v\n", res.Title) } - //for i := 0; i < 10; i++ { - // items = append(items, Movie{m: movie.Movie{Title: fmt.Sprintf("title %d", i)}}) - //} m.searchResults.SetItems(items) m.focused = "result"