Skip to content

Commit

Permalink
implemented db integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRoe34 committed Jul 29, 2024
1 parent e681569 commit 26d612f
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 69 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.5

require (
github.com/charmbracelet/bubbletea v0.26.6
github.com/mattn/go-sqlite3 v1.14.22
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2J
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
Expand Down
2 changes: 1 addition & 1 deletion internal/cobra/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCmd(repo *service.TaskRepo) *Cmd {
func (c *Cmd) Execute() {
rootCmd := c.RootCmd()

rootCmd.AddCommand(c.AddCmd(), c.ModCmd(), c.GetCmd(), c.ListCmd(), c.DoneCmd(), c.UndoCmd(), c.NoteCmd(), c.ImportCmd(), c.ExportCmd())
rootCmd.AddCommand(c.AddCmd(), c.ModCmd(), c.DeleteCmd(), c.GetCmd(), c.ListCmd(), c.DoneCmd(), c.UndoCmd(), c.NoteCmd(), c.ImportCmd(), c.ExportCmd())

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
104 changes: 82 additions & 22 deletions internal/cobra/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ gt add "Setup database" @ 11-3 +project
p := 5
ti.priority = &p
}
t := types.NewTask(1, *ti.desc, *ti.priority, ti.addTags, nil, ti.startAt, ti.endAt)
fmt.Printf("Added task %d.\n", t.ID)
t := types.NewTask(*ti.desc, *ti.priority, ti.addTags, nil, ti.startAt, ti.endAt)
i, err := c.repo.AddTask(t)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Added task %d.\n", i)
},
}

Expand Down Expand Up @@ -109,6 +113,7 @@ func (c *Cmd) ModCmd() *cobra.Command {
fmt.Printf("Task %d not found.\n", *ti.id)
return
}

