client and select
This commit is contained in:
parent
2fdcc0d259
commit
56eb32de9f
4
Makefile
4
Makefile
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
run-server:
|
run-server:
|
||||||
go run ./cmd/api/service.go -apikey localOnly
|
go run ./cmd/api-service/service.go -apikey localOnly
|
||||||
|
|
||||||
run-cli:
|
run-cli:
|
||||||
go run ./cmd/cli/main.go
|
go run ./cmd/terminal-client/main.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
|
||||||
|
}
|
|
@ -27,6 +27,8 @@ var sqliteMigrations = []sqliteMigration{
|
||||||
)`,
|
)`,
|
||||||
`CREATE TABLE system ("latest_sync" INTEGER)`,
|
`CREATE TABLE system ("latest_sync" INTEGER)`,
|
||||||
`INSERT INTO system (latest_sync) VALUES (0)`,
|
`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 (
|
var (
|
||||||
|
@ -69,9 +71,9 @@ func (s *SQLite) Store(m *movie.Movie) error {
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
directors := strings.Join(m.Directors, ",")
|
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)
|
if _, err := s.db.Exec(`REPLACE INTO movie (id, tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
m.ID, m.IMDBID, m.Title, m.EnglishTitle, m.Year, directors, m.WatchedOn, m.Rating, m.Comment); err != nil {
|
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)
|
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) {
|
func (s *SQLite) FindOne(id string) (*movie.Movie, error) {
|
||||||
row := s.db.QueryRow(`
|
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
|
FROM movie
|
||||||
WHERE id=?`, id)
|
WHERE id=?`, id)
|
||||||
if row.Err() != nil {
|
if row.Err() != nil {
|
||||||
|
@ -103,7 +105,7 @@ WHERE id=?`, id)
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
var directors string
|
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)
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
||||||
}
|
}
|
||||||
m.Directors = strings.Split(directors, ",")
|
m.Directors = strings.Split(directors, ",")
|
||||||
|
@ -113,7 +115,7 @@ WHERE id=?`, id)
|
||||||
|
|
||||||
func (s *SQLite) FindAll() ([]*movie.Movie, error) {
|
func (s *SQLite) FindAll() ([]*movie.Movie, error) {
|
||||||
rows, err := s.db.Query(`
|
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`)
|
FROM movie`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
||||||
|
@ -124,7 +126,7 @@ FROM movie`)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
m := &movie.Movie{}
|
m := &movie.Movie{}
|
||||||
var directors string
|
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)
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
||||||
}
|
}
|
||||||
m.Directors = strings.Split(directors, ",")
|
m.Directors = strings.Split(directors, ",")
|
||||||
|
|
|
@ -43,7 +43,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case "ctrl+c", "q", "esc":
|
case "ctrl+c", "q", "esc":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "enter":
|
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:
|
case tea.WindowSizeMsg:
|
||||||
|
@ -82,11 +87,7 @@ func (m *model) Search() {
|
||||||
items := []list.Item{}
|
items := []list.Item{}
|
||||||
for _, res := range movies {
|
for _, res := range movies {
|
||||||
items = append(items, Movie{m: res})
|
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.searchResults.SetItems(items)
|
||||||
m.focused = "result"
|
m.focused = "result"
|
||||||
|
|
Loading…
Reference in New Issue