Skip to content

Commit

Permalink
Added due time to goals (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
XDoubleU authored Nov 23, 2024
1 parent 3558004 commit 663a9b6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/api/migrations/00001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CREATE TABLE IF NOT EXISTS goals (
is_linked boolean NOT NULL,
target_value integer,
type_id integer,
state varchar(255) NOT NULL
state varchar(255) NOT NULL,
due_time timestamp
);

CREATE TABLE IF NOT EXISTS progress (
Expand Down
16 changes: 14 additions & 2 deletions cmd/api/templates/html/goals/goalWithSubgoals.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{{define "goalWithSubGoals"}}
<li>
{{if .Goal.IsLinked}}
<li><a href="/{{.Goal.ID}}">{{.Goal.Name}}</a> (<a href="/edit/{{.Goal.ID}}">edit</a>) (<a href="/unlink/{{.Goal.ID}}">unlink</a>)</li>
<a href="/{{.Goal.ID}}">{{.Goal.Name}}</a>
{{else}}
<li>{{.Goal.Name}} (<a href="/link/{{.Goal.ID}}">link</a>)</li>
{{.Goal.Name}}
{{end}}

{{if .Goal.DueTime}}
(due <b>{{.Goal.DueTime.Format "2006-02-01"}}</b>)
{{end}}

{{if .Goal.IsLinked}}
(<a href="/edit/{{.Goal.ID}}">edit</a>) (<a href="/unlink/{{.Goal.ID}}">unlink</a>)
{{else}}
(<a href="/link/{{.Goal.ID}}">link</a>)
{{end}}
</li>

{{if .HasSubgoals}}
<ul>
{{range $goalWithSubGoals := .Subgoals}}
Expand Down
25 changes: 17 additions & 8 deletions internal/models/goal.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package models

import (
"time"

"goal-tracker/api/pkg/todoist"
)

type Goal struct {
ID string `json:"id"`
ParentID *string `json:"parentId"`
UserID string `json:"userId"`
Name string `json:"name"`
IsLinked bool `json:"isLinked"`
TargetValue *int64 `json:"targetValue"`
TypeID *int64 `json:"typeId"`
State string `json:"state"`
ID string `json:"id"`
ParentID *string `json:"parentId"`
UserID string `json:"userId"`
Name string `json:"name"`
IsLinked bool `json:"isLinked"`
TargetValue *int64 `json:"targetValue"`
TypeID *int64 `json:"typeId"`
State string `json:"state"`
DueTime *time.Time `json:"time"`
} // @name Goal

func NewGoalFromTask(task todoist.Task, userID string, state string) Goal {
var dueTime *time.Time
if task.Due != nil {
dueTime = &task.Due.Date.Time
}

return Goal{
ID: task.ID,
ParentID: task.ParentID,
Expand All @@ -25,5 +33,6 @@ func NewGoalFromTask(task todoist.Task, userID string, state string) Goal {
TargetValue: nil,
TypeID: nil,
State: state,
DueTime: dueTime,
}
}
14 changes: 10 additions & 4 deletions internal/repositories/goals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repositories

import (
"context"
"time"

"github.com/XDoubleU/essentia/pkg/database"
"github.com/XDoubleU/essentia/pkg/database/postgres"
Expand All @@ -18,7 +19,7 @@ func (repo GoalRepository) GetAll(
userID string,
) ([]models.Goal, error) {
query := `
SELECT id, name, is_linked, target_value, type_id, state
SELECT id, name, is_linked, target_value, type_id, state, due_time
FROM goals
WHERE user_id = $1
`
Expand All @@ -42,6 +43,7 @@ func (repo GoalRepository) GetAll(
&goal.TargetValue,
&goal.TypeID,
&goal.State,
&goal.DueTime,
)
if err != nil {
return nil, postgres.PgxErrorToHTTPError(err)
Expand All @@ -63,7 +65,7 @@ func (repo GoalRepository) GetByID(
userID string,
) (*models.Goal, error) {
query := `
SELECT name, target_value, source_id, type_id, state
SELECT name, target_value, source_id, type_id, state, due_time
FROM goals
WHERE goals.id = $1 AND user_id = $2
`
Expand All @@ -81,6 +83,7 @@ func (repo GoalRepository) GetByID(
&goal.TargetValue,
&goal.TypeID,
&goal.State,
&goal.DueTime,
)
if err != nil {
return nil, postgres.PgxErrorToHTTPError(err)
Expand All @@ -99,11 +102,12 @@ func (repo GoalRepository) Create(
targetValue int64,
typeID int64,
state string,
dueTime *time.Time,
) (*models.Goal, error) {
query := `
INSERT INTO goals (id, parent_id, user_id, name,
is_linked, target_value, type_id, state)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
is_linked, target_value, type_id, state, due_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id
`

Expand All @@ -116,6 +120,7 @@ func (repo GoalRepository) Create(
TargetValue: &targetValue,
TypeID: &typeID,
State: state,
DueTime: dueTime,
}

err := repo.db.QueryRow(
Expand All @@ -129,6 +134,7 @@ func (repo GoalRepository) Create(
targetValue,
typeID,
state,
dueTime,
).Scan(&goal.ID)

if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/services/goals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"context"
"time"

"github.com/XDoubleU/essentia/pkg/errors"

Expand Down Expand Up @@ -98,6 +99,11 @@ func (service GoalService) Link(
return err
}

var dueTime *time.Time
if task.Due != nil {
dueTime = &task.Due.Date.Time
}

_, err = service.goals.Create(
ctx,
id,
Expand All @@ -108,6 +114,7 @@ func (service GoalService) Link(
linkGoalDto.TargetValue,
linkGoalDto.TypeID,
sectionsMap[task.SectionID],
dueTime,
)
return err
}
39 changes: 33 additions & 6 deletions pkg/todoist/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"time"
)

const TasksEndpoint = "tasks"
Expand All @@ -19,7 +20,7 @@ type Task struct {
ParentID *string `json:"parent_id"`
Order int `json:"order"`
Priority int `json:"priority"`
Due Due `json:"due"`
Due *Due `json:"due"`
URL string `json:"url"`
CommentCount int `json:"comment_count"`
CreatedAt string `json:"created_at"`
Expand All @@ -30,11 +31,37 @@ type Task struct {
}

type Due struct {
String string `json:"string"`
Date string `json:"date"`
IsRecurring bool `json:"is_recurring"`
Datetime string `json:"datetime"`
Timezone string `json:"timezone"`
String string `json:"string"`
Date Date `json:"date"`
IsRecurring bool `json:"is_recurring"`
Datetime DateTime `json:"datetime"`
Timezone string `json:"timezone"`
}

type Date struct {
time.Time
}

type DateTime struct {
time.Time
}

func (d *Date) UnmarshalJSON(bytes []byte) error {
date, err := time.Parse(`"2006-01-02"`, string(bytes))
if err != nil {
return err
}
d.Time = date
return nil
}

func (d *DateTime) UnmarshalJSON(bytes []byte) error {
date, err := time.Parse(`"2006-01-02T15:04:05"`, string(bytes))
if err != nil {
return err
}
d.Time = date
return nil
}

type Duration struct {
Expand Down

0 comments on commit 663a9b6

Please sign in to comment.