fmt.Printf("Task %d '%s' has been updated:\n", t.ID, t.Desc)
if ti.desc != nil {
fmt.Printf(" - Description updated to '%s'\n", *ti.desc)
Expand All @@ -126,6 +131,7 @@ func (c *Cmd) ModCmd() *cobra.Command {
}
if ti.startAt != nil {
t.StartAt = ti.startAt
t.EndAt = nil
}
if ti.endAt != nil {
t.EndAt = ti.endAt
Expand All @@ -138,6 +144,12 @@ func (c *Cmd) ModCmd() *cobra.Command {
fmt.Printf("%s %s\n", t.StartAt.Format("Mon, 02 Jan 2006"), t.StartAt.Format(time.Kitchen))
}
}
curr := time.Now()
t.UpdatedAt = &curr
err = c.repo.UpdateTask(t)
if err != nil {
log.Fatal(err)
}
fmt.Println("Update complete. 1 task modified.")
},
}
Expand All @@ -154,16 +166,15 @@ func (c *Cmd) NoteCmd() *cobra.Command {
if err != nil {
log.Fatal(err)
}
t, err := c.repo.GetTask(id)
d, err := c.repo.GetDesc(id)
if err != nil {
log.Fatal(err)
}
//check if task exists, because of nil pointer dereference
if t == nil {
fmt.Printf("Task %d not found.\n", id)
return
err = c.repo.AddNote(id, n)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Task %d '%s' has been updated with a new note:\n", t.ID, t.Desc)
fmt.Printf("Task %d '%s' has been updated with a new note:\n", id, d)
fmt.Printf(" - Note: \"%s\"\n", n)
fmt.Println("1 task updated with a note.")
},
Expand Down Expand Up @@ -206,7 +217,15 @@ func (c *Cmd) DoneCmd() *cobra.Command {
fmt.Printf("Task %d not found.\n", i)
continue
}
fmt.Printf("Finished task %d '%s'.\n", i, t.Desc)
if t.Finished {
fmt.Printf("Task %d already finished.\n", i)
} else {
err = c.repo.UpdateStatus(i, true)
if err != nil {
return
}
fmt.Printf("Finished task %d '%s'.\n", i, t.Desc)
}
}
if len(ids) > 1 {
fmt.Printf("Finished %d tasks.\n", len(ids))
Expand Down Expand Up @@ -237,7 +256,15 @@ func (c *Cmd) UndoCmd() *cobra.Command {
fmt.Printf("Task %d not found.\n", i)
continue
}
fmt.Printf("Reverted task %d '%s' to incomplete.\n", i, t.Desc)
if !t.Finished {
fmt.Printf("Task %d already incomplete.\n", i)
} else {
err = c.repo.UpdateStatus(i, false)
if err != nil {
return
}
fmt.Printf("Reverted task %d '%s' to incomplete.\n", i, t.Desc)
}
}
if len(ids) > 1 {
fmt.Printf("Reverted %d tasks.\n", len(ids))
Expand All @@ -255,14 +282,42 @@ func (c *Cmd) DeleteCmd() *cobra.Command {
Short: "Deletes tasks by ID",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
/*
Preparing to delete tasks with IDs: 1, 3, 5
- Task 1: 'finish some work'
- Task 3: 'study BSTs'
- Task 5: 'clean the house'
Are you sure you want to delete these tasks? (y/n):
*/
ids, err := parseGet(args)
if err != nil {
log.Fatal(err)
}
var tasks []*types.Task
for _, i := range ids {
t, err := c.repo.GetTask(i)
if err != nil {
log.Fatal(err)
}
tasks = append(tasks, t)
}
fmt.Printf("Preparing to delete tasks with ")
if len(ids) == 1 {
fmt.Printf("ID: %d\n", ids[0])
} else {
fmt.Printf("IDs: %d", ids[0])
for j := 1; j < len(ids); j++ {
fmt.Printf(", %d", ids[j])
}
}
for _, t := range tasks {
fmt.Printf(" - Task %d: '%s'\n", t.ID, t.Desc)
}
if len(ids) == 1 {
fmt.Printf("\nAre you sure you want to delete this task? (y/n): ")
} else {
fmt.Printf("\nAre you sure you want to delete these tasks? (y/n): ")
}
// todo need to prompt user for input (check whether first char matches 'y')
for _, i := range ids {
err = c.repo.DeleteTask(i)
if err != nil {
log.Fatal(err)
}
}
},
}
return deleteCmd
Expand Down Expand Up @@ -320,7 +375,12 @@ func displayTask(task *types.Task) {
// Display last modified time
fmt.Printf("Last modified %s\n", task.UpdatedAt.Format(time.RFC1123))

fmt.Printf("\nNotes:\nThu, 18 Jul 2024 00:50:46 EDT - unexpected issue came up, am resolving now")
fmt.Printf("\nNotes:\n")
if task.Notes != nil {
for _, n := range task.Notes {
fmt.Printf(" - %s\n", n)
}
}
}

// / formatTask prints a task in the desired format
Expand Down Expand Up @@ -350,15 +410,15 @@ func formatTask(task *types.Task) string {
}

// Format the output string with additional spaces for the 'Due' column
return fmt.Sprintf("%d %s %-30s %d %-13s %s ", // Adjusted format string with extra spaces
return fmt.Sprintf("%-6d %s %-30s %d %-13s %s ", // Adjusted format string with extra spaces
task.ID, status, task.Desc, task.Priority, tags, due)
}

