From d4f32a37eaf5e9ed786ee4e15dd0e1d9fc22e9fd Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Tue, 4 Jun 2024 11:26:07 +0500 Subject: [PATCH] removed the second the alert message --- cmd/budget/sub_handler/setup.go | 4 --- cmd/budget/sub_handler/update.go | 4 --- cmd/root.go | 2 +- entities/alert.go | 1 - framework/db/alert_db/alert_db.go | 32 ++++++++----------- .../db/migrations/002_create_alert_table.sql | 1 - interfaces/interfaces.go | 8 ++--- usecases/app/alert/alert.go | 9 +++--- usecases/app/spend/notification.go | 12 +++---- 9 files changed, 28 insertions(+), 45 deletions(-) diff --git a/cmd/budget/sub_handler/setup.go b/cmd/budget/sub_handler/setup.go index 766181e..8f8a67c 100644 --- a/cmd/budget/sub_handler/setup.go +++ b/cmd/budget/sub_handler/setup.go @@ -19,13 +19,11 @@ var SetupCmd = &cobra.Command{ weekday, _ := cmd.Flags().GetString("weekday") hour, _ := cmd.Flags().GetString("hour") minute, _ := cmd.Flags().GetString("minute") - second, _ := cmd.Flags().GetString("second") h := TakeHandler() dayInt := h.Deps.Common.StringToInt(day) hourInt := h.Deps.Common.StringToInt(hour) minuteInt := h.Deps.Common.StringToInt(minute) - secondInt := h.Deps.Common.StringToInt(second) av := entities.AlertVariables{ Category: category, @@ -35,7 +33,6 @@ var SetupCmd = &cobra.Command{ Weekdays: weekday, Hours: hourInt, Minutes: minuteInt, - Seconds: secondInt, } err := h.Deps.ManageAlerts.AlertSetup(&av) @@ -53,5 +50,4 @@ func init() { SetupCmd.Flags().StringP("weekday", "w", "", "Write a weekday to set the notification") SetupCmd.Flags().StringP("hour", "o", "", "Write the hour to set the notification") SetupCmd.Flags().StringP("minute", "m", "", "Write the minute to set the notification") - SetupCmd.Flags().StringP("second", "s", "", "Write the second to set the notification") } diff --git a/cmd/budget/sub_handler/update.go b/cmd/budget/sub_handler/update.go index 4e199d0..2482396 100644 --- a/cmd/budget/sub_handler/update.go +++ b/cmd/budget/sub_handler/update.go @@ -20,13 +20,11 @@ var UpdateCmd = &cobra.Command{ weekday, _ := cmd.Flags().GetString("weekday") hour, _ := cmd.Flags().GetString("hour") minute, _ := cmd.Flags().GetString("minute") - second, _ := cmd.Flags().GetString("second") h := TakeHandler() dayInt := h.Deps.Common.StringToInt(day) hourInt := h.Deps.Common.StringToInt(hour) minuteInt := h.Deps.Common.StringToInt(minute) - secondInt := h.Deps.Common.StringToInt(second) av := entities.AlertVariables{ Category: old_category, @@ -37,7 +35,6 @@ var UpdateCmd = &cobra.Command{ Weekdays: weekday, Hours: hourInt, Minutes: minuteInt, - Seconds: secondInt, } err := h.Deps.AlertDB.UpdateAlert(&av) @@ -56,5 +53,4 @@ func init() { UpdateCmd.Flags().StringP("weekday", "w", "", "Write the minute to set the notification") UpdateCmd.Flags().StringP("hour", "o", "", "Write the hour to set the notification") UpdateCmd.Flags().StringP("minute", "m", "", "Write the minute to set the notification") - UpdateCmd.Flags().StringP("second", "s", "", "Write the second to set the notification") } diff --git a/cmd/root.go b/cmd/root.go index bdb7fc3..57f5c22 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.1.126" +const version = "v0.1.127" // rootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ diff --git a/entities/alert.go b/entities/alert.go index d5e17da..14352b8 100644 --- a/entities/alert.go +++ b/entities/alert.go @@ -9,5 +9,4 @@ type AlertVariables struct { Weekdays string Hours int Minutes int - Seconds int } diff --git a/framework/db/alert_db/alert_db.go b/framework/db/alert_db/alert_db.go index 4fb64e1..ccdfe9b 100644 --- a/framework/db/alert_db/alert_db.go +++ b/framework/db/alert_db/alert_db.go @@ -22,7 +22,7 @@ func (h MyAlertDB) CreateAlert(av *entities.AlertVariables) error { return err } - query := "INSERT INTO Alert(categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes, alert_seconds) VALUES($1, $2, $3, $4, $5, $6, $7, $8)" + query := "INSERT INTO Alert(categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes) VALUES($1, $2, $3, $4, $5, $6, $7)" insert, err := data.Prepare(query) if err != nil { return err @@ -30,7 +30,7 @@ func (h MyAlertDB) CreateAlert(av *entities.AlertVariables) error { defer insert.Close() if len(av.Category) != 0 && len(av.Method) != 0 && len(av.Frequency) != 0 { - _, err = insert.Exec(av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes, av.Seconds) + _, err = insert.Exec(av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes) if err != nil { return err } @@ -40,45 +40,45 @@ func (h MyAlertDB) CreateAlert(av *entities.AlertVariables) error { return nil } -func (h MyAlertDB) ViewAlert(category string) ([9]interface{}, error) { +func (h MyAlertDB) ViewAlert(category string) ([8]interface{}, error) { av := new(entities.AlertVariables) db, err := h.Deps.Connect.Connection() if err != nil { - return [9]interface{}{}, err + return [8]interface{}{}, err } tw := table.NewWriter() - tw.AppendHeader(table.Row{"Categories", "Methods", "Frequencies", "Days", "Weekdays", "Hours", "Minutes", "Seconds"}) + tw.AppendHeader(table.Row{"Categories", "Methods", "Frequencies", "Days", "Weekdays", "Hours", "Minutes"}) var rows *sql.Rows if len(category) != 0 { - query := "SELECT categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes, alert_seconds FROM Alert WHERE categories=$1" + query := "SELECT categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes FROM Alert WHERE categories=$1" rows, err = db.Query(query, category) } else { - query := "SELECT categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes, alert_seconds FROM Alert" + query := "SELECT categories, alert_methods, alert_frequencies, alert_days, alert_weekdays, alert_hours, alert_minutes FROM Alert" rows, err = db.Query(query) } if err != nil { - return [9]interface{}{}, err + return [8]interface{}{}, err } defer rows.Close() for rows.Next() { - if err := rows.Scan(&av.Category, &av.Method, &av.Frequency, &av.Days, &av.Weekdays, &av.Hours, &av.Minutes, &av.Seconds); err != nil { - return [9]interface{}{}, err + if err := rows.Scan(&av.Category, &av.Method, &av.Frequency, &av.Days, &av.Weekdays, &av.Hours, &av.Minutes); err != nil { + return [8]interface{}{}, err } - tw.AppendRow([]interface{}{av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes, av.Seconds}) + tw.AppendRow([]interface{}{av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes}) tw.AppendSeparator() } if err := rows.Err(); err != nil { - return [9]interface{}{}, err + return [8]interface{}{}, err } tableRender := "Alert Info\n" + tw.Render() - values := [9]interface{}{tableRender, av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes, av.Seconds} + values := [8]interface{}{tableRender, av.Category, av.Method, av.Frequency, av.Days, av.Weekdays, av.Hours, av.Minutes} return values, nil } @@ -206,12 +206,6 @@ func (h MyAlertDB) UpdateAlert(av *entities.AlertVariables) error { paramCount++ updatedFields = true } - if av.Seconds != 0 { - query += "alert_seconds=$" + strconv.Itoa(paramCount) + ", " - params = append(params, av.Seconds) - paramCount++ - updatedFields = true - } if !updatedFields { return errors.New("field is not found to update") diff --git a/framework/db/migrations/002_create_alert_table.sql b/framework/db/migrations/002_create_alert_table.sql index 297e0b9..1e155dc 100644 --- a/framework/db/migrations/002_create_alert_table.sql +++ b/framework/db/migrations/002_create_alert_table.sql @@ -7,6 +7,5 @@ CREATE TABLE IF NOT EXISTS Alert ( alert_weekdays VARCHAR(255) NOT NULL, alert_hours INT NOT NULL, alert_minutes INT NOT NULL, - alert_seconds INT NOT NULL, UNIQUE(categories) ); \ No newline at end of file diff --git a/interfaces/interfaces.go b/interfaces/interfaces.go index 438eda9..a2a13e9 100644 --- a/interfaces/interfaces.go +++ b/interfaces/interfaces.go @@ -47,9 +47,9 @@ type SpendAmount interface { StoreHistory(category string, spending_amount int) error HourlyNotification(category string) - DailyNotification(hour, min, sec int, category string) - WeeklyNotification(weekday time.Weekday, hour, min, sec int, category string) - MonthlyNotification(day, hour, min, sec int, category string) + DailyNotification(hour, min int, category string) + WeeklyNotification(weekday time.Weekday, hour, min int, category string) + MonthlyNotification(day, hour, min int, category string) } type ManageBudget interface { @@ -85,7 +85,7 @@ type ManageAlerts interface { type AlertDB interface { CreateAlert(av *entities.AlertVariables) error - ViewAlert(category string) ([9]interface{}, error) + ViewAlert(category string) ([8]interface{}, error) RemoveAlert(category string) error UpdateAlert(av *entities.AlertVariables) error } diff --git a/usecases/app/alert/alert.go b/usecases/app/alert/alert.go index 13881ff..7f832da 100644 --- a/usecases/app/alert/alert.go +++ b/usecases/app/alert/alert.go @@ -60,9 +60,8 @@ func (h MyAlert) SendAlert(category string) error { weekdayStr, ok4 := value[5].(string) hour, ok5 := value[6].(int) minute, ok6 := value[7].(int) - second, ok7 := value[8].(int) - if !ok1 || !ok2 || !ok3 || !ok4 || !ok5 || !ok6 || !ok7 { + if !ok1 || !ok2 || !ok3 || !ok4 || !ok5 || !ok6 { return errors.New("unable to convert string to int and string") } @@ -93,11 +92,11 @@ func (h MyAlert) SendAlert(category string) error { case "hourly": h.Deps.SpendAmount.HourlyNotification(category) case "daily": - h.Deps.SpendAmount.DailyNotification(hour, minute, second, category) + h.Deps.SpendAmount.DailyNotification(hour, minute, category) case "weekly": - h.Deps.SpendAmount.WeeklyNotification(weekday, hour, minute, second, category) + h.Deps.SpendAmount.WeeklyNotification(weekday, hour, minute, category) case "monthly": - h.Deps.SpendAmount.MonthlyNotification(day, hour, minute, second, category) + h.Deps.SpendAmount.MonthlyNotification(day, hour, minute, category) default: return errors.New("wrong or no frequency is selected") } diff --git a/usecases/app/spend/notification.go b/usecases/app/spend/notification.go index 546b701..b90d98d 100644 --- a/usecases/app/spend/notification.go +++ b/usecases/app/spend/notification.go @@ -15,9 +15,9 @@ func (h MySpending) HourlyNotification(category string) { } } -func (h MySpending) DailyNotification(hour, min, sec int, category string) { +func (h MySpending) DailyNotification(hour, min int, category string) { now := time.Now() - next := time.Date(now.Year(), now.Month(), now.Day(), hour, min, sec, 0, now.Location()) + next := time.Date(now.Year(), now.Month(), now.Day(), hour, min, 0, 0, now.Location()) if next.Before(now) { next = next.Add(24 * time.Hour) } @@ -27,20 +27,20 @@ func (h MySpending) DailyNotification(hour, min, sec int, category string) { fmt.Println("Printed daily at the specified time") } -func (h MySpending) WeeklyNotification(weekday time.Weekday, hour, min, sec int, category string) { +func (h MySpending) WeeklyNotification(weekday time.Weekday, hour, min int, category string) { now := time.Now() daysUntilNextWeekday := int((weekday - now.Weekday() + 7) % 7) - next := time.Date(now.Year(), now.Month(), now.Day()+daysUntilNextWeekday, hour, min, sec, 0, now.Location()) + next := time.Date(now.Year(), now.Month(), now.Day()+daysUntilNextWeekday, hour, min, 0, 0, now.Location()) fmt.Printf("Next weekly print will be on %s at %s\n", weekday, next) time.Sleep(next.Sub(now)) h.Deps.HandleEmail.SendAlertEmail(category) fmt.Println("Printed weekly on the specified day and time") } -func (h MySpending) MonthlyNotification(day, hour, min, sec int, category string) { +func (h MySpending) MonthlyNotification(day, hour, min int, category string) { now := time.Now() year, month, _ := now.Date() - next := time.Date(year, month, day, hour, min, sec, 0, now.Location()) + next := time.Date(year, month, day, hour, min, 0, 0, now.Location()) if next.Before(now) { next = next.AddDate(0, 1, 0) }