Skip to content

Commit

Permalink
edit activity with minutes - seconds is kinda dumb
Browse files Browse the repository at this point in the history
  • Loading branch information
prmaloney committed Jan 23, 2024
1 parent b685a4e commit 73c7e89
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 102 deletions.
21 changes: 14 additions & 7 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ var projectCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
projects, _ := data.GetProjects()
t := table.New().
Border(lipgloss.NormalBorder()).
Headers("ID", "Name")
Border(lipgloss.RoundedBorder()).
Headers("ID", "Name").
StyleFunc(func(row, col int) lipgloss.Style {
return lipgloss.NewStyle().Padding(0, 1)
})
for _, project := range projects {
t.Row(fmt.Sprintf("%d", project.Id), project.GetName())
}
Expand All @@ -32,7 +35,10 @@ var taskCmd = &cobra.Command{
projectId, err := cmd.Flags().GetInt("project")

t := table.New().
Border(lipgloss.NormalBorder()).
Border(lipgloss.RoundedBorder()).
StyleFunc(func(row, col int) lipgloss.Style {
return lipgloss.NewStyle().Padding(0, 1)
}).
Headers("ID", "Name")
if projectId != 0 && err == nil {
project, err := data.GetProject(projectId)
Expand Down Expand Up @@ -84,17 +90,18 @@ var activityCmd = &cobra.Command{
}
}
t := table.New().
Border(lipgloss.NormalBorder()).
Border(lipgloss.RoundedBorder()).
Headers("ID", "Date", "Time", "Description").
StyleFunc(func(row int, col int) lipgloss.Style {
style := lipgloss.NewStyle().Padding(0, 1)
if (runningIndex < 1) {
return lipgloss.Style{}
return style
}

if runningIndex == row {
return lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Bold(true)
return style.Foreground(lipgloss.Color("9")).Bold(true)
}
return lipgloss.Style{}
return style
}).
Rows(rows...)
fmt.Println(t)
Expand Down
190 changes: 95 additions & 95 deletions data/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,58 @@ func StartActivity(activityId int) error {
config := config.Init()
apiKey := config.GetString("api_key")
if apiKey == "" {
return fmt.Errorf("api_key not set")
return fmt.Errorf("api_key not set")
}
domain := config.GetString("domain")
if domain == "" {
return fmt.Errorf("domain not set")
return fmt.Errorf("domain not set")
}

req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, activityId), nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode == 422 {
return fmt.Errorf("Something went wrong")
return fmt.Errorf("Something went wrong")
} else if resp.StatusCode == 404 {
return fmt.Errorf("Activity not found")
}
return fmt.Errorf("Activity not found")
}
defer resp.Body.Close()
return nil
return nil
}

func StopActivity(activityId int) error {
config := config.Init()
apiKey := config.GetString("api_key")
if apiKey == "" {
return fmt.Errorf("api_key not set")
return fmt.Errorf("api_key not set")
}
domain := config.GetString("domain")
if domain == "" {
return fmt.Errorf("domain not set")
return fmt.Errorf("domain not set")
}

req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/stop_timer",domain, activityId), nil)
req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/stop_timer", domain, activityId), nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode == 422 {
return fmt.Errorf("Error on response.\n[ERROR] - %s", err)
return fmt.Errorf("Error on response.\n[ERROR] - %s", err)
}
defer resp.Body.Close()
return nil
return nil
}

func CreateActivity(projectId int, taskId int, description string) error {
config := config.Init()
apiKey := config.GetString("api_key")
domain := config.GetString("domain")
domain := config.GetString("domain")
if apiKey == "" {
return fmt.Errorf("api_key not set")
return fmt.Errorf("api_key not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}

type ActivityBody struct {
ProjectId int `json:"project_id"`
Expand All @@ -97,101 +97,101 @@ func CreateActivity(projectId int, taskId int, description string) error {
Date string `json:"date"`
}

body := ActivityBody{
ProjectId: projectId,
TaskId: taskId,
Description: description,
Date: time.Now().Format("2006-01-02"),
}
marshaledBody, err := json.Marshal(body)
if err != nil {
log.Fatal(err)
}
body := ActivityBody{
ProjectId: projectId,
TaskId: taskId,
Description: description,
Date: time.Now().Format("2006-01-02"),
}
marshaledBody, err := json.Marshal(body)
if err != nil {
log.Fatal(err)
}

req, _ := http.NewRequest("POST", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities", domain), bytes.NewReader(marshaledBody))
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode == 422 {
return err
}
defer resp.Body.Close()
return nil
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode == 422 {
return err
}
defer resp.Body.Close()
return nil
}

func EditActivity(id int, seconds int, description string) error {
config := config.Init()
apiKey := config.GetString("api_key")
domain := config.GetString("domain")
if apiKey == "" {
return fmt.Errorf("api_key not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}

type Body struct {
Description string `json:"description,omitempty"`
Seconds int `json:"seconds,omitempty"`
}

body := Body{
Description: description,
Seconds: seconds,
}
marshaledBody, err := json.Marshal(body)

req, _ := http.NewRequest("PUT", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), bytes.NewReader(marshaledBody))
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
return err
}
defer resp.Body.Close()
return nil
func EditActivity(id int, minutes int, description string) error {
config := config.Init()
apiKey := config.GetString("api_key")
domain := config.GetString("domain")
if apiKey == "" {
return fmt.Errorf("api_key not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}

type Body struct {
Description string `json:"description,omitempty"`
Seconds int `json:"seconds,omitempty"`
}

body := Body{
Description: description,
Seconds: minutes * 60,
}
marshaledBody, err := json.Marshal(body)

req, _ := http.NewRequest("PUT", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), bytes.NewReader(marshaledBody))
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
return err
}
defer resp.Body.Close()
return nil
}

func DeleteActivity(id int) error {
config := config.Init()
apiKey := config.GetString("api_key")
domain := config.GetString("domain")
if apiKey == "" {
return fmt.Errorf("api_key not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}

req, _ := http.NewRequest("DELETE", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
return err
}
defer resp.Body.Close()
return nil
config := config.Init()
apiKey := config.GetString("api_key")
domain := config.GetString("domain")
if apiKey == "" {
return fmt.Errorf("api_key not set")
}
if domain == "" {
return fmt.Errorf("domain not set")
}

req, _ := http.NewRequest("DELETE", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d", domain, id), nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode >= 400 && resp.StatusCode <= 499 {
return err
}
defer resp.Body.Close()
return nil
}

func GetActivities() ([]Activity, error) {
config := config.Init()

apiKey := config.GetString("api_key")
domain := config.GetString("domain")
domain := config.GetString("domain")
if apiKey == "" {
return nil, fmt.Errorf("api_key not set")
return nil, fmt.Errorf("api_key not set")
}
if domain == "" {
return nil, fmt.Errorf("domain not set")
}
if domain == "" {
return nil, fmt.Errorf("domain not set")
}

userId, err := GetUserId()
if err != nil {
return nil, err
}
userId, err := GetUserId()
if err != nil {
return nil, err
}
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities?user_id=%d", domain, userId), nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey))
client := &http.Client{}
Expand All @@ -205,7 +205,7 @@ func GetActivities() ([]Activity, error) {

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, err
}
json.Unmarshal(body, &activities)

Expand Down

0 comments on commit 73c7e89

Please sign in to comment.