Skip to content

Commit

Permalink
entries: implemented edit
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
1 parent 0a7b470 commit 521e89b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
78 changes: 77 additions & 1 deletion cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var (
createNote string
createProjectId string
createServiceId string
editTimeEntryId string
editDate string
editDuration string
editNote string
editProjectId string
editServiceId string
)

func init() {
Expand All @@ -40,6 +46,14 @@ func init() {
entriesCreateCommand.Flags().StringVarP(&createProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
entriesCreateCommand.Flags().StringVarP(&createServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
entriesCommand.AddCommand(entriesCreateCommand)
// edit
entriesEditCommand.Flags().StringVarP(&editDate, "date", "D", now.Format("2006-01-02"), "day for which to edit entry (in YYYY-MM-DD format)")
entriesEditCommand.Flags().StringVarP(&editDuration, "duration", "d", "", "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesEditCommand.Flags().StringVarP(&editNote, "note", "n", "", "a note describing what was worked on")
entriesEditCommand.Flags().StringVarP(&editTimeEntryId, "id", "i", "", "the time entry id to edit")
entriesEditCommand.Flags().StringVarP(&editProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
entriesEditCommand.Flags().StringVarP(&editServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
entriesCommand.AddCommand(entriesEditCommand)
rootCmd.AddCommand(entriesCommand)
}

Expand Down Expand Up @@ -93,7 +107,7 @@ func printEntries(entries []*mite.TimeEntry) {

var entriesCreateCommand = &cobra.Command{
Use: "create",
Short: "create time entries",
Short: "creates a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
if createProjectId == "" {
createProjectId = deps.conf.Get("projectId")
Expand Down Expand Up @@ -129,3 +143,65 @@ var entriesCreateCommand = &cobra.Command{
return nil
},
}

var entriesEditCommand = &cobra.Command{
Use: "edit",
Short: "edits a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
entry, err := deps.miteApi.TimeEntry(editTimeEntryId)
if err != nil {
return err
}

// use retrieved values as defaults
timeEntry := mite.TimeEntryCommand{
Date: &entry.Date,
Duration: &entry.Duration,
Note: entry.Note,
ProjectId: entry.ProjectId,
ServiceId: entry.ServiceId,
}

// override only fields affected by set parameters of edit
if editDate != "" {
eDate, err := time.Parse("2006-01-02", editDate)
if err != nil {
return err
}
timeEntry.Date = &eDate
}

if editDuration != "" {
eDuration, err := time.ParseDuration(editDuration)
if err != nil {
return err
}
timeEntry.Duration = &eDuration
}

if editNote != "" {
timeEntry.Note = editNote
}

if editProjectId != "" {
timeEntry.ProjectId = editProjectId
}

if editServiceId != "" {
timeEntry.ServiceId = editServiceId
}

err = deps.miteApi.EditTimeEntry(editTimeEntryId, &timeEntry)
if err != nil {
return err
}

entry, err = deps.miteApi.TimeEntry(editTimeEntryId)
if err != nil {
return err
}

printEntries([]*mite.TimeEntry{entry})
return nil
},
}
6 changes: 6 additions & 0 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type TimeEntry struct {
Note string
Duration time.Duration
Date time.Time
ProjectId string
ProjectName string
ServiceId string
ServiceName string
}

Expand Down Expand Up @@ -84,7 +86,9 @@ type timeEntryResponse struct {
Note string `json:"note"`
Minutes int `json:"minutes"`
Date string `json:"date_at"`
ProjectId int `json:"project_id"`
ProjectName string `json:"project_name"`
ServiceId int `json:"service_id"`
ServiceName string `json:"service_name"`
} `json:"time_entry"`
}
Expand All @@ -100,7 +104,9 @@ func (r *timeEntryResponse) ToTimeEntry() *TimeEntry {
Note: r.TimeEntry.Note,
Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute,
Date: date,
ProjectId: fmt.Sprintf("%d", r.TimeEntry.ProjectId),
ProjectName: r.TimeEntry.ProjectName,
ServiceId: fmt.Sprintf("%d", r.TimeEntry.ServiceId),
ServiceName: r.TimeEntry.ServiceName,
}
}
Expand Down

0 comments on commit 521e89b

Please sign in to comment.