Skip to content

Commit

Permalink
removed the second the alert message
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed Jun 4, 2024
1 parent 1b6ea90 commit d4f32a3
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 45 deletions.
4 changes: 0 additions & 4 deletions cmd/budget/sub_handler/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +33,6 @@ var SetupCmd = &cobra.Command{
Weekdays: weekday,
Hours: hourInt,
Minutes: minuteInt,
Seconds: secondInt,
}

err := h.Deps.ManageAlerts.AlertSetup(&av)
Expand All @@ -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")
}
4 changes: 0 additions & 4 deletions cmd/budget/sub_handler/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,7 +35,6 @@ var UpdateCmd = &cobra.Command{
Weekdays: weekday,
Hours: hourInt,
Minutes: minuteInt,
Seconds: secondInt,
}

err := h.Deps.AlertDB.UpdateAlert(&av)
Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 0 additions & 1 deletion entities/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ type AlertVariables struct {
Weekdays string
Hours int
Minutes int
Seconds int
}
32 changes: 13 additions & 19 deletions framework/db/alert_db/alert_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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
}
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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion framework/db/migrations/002_create_alert_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
8 changes: 4 additions & 4 deletions interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions usecases/app/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}
Expand Down
12 changes: 6 additions & 6 deletions usecases/app/spend/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit d4f32a3

Please sign in to comment.