// displayTasks prints a list of tasks in the desired format
func displayTasks(tasks []*types.Task) {
// Print header
fmt.Println("ID Status Desc Priority Tags Due ")
fmt.Println("------------------------------------------------------------------------------------------------------")
fmt.Println("ID Status Desc Priority Tags Due ")
fmt.Println("---------------------------------------------------------------------------------------------------------")

// Print each task
for _, task := range tasks {
Expand Down
64 changes: 46 additions & 18 deletions internal/service/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package service

import (
"database/sql"
"github.com/EvoSched/gotask/internal/sqlite"
"github.com/EvoSched/gotask/internal/types"
)

type TaskRepoQuery interface {
GetTask(id int) (*types.Task, error)
GetTasks() ([]*types.Task, error)
// todo need to add interface functions for getting tasks by priority, tags, and time
GetDesc(id int) (string, error)
GetNotes(id int) ([]string, error)
}

type TaskRepoStmt interface {
AddTask(task *types.Task) error
EditTask(id int, task *types.Task) error
RemoveTask(id int) error
CompleteTask(id int) error
IncompleteTask(id int) error
AddTask(task *types.Task) (int, error)
AddNote(id int, note string) error
UpdateStatus(id int, status bool) error
UpdateTask(task *types.Task) error
DeleteTask(db *sql.DB, id int) error
}

type TaskRepo struct {
Expand All @@ -28,24 +29,51 @@ func NewTaskRepo(db *sql.DB) *TaskRepo {
return &TaskRepo{db}
}

func (r *TaskRepo) GetDesc(id int) (string, error) {
return sqlite.QueryDesc(r.db, id)
}

func (r *TaskRepo) GetNotes(id int) ([]string, error) {
return sqlite.QueryNotes(r.db, id)
}

func (r *TaskRepo) GetTask(id int) (*types.Task, error) {
//TODO: sql query
t, err := sqlite.QueryTask(r.db, id)
if err != nil {
return nil, err
}
n, err := sqlite.QueryNotes(r.db, id)
if err != nil {
return nil, err
}
t.Notes = append(t.Notes, n...)
return &t, nil
}

for _, task := range tasks {
if task.ID == id {
return task, nil
}
func (r *TaskRepo) GetTasks() ([]*types.Task, error) {
return sqlite.QueryTasks(r.db)
}

func (r *TaskRepo) AddTask(task *types.Task) (int, error) {
err := sqlite.InsertTask(r.db, task)
if err != nil {
return 0, err
}
return sqlite.QueryLastID(r.db)
}

return nil, nil
func (r *TaskRepo) AddNote(id int, note string) error {
return sqlite.InsertNote(r.db, id, note)
}

func (r *TaskRepo) GetTasks() ([]*types.Task, error) {
//TODO: sql query
func (r *TaskRepo) UpdateStatus(id int, status bool) error {
return sqlite.UpdateStatus(r.db, id, status)
}

// todo remove when integrating SQL (this is purely for displaying mock data
tasks[1].Finished = true
tasks[3].Finished = true
func (r *TaskRepo) UpdateTask(task *types.Task) error {
return sqlite.UpdateTask(r.db, task)
}

return tasks, nil
func (r *TaskRepo) DeleteTask(id int) error {
return sqlite.DeleteTask(r.db, id)
}
8 changes: 4 additions & 4 deletions internal/service/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var date = time.Date(curr.Year(), curr.Month(), curr.Day(), 23, 59, 0, 0, time.U

// sample data to test command functions
var tasks = []*types.Task{
types.NewTask(1, "finish project3", 5, []string{"MA", "CS"}, []string{"comment1"}, &start, nil),
types.NewTask(2, "study BSTs", 8, []string{"CS"}, []string{"comment2"}, &start, &end),
types.NewTask(3, "lunch with Edgar", 2, []string{"Fun"}, []string{"comment3"}, nil, nil),
types.NewTask(4, "meeting for db proposal", 5, []string{"Project"}, []string{"comment4"}, &date, nil),
types.NewTask("finish project3", 5, []string{"MA", "CS"}, []string{"comment1"}, &start, nil),
types.NewTask("study BSTs", 8, []string{"CS"}, []string{"comment2"}, &start, &end),
types.NewTask("lunch with Edgar", 2, []string{"Fun"}, []string{"comment3"}, nil, nil),
types.NewTask("meeting for db proposal", 5, []string{"Project"}, []string{"comment4"}, &date, nil),
}
Loading

0 comments on commit 26d612f

Please sign in to comment.