wip
This commit is contained in:
parent
06ead2b299
commit
0c9496f087
|
@ -25,45 +25,45 @@ func NewEMDB(baseURL string, apiKey string) *EMDB {
|
|||
}
|
||||
|
||||
func (e *EMDB) GetMovies() ([]model.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)
|
||||
//req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//req.Header.Add("Authorization", e.apiKey)
|
||||
//
|
||||
//resp, err := e.c.Do(req)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//
|
||||
//if resp.StatusCode != http.StatusOK {
|
||||
// return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
//}
|
||||
//
|
||||
//body, err := io.ReadAll(resp.Body)
|
||||
//defer resp.Body.Close()
|
||||
//
|
||||
//var movies []model.Movie
|
||||
//if err := json.Unmarshal(body, &movies); err != nil {
|
||||
// return nil, err
|
||||
//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)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Authorization", e.apiKey)
|
||||
|
||||
resp, err := e.c.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
var movies []model.Movie
|
||||
if err := json.Unmarshal(body, &movies); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
func (e *EMDB) AddMovie(movie model.Movie) (model.Movie, error) {
|
||||
func (e *EMDB) CreateMovie(movie model.Movie) (model.Movie, error) {
|
||||
body, err := json.Marshal(movie)
|
||||
if err != nil {
|
||||
return model.Movie{}, err
|
||||
|
@ -99,7 +99,7 @@ func (e *EMDB) AddMovie(movie model.Movie) (model.Movie, error) {
|
|||
return newMovie, nil
|
||||
}
|
||||
|
||||
func (e *EMDB) StoreMovie(movie model.Movie) (model.Movie, error) {
|
||||
func (e *EMDB) UpdateMovie(movie model.Movie) (model.Movie, error) {
|
||||
body, err := json.Marshal(movie)
|
||||
if err != nil {
|
||||
return model.Movie{}, err
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type modelEdit struct {
|
||||
movie Movie
|
||||
focused string
|
||||
ratingField textinput.Model
|
||||
commentFiel textinput.Model
|
||||
}
|
||||
|
||||
func NewEdit(movie Movie) *modelEdit {
|
||||
m := &modelEdit{
|
||||
movie: movie,
|
||||
focused: "rating",
|
||||
ratingField: textinput.New(),
|
||||
commentFiel: textinput.New(),
|
||||
}
|
||||
m.ratingField.Placeholder = "Rating"
|
||||
m.ratingField.Width = 2
|
||||
m.ratingField.CharLimit = 2
|
||||
m.ratingField.Focus()
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m modelEdit) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m modelEdit) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q", "esc":
|
||||
return m, tea.Quit
|
||||
case "tab":
|
||||
switch m.focused {
|
||||
case "rating":
|
||||
m.focused = "comment"
|
||||
m.commentFiel.Focus()
|
||||
case "comment":
|
||||
m.focused = "rating"
|
||||
m.ratingField.Focus()
|
||||
}
|
||||
case "enter":
|
||||
}
|
||||
}
|
||||
|
||||
switch m.focused {
|
||||
case "rating":
|
||||
m.ratingField, cmd = m.ratingField.Update(msg)
|
||||
case "comment":
|
||||
m.commentFiel, cmd = m.commentFiel.Update(msg)
|
||||
}
|
||||
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m modelEdit) View() string {
|
||||
return fmt.Sprintf("Title: \t%s\nSumary: \t%s\nRating: \t%s\nComment: \t%s\n",
|
||||
m.movie.Title(),
|
||||
m.movie.Description(),
|
||||
m.ratingField.View(),
|
||||
m.commentFiel.View(),
|
||||
)
|
||||
}
|
|
@ -13,6 +13,12 @@ import (
|
|||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var (
|
||||
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
||||
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
|
||||
noStyle = lipgloss.NewStyle()
|
||||
)
|
||||
|
||||
type UpdateForm tea.Msg
|
||||
type StoredMovie struct{}
|
||||
|
||||
|
@ -36,14 +42,6 @@ func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
|||
list.SetShowHelp(false)
|
||||
|
||||
formLabels := []string{
|
||||
"ID",
|
||||
"TMDB ID",
|
||||
"IMDB ID",
|
||||
"Title",
|
||||
"English Title",
|
||||
"Year",
|
||||
"Director",
|
||||
"Summary",
|
||||
"Rating",
|
||||
"Comment",
|
||||
}
|
||||
|
@ -95,10 +93,8 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
m.logger.Log("stored movie, fetching movie list")
|
||||
cmds = append(cmds, FetchMovieList(m.emdb))
|
||||
case tea.KeyMsg:
|
||||
m.Log("key msg")
|
||||
switch m.mode {
|
||||
case "edit":
|
||||
m.Log("processing edit mode")
|
||||
switch msg.String() {
|
||||
case "tab", "shift+tab", "up", "down":
|
||||
s := msg.String()
|
||||
|
@ -115,10 +111,14 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
}
|
||||
for i := 0; i <= len(m.formInputs)-1; i++ {
|
||||
if i == m.formFocus {
|
||||
m.formInputs[i].PromptStyle = focusedStyle
|
||||
m.formInputs[i].TextStyle = focusedStyle
|
||||
cmds = append(cmds, m.formInputs[i].Focus())
|
||||
continue
|
||||
}
|
||||
m.formInputs[i].Blur()
|
||||
m.formInputs[i].PromptStyle = noStyle
|
||||
m.formInputs[i].TextStyle = noStyle
|
||||
}
|
||||
case "enter":
|
||||
m.mode = "view"
|
||||
|
@ -127,18 +127,17 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
cmds = append(cmds, m.updateFormInputs(msg))
|
||||
}
|
||||
default:
|
||||
m.Log("processing view mode")
|
||||
switch msg.String() {
|
||||
case "up":
|
||||
m.list, cmd = m.list.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
m.SetForm(m.list.SelectedItem().(Movie))
|
||||
case "down":
|
||||
m.list, cmd = m.list.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
m.SetForm(m.list.SelectedItem().(Movie))
|
||||
case "e":
|
||||
m.mode = "edit"
|
||||
m.formFocus = 0
|
||||
m.formInputs[0].Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,37 +155,61 @@ func (m *tabEMDB) updateFormInputs(msg tea.Msg) tea.Cmd {
|
|||
}
|
||||
|
||||
func (m tabEMDB) View() string {
|
||||
labels := make([]string, len(m.formLabels))
|
||||
for i := range m.formLabels {
|
||||
labels[i] = fmt.Sprintf("%s: ", m.formLabels[i])
|
||||
}
|
||||
fields := make([]string, len(m.formLabels))
|
||||
for i := range m.formLabels {
|
||||
fields[i] = m.formInputs[i].View()
|
||||
}
|
||||
labelView := strings.Join(labels, "\n")
|
||||
fieldsView := strings.Join(fields, "\n")
|
||||
form := lipgloss.JoinHorizontal(lipgloss.Top, labelView, fieldsView)
|
||||
|
||||
colLeft := lipgloss.NewStyle().Width(m.colWidth).Render(m.list.View())
|
||||
colRight := lipgloss.NewStyle().Width(m.colWidth).Render(form)
|
||||
colRight := lipgloss.NewStyle().Width(m.colWidth).Render(m.ViewForm())
|
||||
|
||||
return lipgloss.JoinHorizontal(lipgloss.Top, colLeft, colRight)
|
||||
}
|
||||
|
||||
func (m *tabEMDB) SetForm(movie Movie) {
|
||||
m.formInputs[0].SetValue(movie.m.ID)
|
||||
m.formInputs[1].SetValue(fmt.Sprintf("%d", movie.m.TMDBID))
|
||||
m.formInputs[2].SetValue(movie.m.IMDBID)
|
||||
m.formInputs[3].SetValue(movie.m.Title)
|
||||
m.formInputs[4].SetValue(movie.m.EnglishTitle)
|
||||
m.formInputs[5].SetValue(fmt.Sprintf("%d", movie.m.Year))
|
||||
m.formInputs[6].SetValue(strings.Join(movie.m.Directors, ","))
|
||||
m.formInputs[7].SetValue(movie.m.Summary)
|
||||
m.formInputs[8].SetValue(fmt.Sprintf("%d", movie.m.Rating))
|
||||
func (m *tabEMDB) UpdateForm() {
|
||||
movie, ok := m.list.SelectedItem().(Movie)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
m.formInputs[0].SetValue(fmt.Sprintf("%d", movie.m.Rating))
|
||||
m.formInputs[9].SetValue(movie.m.Comment)
|
||||
}
|
||||
|
||||
func (m *tabEMDB) ViewForm() string {
|
||||
movie, ok := m.list.SelectedItem().(Movie)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
labels := []string{
|
||||
"ID: ",
|
||||
"TMDBID: ",
|
||||
"IMDBID: ",
|
||||
"Title",
|
||||
"English title",
|
||||
"Year",
|
||||
"Directors",
|
||||
"Summary",
|
||||
}
|
||||
for _, l := range m.formLabels {
|
||||
labels = append(labels, fmt.Sprintf("%s: ", l))
|
||||
}
|
||||
|
||||
fields := []string{
|
||||
movie.m.ID,
|
||||
fmt.Sprintf("%d", movie.m.TMDBID),
|
||||
movie.m.IMDBID,
|
||||
movie.m.Title,
|
||||
movie.m.EnglishTitle,
|
||||
fmt.Sprintf("%d", movie.m.Year),
|
||||
strings.Join(movie.m.Directors, ","),
|
||||
movie.m.Summary,
|
||||
}
|
||||
for _, f := range m.formInputs {
|
||||
fields = append(fields, f.View())
|
||||
}
|
||||
labelView := strings.Join(labels, "\n")
|
||||
fieldsView := strings.Join(fields, "\n")
|
||||
|
||||
return lipgloss.JoinHorizontal(lipgloss.Top, labelView, fieldsView)
|
||||
}
|
||||
|
||||
func (m *tabEMDB) StoreMovie() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
tmdbId, err := strconv.Atoi(m.formInputs[1].Value())
|
||||
|
@ -216,7 +239,7 @@ func (m *tabEMDB) StoreMovie() tea.Cmd {
|
|||
},
|
||||
}
|
||||
m.Log(fmt.Sprintf("storing movie %s", movie.Title()))
|
||||
if _, err := m.emdb.AddMovie(movie.m); err != nil {
|
||||
if _, err := m.emdb.CreateMovie(movie.m); err != nil {
|
||||
return err
|
||||
}
|
||||
return StoredMovie{}
|
||||
|
|
|
@ -114,7 +114,7 @@ func (m *tabTMDB) SearchTMDBCmd(query string) tea.Cmd {
|
|||
|
||||
func (m *tabTMDB) ImportMovieCmd(movie Movie) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
newMovie, err := m.emdb.AddMovie(movie.m)
|
||||
newMovie, err := m.emdb.CreateMovie(movie.m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue