From 5ece7b08737fb01751a3a292fe7bcbf097d4203e Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 02:39:56 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=8E=A8=20Format=20code=20via=20`go=20?= =?UTF-8?q?fmt`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd_add.go | 4 +-- cmd/cmd_list.go | 2 +- cmd/cmd_media_search.go | 2 +- cmd/cmd_update.go | 10 +++--- internal/api/anilist.go | 2 +- internal/api/mutations.go | 2 +- internal/api/responses/media_list.go | 6 ++-- internal/api/responses/media_search.go | 4 +-- .../api/responses/media_update_response.go | 4 +-- internal/constants.go | 12 +++---- internal/db/queries.go | 4 +-- internal/helpers.go | 4 +-- internal/ui/media_list.go | 10 +++--- internal/ui/media_search.go | 16 ++++----- internal/ui/profile.go | 2 +- internal/ui/styles.go | 2 +- internal/viewmodel/login_handler.go | 2 +- internal/viewmodel/media_list_handler.go | 8 ++--- internal/viewmodel/media_search_handler.go | 2 +- internal/viewmodel/media_update_handler.go | 33 +++++++++---------- 20 files changed, 65 insertions(+), 66 deletions(-) diff --git a/cmd/cmd_add.go b/cmd/cmd_add.go index 9d7bb15..c07f445 100644 --- a/cmd/cmd_add.go +++ b/cmd/cmd_add.go @@ -24,8 +24,8 @@ func handleMediaAdd(cmd *cobra.Command, args []string) { err = viewmodel.HandleMediaUpdate( viewmodel.MediaUpdateParams{ IsNewAddition: true, - MediaId: mediaId, - Status: mediaAddStatus, + MediaId: mediaId, + Status: mediaAddStatus, }, ) if err != nil { diff --git a/cmd/cmd_list.go b/cmd/cmd_list.go index c1018a0..cf6c036 100644 --- a/cmd/cmd_list.go +++ b/cmd/cmd_list.go @@ -22,7 +22,7 @@ var mediaListCmd = &cobra.Command{ Use: "list", Short: "List your current anime/manga list", Aliases: []string{"ls"}, - Run: handleLs, + Run: handleLs, } func init() { diff --git a/cmd/cmd_media_search.go b/cmd/cmd_media_search.go index db5c26c..17a224d 100644 --- a/cmd/cmd_media_search.go +++ b/cmd/cmd_media_search.go @@ -86,7 +86,7 @@ var mediaSearchCmd = &cobra.Command{ Use: "search [query...]", Short: "Search for anime and manga", Args: cobra.MinimumNArgs(1), - Run: handleMediaSearch, + Run: handleMediaSearch, } func init() { diff --git a/cmd/cmd_update.go b/cmd/cmd_update.go index 8a7e139..4734f91 100644 --- a/cmd/cmd_update.go +++ b/cmd/cmd_update.go @@ -51,10 +51,10 @@ func handleUpdate(cmd *cobra.Command, args []string) { err = viewmodel.HandleMediaUpdate( viewmodel.MediaUpdateParams{ IsNewAddition: false, - MediaId: id, - Progress: progress, - Status: "none", - StartDate: "none", + MediaId: id, + Progress: progress, + Status: "none", + StartDate: "none", }, ) @@ -67,7 +67,7 @@ var mediaUpdateCmd = &cobra.Command{ Use: "update [id]", Short: "Update a list entry", Args: cobra.MinimumNArgs(1), - Run: handleUpdate, + Run: handleUpdate, } func init() { diff --git a/internal/api/anilist.go b/internal/api/anilist.go index e4d30c5..443610c 100644 --- a/internal/api/anilist.go +++ b/internal/api/anilist.go @@ -145,4 +145,4 @@ func UpdateMediaEntry(params map[string]any) (*responses.MediaUpdateResponse, er } return &responseStruct, nil -} \ No newline at end of file +} diff --git a/internal/api/mutations.go b/internal/api/mutations.go index bfb52dd..499055a 100644 --- a/internal/api/mutations.go +++ b/internal/api/mutations.go @@ -37,4 +37,4 @@ const mediaEntryUpdateMutation = `mutation( } } } -}` \ No newline at end of file +}` diff --git a/internal/api/responses/media_list.go b/internal/api/responses/media_list.go index a9575e4..3eb75c0 100644 --- a/internal/api/responses/media_list.go +++ b/internal/api/responses/media_list.go @@ -11,9 +11,9 @@ type ListCollection struct { Title struct { UserPreferred string `json:"userPreferred"` } `json:"title"` - Chapters *int `json:"chapters"` - Volumes *int `json:"volumes"` - Episodes *int `json:"episodes"` + Chapters *int `json:"chapters"` + Volumes *int `json:"volumes"` + Episodes *int `json:"episodes"` MediaFormat string `json:"format"` } `json:"media"` } `json:"entries"` diff --git a/internal/api/responses/media_search.go b/internal/api/responses/media_search.go index 955c4c5..2329645 100644 --- a/internal/api/responses/media_search.go +++ b/internal/api/responses/media_search.go @@ -6,8 +6,8 @@ type MediaSearchList struct { UserPreferred string `json:"userPreferred"` } `json:"title"` AverageScore *float64 `json:"averageScore"` - MediaType string `json:"type"` - MediaFormat string `json:"format"` + MediaType string `json:"type"` + MediaFormat string `json:"format"` } type MediaSearch struct { diff --git a/internal/api/responses/media_update_response.go b/internal/api/responses/media_update_response.go index 8a0e4f1..c1f541e 100644 --- a/internal/api/responses/media_update_response.go +++ b/internal/api/responses/media_update_response.go @@ -4,11 +4,11 @@ type MediaUpdateResponse struct { Data struct { SaveMediaListEntry struct { Media struct { - Id int `json:"id"` + Id int `json:"id"` Title struct { UserPreferred string `json:"userPreferred"` } `json:"title"` } `json:"media"` } `json:"SaveMediaListEntry"` } `json:"data"` -} \ No newline at end of file +} diff --git a/internal/constants.go b/internal/constants.go index d140993..3c296da 100644 --- a/internal/constants.go +++ b/internal/constants.go @@ -4,16 +4,16 @@ package internal // Base URL is not gonna get changed for a while. // So, keeping it as constant is not gonna hurt anyone. -const( - API_ENDPOINT = "https://graphql.anilist.co" - AUTH_URL = "https://anilist.co/api/v2/oauth/authorize?client_id=4593&response_type=token" +const ( + API_ENDPOINT = "https://graphql.anilist.co" + AUTH_URL = "https://anilist.co/api/v2/oauth/authorize?client_id=4593&response_type=token" BOLT_BUCKET_NAME = "ChibiConfig" - BOLT_DB_NAME = "chibi_config.db" + BOLT_DB_NAME = "chibi_config.db" ) -type MediaType string +type MediaType string const ( ANIME MediaType = "ANIME" MANGA MediaType = "MANGA" -) \ No newline at end of file +) diff --git a/internal/db/queries.go b/internal/db/queries.go index 2943caf..6ec2780 100644 --- a/internal/db/queries.go +++ b/internal/db/queries.go @@ -1,6 +1,6 @@ package db -const( +const ( QUERY_CREATE_TABLE = `CREATE TABLE IF NOT EXISTS config ( id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL, @@ -10,4 +10,4 @@ const( QUERY_INSERT_CONFIG = `INSERT INTO config (key, value) VALUES (?, ?)` QUERY_GET_CONFIG = `SELECT value FROM config WHERE key = ?` -) \ No newline at end of file +) diff --git a/internal/helpers.go b/internal/helpers.go index 2c10e73..50b2f1b 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -17,7 +17,7 @@ func CreateConfigDir() { os.RemoveAll(configDir) } os.MkdirAll(configDir, 0755) -} +} // maps "type" command line argument string to valid // MediaType enum required by AniList API @@ -74,4 +74,4 @@ func MediaFormatFormatter(mediaFormat string) string { default: return "?" } -} \ No newline at end of file +} diff --git a/internal/ui/media_list.go b/internal/ui/media_list.go index 5e0901e..ec67f87 100644 --- a/internal/ui/media_list.go +++ b/internal/ui/media_list.go @@ -4,7 +4,7 @@ import ( "fmt" "os" "strconv" - + "github.com/CosmicPredator/chibi/internal" "github.com/CosmicPredator/chibi/internal/api/responses" "github.com/charmbracelet/lipgloss" @@ -39,7 +39,7 @@ func (l *MediaListUI) renderTable(rows ...[]string) (*table.Table, error) { Align(lipgloss.Center) } - if row % 2 == 0 && (col == 0 || col == 2 || col == 3) { + if row%2 == 0 && (col == 0 || col == 2 || col == 3) { return lipgloss.NewStyle().Align(lipgloss.Center).Faint(true) } @@ -51,7 +51,7 @@ func (l *MediaListUI) renderTable(rows ...[]string) (*table.Table, error) { PaddingLeft(1). PaddingRight(1). Width((tw - 6) / 3).Inline(true) - if row % 2 == 0 { + if row%2 == 0 { colStyle = colStyle.Faint(true) } return colStyle @@ -102,7 +102,7 @@ func (l *MediaListUI) Render() error { if list.Status == "REPEATING" { entry.Media.Title.UserPreferred = "(R) " + entry.Media.Title.UserPreferred } - + rows = append(rows, []string{ strconv.Itoa(entry.Media.Id), entry.Media.Title.UserPreferred, @@ -120,4 +120,4 @@ func (l *MediaListUI) Render() error { fmt.Println(table) return nil -} \ No newline at end of file +} diff --git a/internal/ui/media_search.go b/internal/ui/media_search.go index 481a122..0d24391 100644 --- a/internal/ui/media_search.go +++ b/internal/ui/media_search.go @@ -31,13 +31,13 @@ func (ms *MediaSearchUI) renderTable(rows ...[]string) (*table.Table, error) { // style for table header row if row == -1 { return lipgloss. - NewStyle(). - Foreground(lipgloss.Color("#FF79C6")). - Bold(true). - Align(lipgloss.Center) + NewStyle(). + Foreground(lipgloss.Color("#FF79C6")). + Bold(true). + Align(lipgloss.Center) } - if row % 2 == 0 && (col == 0 || col == 2 || col == 3) { + if row%2 == 0 && (col == 0 || col == 2 || col == 3) { return lipgloss.NewStyle().Align(lipgloss.Center).Faint(true) } @@ -49,7 +49,7 @@ func (ms *MediaSearchUI) renderTable(rows ...[]string) (*table.Table, error) { PaddingLeft(2). PaddingRight(2). Width((tw - 6) / 3).Inline(true) - if row % 2 == 0 { + if row%2 == 0 { colStyle = colStyle.Faint(true) } return colStyle @@ -63,7 +63,7 @@ func (ms *MediaSearchUI) renderTable(rows ...[]string) (*table.Table, error) { }). Headers("ID", "TITLE", "FORMAT", "SCORE"). Rows(rows...).Width(tw) - + return table, nil } @@ -93,4 +93,4 @@ func (ms *MediaSearchUI) Render() error { fmt.Println(table) return nil -} \ No newline at end of file +} diff --git a/internal/ui/profile.go b/internal/ui/profile.go index df05a35..c129e9d 100644 --- a/internal/ui/profile.go +++ b/internal/ui/profile.go @@ -53,7 +53,7 @@ func (p *ProfileUI) Render() error { valueStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#8BE9FD")) var sb strings.Builder - + // iterating over dataSlice and adding the KV pairs to String Builder // with appropriate padding for _, kv := range dataSlice { diff --git a/internal/ui/styles.go b/internal/ui/styles.go index 7f08ce7..4db4b21 100644 --- a/internal/ui/styles.go +++ b/internal/ui/styles.go @@ -41,4 +41,4 @@ func ActionSpinner(title string, action func(context.Context) error) error { Title(title). ActionWithErr(action). Run() -} \ No newline at end of file +} diff --git a/internal/viewmodel/login_handler.go b/internal/viewmodel/login_handler.go index 091127a..b9635df 100644 --- a/internal/viewmodel/login_handler.go +++ b/internal/viewmodel/login_handler.go @@ -34,7 +34,7 @@ func HandleLogin() error { return err } - // gets user profile details from api and saves + // gets user profile details from api and saves // the username and ID to db var profile *responses.Profile err = ui.ActionSpinner("Logging In...", func(ctx context.Context) error { diff --git a/internal/viewmodel/media_list_handler.go b/internal/viewmodel/media_list_handler.go index 3f5011f..d0097c5 100644 --- a/internal/viewmodel/media_list_handler.go +++ b/internal/viewmodel/media_list_handler.go @@ -32,9 +32,9 @@ func HandleMediaList(mediaType, mediaStatus string) error { var mediaStatuIn []string if mediaStatus == "CURRENT" { - mediaStatuIn = []string{ mediaStatus, "REPEATING" } + mediaStatuIn = []string{mediaStatus, "REPEATING"} } else { - mediaStatuIn = []string{ mediaStatus } + mediaStatuIn = []string{mediaStatus} } var mediaList *responses.MediaList @@ -52,7 +52,7 @@ func HandleMediaList(mediaType, mediaStatus string) error { MediaType: mediaType, MediaList: mediaList, } - + err = mediaListUI.Render() return err -} \ No newline at end of file +} diff --git a/internal/viewmodel/media_search_handler.go b/internal/viewmodel/media_search_handler.go index cbac35e..51e1b54 100644 --- a/internal/viewmodel/media_search_handler.go +++ b/internal/viewmodel/media_search_handler.go @@ -28,4 +28,4 @@ func HandleMediaSearch(searchQuery string, mediaType string, perPage int) error } err = mediaSearchUI.Render() return err -} \ No newline at end of file +} diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index 24acbe8..e82554a 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -16,13 +16,12 @@ import ( "github.com/charmbracelet/huh/spinner" ) - type MediaUpdateParams struct { IsNewAddition bool - MediaId int - Progress string - Status string - StartDate string + MediaId int + Progress string + Status string + StartDate string } func getCurrentProgress(userId int, mediaId int) (current int, total *int, err error) { @@ -30,7 +29,7 @@ func getCurrentProgress(userId int, mediaId int) (current int, total *int, err e err = spinner.New().Title("Getting your list...").Action(func() { mediaList, err = api.GetMediaList( userId, - []string{ "CURRENT", "REPEATING" }, + []string{"CURRENT", "REPEATING"}, ) }).Run() @@ -63,7 +62,7 @@ func getCurrentProgress(userId int, mediaId int) (current int, total *int, err e func handleNewAddition(params MediaUpdateParams) error { payload := map[string]any{ - "id": params.MediaId, + "id": params.MediaId, "status": internal.MediaStatusEnumMapper(params.Status), } @@ -100,7 +99,7 @@ func handleNewAddition(params MediaUpdateParams) error { fmt.Println( ui.SuccessText( fmt.Sprintf( - "Added %s to %s", + "Added %s to %s", response.Data.SaveMediaListEntry.Media.Title.UserPreferred, statusString, ), @@ -172,10 +171,10 @@ func handleMediaCompletedAction(params MediaUpdateParams, progress int) error { } fmt.Println( - ui.SuccessText( - fmt.Sprintf( - "Marked %s as completed", - response.Data.SaveMediaListEntry.Media.Title.UserPreferred), + ui.SuccessText( + fmt.Sprintf( + "Marked %s as completed", + response.Data.SaveMediaListEntry.Media.Title.UserPreferred), ), ) @@ -212,7 +211,7 @@ func HandleMediaUpdate(params MediaUpdateParams) error { if *total != 0 && accumulatedProgress > *total { return fmt.Errorf("entered value is greater than total episodes / chapters, which is %d", *total) } - + if accumulatedProgress == *total { var markAsCompleted string fmt.Print("Mark as media completed (y/N)? ") @@ -229,7 +228,7 @@ func HandleMediaUpdate(params MediaUpdateParams) error { var response *responses.MediaUpdateResponse err = ui.ActionSpinner("Updating entry...", func(ctx context.Context) error { response, err = api.UpdateMediaEntry(map[string]any{ - "id": params.MediaId, + "id": params.MediaId, "progress": accumulatedProgress, }) return err @@ -238,8 +237,8 @@ func HandleMediaUpdate(params MediaUpdateParams) error { fmt.Println( ui.SuccessText( fmt.Sprintf( - "Progress updated for %s (%d -> %d)\n", - response.Data.SaveMediaListEntry.Media.Title.UserPreferred, + "Progress updated for %s (%d -> %d)\n", + response.Data.SaveMediaListEntry.Media.Title.UserPreferred, current, accumulatedProgress), ), ) @@ -269,4 +268,4 @@ func parseRelativeProgress(progress string, current int) (int, error) { accumulatedProgress = pgrInt } return accumulatedProgress, nil -} \ No newline at end of file +} From 1f14cc0b0c17dace977f9718da80e05c6700df5b Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 03:47:36 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=90=9B=20Parse=20empty=20progress=20a?= =?UTF-8?q?s=20current=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd_update.go | 2 +- internal/viewmodel/media_update_handler.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/cmd_update.go b/cmd/cmd_update.go index 4734f91..9ee9e92 100644 --- a/cmd/cmd_update.go +++ b/cmd/cmd_update.go @@ -75,7 +75,7 @@ func init() { &progress, "progress", "p", - "0", + "", "The number of episodes/chapter to update", ) } diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index e82554a..29dd4bb 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -248,6 +248,9 @@ func HandleMediaUpdate(params MediaUpdateParams) error { func parseRelativeProgress(progress string, current int) (int, error) { var accumulatedProgress int + if len(progress) == 0 { + return current, nil + } if strings.Contains(progress, "+") || strings.Contains(progress, "-") { if progress[:1] == "+" { prgInt, _ := strconv.Atoi(progress[1:]) From af998ea08dffcae9537354e21f1fcf099c1bda5d Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 03:50:24 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=E2=9C=A8=20Add=20status=20flag=20in=20upda?= =?UTF-8?q?te=20entry=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd_update.go | 6 +++++- internal/viewmodel/media_update_handler.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/cmd_update.go b/cmd/cmd_update.go index 9ee9e92..ebc18b9 100644 --- a/cmd/cmd_update.go +++ b/cmd/cmd_update.go @@ -11,6 +11,7 @@ import ( // // TODO: Update progress relatively. For example "+2", "-10" etc., var progress string +var updateStatus string // func handleUpdate(mediaId int) { // CheckIfTokenExists() @@ -53,7 +54,7 @@ func handleUpdate(cmd *cobra.Command, args []string) { IsNewAddition: false, MediaId: id, Progress: progress, - Status: "none", + Status: updateStatus, StartDate: "none", }, ) @@ -78,4 +79,7 @@ func init() { "", "The number of episodes/chapter to update", ) + mediaUpdateCmd.Flags().StringVarP( + &updateStatus, "status", "s", "none", "Status of the media. Can be 'watching/w or reading/r', 'planning/p', 'completed/c', 'dropped/d', 'paused/ps'", + ) } diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index 29dd4bb..1aec8f3 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -207,6 +207,21 @@ func HandleMediaUpdate(params MediaUpdateParams) error { return err } + status := internal.MediaStatusEnumMapper(params.Status) + if status == "COMPLETED" { + if *total != 0 && accumulatedProgress < *total { + var markAsCompleted string + fmt.Print("Accumulated progress is less than total episodes / chapters. Mark as media completed (y/N)? ") + fmt.Scan(&markAsCompleted) + + if strings.ToLower(markAsCompleted) != "y" { + return nil + } + } + err = handleMediaCompletedAction(params, accumulatedProgress) + return err + } + if total != nil { if *total != 0 && accumulatedProgress > *total { return fmt.Errorf("entered value is greater than total episodes / chapters, which is %d", *total) @@ -230,6 +245,7 @@ func HandleMediaUpdate(params MediaUpdateParams) error { response, err = api.UpdateMediaEntry(map[string]any{ "id": params.MediaId, "progress": accumulatedProgress, + "status": status, }) return err }) From 8f3b9b8014dbc6926484cd239d1e26dcae772f8d Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 03:52:36 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9C=A8=20Add=20notes=20flag=20in=20updat?= =?UTF-8?q?e=20entry=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd_update.go | 9 +++++++++ internal/viewmodel/media_update_handler.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/cmd/cmd_update.go b/cmd/cmd_update.go index ebc18b9..47b7d99 100644 --- a/cmd/cmd_update.go +++ b/cmd/cmd_update.go @@ -12,6 +12,7 @@ import ( // // TODO: Update progress relatively. For example "+2", "-10" etc., var progress string var updateStatus string +var notes string // func handleUpdate(mediaId int) { // CheckIfTokenExists() @@ -56,6 +57,7 @@ func handleUpdate(cmd *cobra.Command, args []string) { Progress: progress, Status: updateStatus, StartDate: "none", + Notes: notes, }, ) @@ -82,4 +84,11 @@ func init() { mediaUpdateCmd.Flags().StringVarP( &updateStatus, "status", "s", "none", "Status of the media. Can be 'watching/w or reading/r', 'planning/p', 'completed/c', 'dropped/d', 'paused/ps'", ) + mediaUpdateCmd.Flags().StringVarP( + ¬es, + "notes", + "n", + "", + "Text notes. Note: you can add multiple lines by typing \"\\n\" and wrapping the note in double quotes", + ) } diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index 1aec8f3..300ef98 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -22,6 +22,7 @@ type MediaUpdateParams struct { Progress string Status string StartDate string + Notes string } func getCurrentProgress(userId int, mediaId int) (current int, total *int, err error) { @@ -240,12 +241,18 @@ func HandleMediaUpdate(params MediaUpdateParams) error { } } + var notes string + if len(params.Notes) > 0 { + notes = strings.ReplaceAll(params.Notes, `\n`, "\n") + } + var response *responses.MediaUpdateResponse err = ui.ActionSpinner("Updating entry...", func(ctx context.Context) error { response, err = api.UpdateMediaEntry(map[string]any{ "id": params.MediaId, "progress": accumulatedProgress, "status": status, + "notes": notes, }) return err }) From d612e39188035317278828cc80cfa0513f7a9117 Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 03:56:37 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=90=9B=20Set=20start=20date=20when=20?= =?UTF-8?q?a=20new=20entry=20gets=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/viewmodel/media_update_handler.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index 300ef98..1143d24 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -78,6 +78,14 @@ func handleNewAddition(params MediaUpdateParams) error { payload["sMonth"] = int(startDateRaw.Month()) payload["sYear"] = startDateRaw.Year() } + } else { + startDate := time.Now() + + if params.Status == "CURRENT" { + payload["sDate"] = startDate.Day() + payload["sMonth"] = int(startDate.Month()) + payload["sYear"] = startDate.Year() + } } var response *responses.MediaUpdateResponse From 085fff237d04fde01e6b19d96078af2030ab1bbb Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 04:04:58 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bad=20if=20check=20whe?= =?UTF-8?q?n=20handling=20start=20date?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/viewmodel/media_update_handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index 1143d24..ac0c27d 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -73,7 +73,7 @@ func handleNewAddition(params MediaUpdateParams) error { return err } - if params.Status == "CURRENT" { + if payload["status"] == "CURRENT" { payload["sDate"] = startDateRaw.Day() payload["sMonth"] = int(startDateRaw.Month()) payload["sYear"] = startDateRaw.Year() @@ -81,7 +81,7 @@ func handleNewAddition(params MediaUpdateParams) error { } else { startDate := time.Now() - if params.Status == "CURRENT" { + if payload["status"] == "CURRENT" { payload["sDate"] = startDate.Day() payload["sMonth"] = int(startDate.Month()) payload["sYear"] = startDate.Year() From faf6f346e513042b6129c87960852cd7fa999858 Mon Sep 17 00:00:00 2001 From: mist8kengas Date: Fri, 28 Feb 2025 04:36:53 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=E2=9C=A8=20Add=20score=20flag=20in=20updat?= =?UTF-8?q?e=20entry=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd_update.go | 42 ++++++++++++++++------ internal/viewmodel/media_update_handler.go | 9 +++-- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cmd/cmd_update.go b/cmd/cmd_update.go index 47b7d99..a746c33 100644 --- a/cmd/cmd_update.go +++ b/cmd/cmd_update.go @@ -13,6 +13,7 @@ import ( var progress string var updateStatus string var notes string +var scoreString string // func handleUpdate(mediaId int) { // CheckIfTokenExists() @@ -50,16 +51,32 @@ func handleUpdate(cmd *cobra.Command, args []string) { ) } - err = viewmodel.HandleMediaUpdate( - viewmodel.MediaUpdateParams{ - IsNewAddition: false, - MediaId: id, - Progress: progress, - Status: updateStatus, - StartDate: "none", - Notes: notes, - }, - ) + var scoreFloat *float64 + if scoreString != "" { + rawScoreFloat, err := strconv.ParseFloat(scoreString, 32) + if err != nil { + fmt.Println(ui.ErrorText(err)) + return + } + scoreFloat = &rawScoreFloat + } + + params := viewmodel.MediaUpdateParams{ + IsNewAddition: false, + MediaId: id, + Progress: progress, + Status: updateStatus, + StartDate: "none", + } + // TODO: add a way to differentiate between an + // empty notes value vs. an unset notes value + if notes != "\n" { + params.Notes = notes + } + if scoreFloat != nil { + params.Score = float32(*scoreFloat) + } + err = viewmodel.HandleMediaUpdate(params) if err != nil { fmt.Println(ui.ErrorText(err)) @@ -88,7 +105,10 @@ func init() { ¬es, "notes", "n", - "", + "\n", "Text notes. Note: you can add multiple lines by typing \"\\n\" and wrapping the note in double quotes", ) + mediaUpdateCmd.Flags().StringVarP( + &scoreString, "score", "r", "", "The score of the entry. If your score is in emoji, type 1 for 😞, 2 for 😐 and 3 for 😊", + ) } diff --git a/internal/viewmodel/media_update_handler.go b/internal/viewmodel/media_update_handler.go index ac0c27d..62e7d78 100644 --- a/internal/viewmodel/media_update_handler.go +++ b/internal/viewmodel/media_update_handler.go @@ -23,6 +23,7 @@ type MediaUpdateParams struct { Status string StartDate string Notes string + Score float32 } func getCurrentProgress(userId int, mediaId int) (current int, total *int, err error) { @@ -256,12 +257,16 @@ func HandleMediaUpdate(params MediaUpdateParams) error { var response *responses.MediaUpdateResponse err = ui.ActionSpinner("Updating entry...", func(ctx context.Context) error { - response, err = api.UpdateMediaEntry(map[string]any{ + payload := map[string]any{ "id": params.MediaId, "progress": accumulatedProgress, "status": status, "notes": notes, - }) + } + if params.Score > 0 { + payload["score"] = params.Score + } + response, err = api.UpdateMediaEntry(payload) return err })