limit the number of entries

pull/10/head
isthisnagee 3 years ago
parent d7b53d74c5
commit 326ffebd24

@ -8,6 +8,7 @@ import (
"git.nagee.dev/isthisnagee/diary/model" "git.nagee.dev/isthisnagee/diary/model"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// listCmd represents the list command // listCmd represents the list command
@ -20,11 +21,22 @@ var listCmd = &cobra.Command{
$ diary list today $ diary list today
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{}) var num_entries *int64 = new(int64)
q := model.GetDiaryEntriesQuery{}
*num_entries = viper.GetInt64("listNumEntries")
if (*num_entries > 0) {
q.NumEntries = num_entries
}
results := App.Db.GetDiaryEntries(q)
PrintEntries(results) PrintEntries(results)
}, },
} }
func init() { func init() {
listCmd.PersistentFlags().Int64P("num-entries", "n", 20, "The number of entries to list")
viper.BindPFlag("listNumEntries", listCmd.PersistentFlags().Lookup("num-entries"))
rootCmd.AddCommand(listCmd) rootCmd.AddCommand(listCmd)
} }

@ -8,10 +8,9 @@ import (
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var cfgFile string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "diary", Use: "diary",
@ -37,6 +36,7 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(InitApp) cobra.OnInitialize(InitApp)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.diary.toml)") rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.diary.toml)")
viper.BindPFlag("config", rootCmd.Flags().Lookup("config"))
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }

@ -7,6 +7,7 @@ package cmd
import ( import (
"time" "time"
"github.com/spf13/viper"
"git.nagee.dev/isthisnagee/diary/model" "git.nagee.dev/isthisnagee/diary/model"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -23,6 +24,7 @@ var todayCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var created_after_ts *int64 = new(int64) var created_after_ts *int64 = new(int64)
var created_before_ts *int64 = new(int64) var created_before_ts *int64 = new(int64)
var num_entries *int64 = new(int64)
var now = time.Now() var now = time.Now()
var startOfToday = time.Date( var startOfToday = time.Date(
@ -39,9 +41,13 @@ var todayCmd = &cobra.Command{
var endOfToday = startOfToday.AddDate(0, 0, 1) var endOfToday = startOfToday.AddDate(0, 0, 1)
*created_before_ts = endOfToday.Unix() *created_before_ts = endOfToday.Unix()
if viper.GetInt64("listNumEntries") > 0 {
*num_entries = viper.GetInt64("listNumEntries")
}
results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{ results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{
CreatedBeforeTs: created_before_ts, CreatedBeforeTs: created_before_ts,
CreatedAfterTs: created_after_ts, CreatedAfterTs: created_after_ts,
NumEntries: num_entries,
}) })
PrintEntries(results) PrintEntries(results)

@ -8,7 +8,6 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"log"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -35,13 +34,14 @@ func initConfig() Cfg {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
cobra.CheckErr(err) cobra.CheckErr(err)
cfgFile := viper.GetString("config")
if cfgFile != "" { if cfgFile != "" {
// Use config file from the flag. // Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
viper.AddConfigPath(path.Join(home, ".config", "diary")) viper.AddConfigPath(path.Join(home, ".config", "diary"))
viper.SetConfigType("toml") viper.SetConfigType("toml")
viper.SetConfigName("diary.toml") viper.SetConfigName("diary")
} }
if viper.Get("db_path") == nil { if viper.Get("db_path") == nil {
@ -51,8 +51,13 @@ func initConfig() Cfg {
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
err = viper.ReadInConfig() if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found. That's OK
} else {
cobra.CheckErr(err) cobra.CheckErr(err)
}
}
return Cfg{viper.GetString("db_path")} return Cfg{viper.GetString("db_path")}
} }

@ -5,7 +5,6 @@ package model
import ( import (
"database/sql" "database/sql"
"git.nagee.dev/isthisnagee/diary/db" "git.nagee.dev/isthisnagee/diary/db"
"log"
"strings" "strings"
) )
@ -105,6 +104,8 @@ type GetDiaryEntriesQuery struct {
CreatedBeforeTs *int64 CreatedBeforeTs *int64
/// Inclusive /// Inclusive
CreatedAfterTs *int64 CreatedAfterTs *int64
NumEntries *int64
} }
func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry { func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
@ -121,6 +122,10 @@ func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
whereParams = append(whereParams, *q.CreatedAfterTs) whereParams = append(whereParams, *q.CreatedAfterTs)
} }
query += " ORDER BY created_at desc, id desc" query += " ORDER BY created_at desc, id desc"
if q.NumEntries != nil {
query += " LIMIT ?"
whereParams = append(whereParams, *q.NumEntries)
}
rows, err := app.Db.Query( rows, err := app.Db.Query(
query, query,

@ -100,3 +100,18 @@ func DeleteDiaryEntryNotFound(t *testing.T) {
teardown(app) teardown(app)
} }
func TestGetDiaryEntries(t *testing.T) {
var app = setup()
var result_1 = app.NewDiaryEntry("Met with Nagee @ 1PM")
var result_2 = app.NewDiaryEntry("Met with Nagee @ 2PM")
queried_result, _ := app.GetDiaryEntries(inserted_result.Id)
assert_int(t, inserted_result.Id, queried_result.Id)
assert_int(t, inserted_result.CreatedAt, queried_result.CreatedAt)
assert_int(t, inserted_result.Version, queried_result.Version)
assert_string(t, inserted_result.Title, queried_result.Title)
teardown(app)
}

Loading…
Cancel
Save