From fa9686c37d5182c57a793c3aaa7bf9658d85717c Mon Sep 17 00:00:00 2001 From: Mika Tuominen Date: Thu, 5 Mar 2020 16:35:31 +0200 Subject: [PATCH] Changing config --- config/config.go | 16 +++++++++++ hours.go | 9 ++++--- main.go | 67 +++++++++++++++++++++++++++++++++++++++------- structs/structs.go | 1 + time_entries.go | 2 +- user.go | 8 +++--- 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/config/config.go b/config/config.go index 19bac1f..91f13e3 100644 --- a/config/config.go +++ b/config/config.go @@ -42,3 +42,19 @@ func LoadConfig(file string) *Config { return c } + +// func LoadConfig(file string) *Config { +// c := new(Config) + +// configFile, err := os.Open(file) +// defer configFile.Close() +// if err != nil { +// log.Printf("OPEN FILE ERROR: %v\n", err.Error()) +// return c +// } + +// confJSONParser := json.NewDecoder(configFile) +// confJSONParser.Decode(&c) + +// return c +// } diff --git a/hours.go b/hours.go index 68411b4..8cda464 100644 --- a/hours.go +++ b/hours.go @@ -1,9 +1,10 @@ package harvest import ( - "github.com/polarsquad/harvest/structs" _ "log" "time" + + "github.com/polarsquad/harvest/structs" ) // GetOvertime counts overtime hours from TimeEntries, using also dayTotal function as a helper. @@ -32,9 +33,11 @@ func (e *TimeEntries) GetOvertime(from time.Time, to time.Time) (totalOvertime f } // TotalHours counts total logged hours from the TimeEntries struct -func (h *Harvest) TotalHours() float64 { +func (e *TimeEntries) TotalHours() float64 { + // func (h *Harvest) TotalHours() float64 { var hours float64 - for _, v := range h.TimeEntries.Entries { + // for _, v := range h.TimeEntries.Entries { + for _, v := range e.Entries { hours = hours + v.Hours } // fmt.Printf("Total hours: %v\n", hours) diff --git a/main.go b/main.go index 3cfe255..747058c 100644 --- a/main.go +++ b/main.go @@ -22,11 +22,21 @@ const ( // Harvest creates the struct for the API, User and Entries type Harvest struct { - API *structs.API - User *User - Users *Users - Project string - TimeEntries *TimeEntries + // API *structs.API + API *API + User *User + // Users *Users + // Project string + // TimeEntries *TimeEntries +} + +// HarvestOLD creates the struct for the API, User and Entries +type HarvestOLD struct { + API *structs.API + User *User + // Users *Users + // Project string + // TimeEntries *TimeEntries } // type api structs.API @@ -70,22 +80,59 @@ type GetTimeEntriesParams struct { // API is something... type API structs.API +// Config is ... +type Config config.Config + +// InitHarvest methot initializes the data structure needed for Harvest +func InitHarvest(conf *Config) *Harvest { + a := &API{ + AuthToken: conf.API.AuthToken, + AccountID: conf.API.AccountID, + } + // a := &structs.API{ + // AuthToken: conf.API.AuthToken, + // AccountID: conf.API.AccountID, + // } + + // e := &TimeEntries{} + // u := &Users{} + + H := &Harvest{ + API: a, + User: &User{}, + // Users: u, + // Project: "", + // TimeEntries: e, + } + + // API.AccountID = conf.API.AccountID + // API.AuthToken = conf.API.AuthToken + // API.BaseURL = conf.API.BaseURL + // h := &Harvest{ + // User: "Mika", + // Projects: "Client", + // Entries: []TimeEntries{}, + // } + + return H +} + // Init methot initializes the data structure needed for Harvest -func Init(conf *config.Config) *Harvest { +func Init(conf *config.Config) *HarvestOLD { a := &structs.API{ AuthToken: conf.API.AuthToken, AccountID: conf.API.AccountID, } - e := &TimeEntries{} + // e := &TimeEntries{} // u := &Users{} - H := &Harvest{ + H := &HarvestOLD{ API: a, User: &User{}, // Users: u, - Project: "", - TimeEntries: e, + // Project: "", + // TimeEntries: e, } // API.AccountID = conf.API.AccountID diff --git a/structs/structs.go b/structs/structs.go index 3053732..4083eba 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -22,6 +22,7 @@ type User struct { LastName string `json:"last_name"` IsActive bool `json:"is_active"` IsAdmin bool `json:"is_admin"` + CreatedAt string `json:"created_at"` } // List of entries fetched from API diff --git a/time_entries.go b/time_entries.go index 78d3fb6..f6b1a9f 100644 --- a/time_entries.go +++ b/time_entries.go @@ -18,7 +18,7 @@ import ( // from: time.Time with format "2006-01-02" // to: time.Time with format "2006-01-02" // u: User, specifies which users TimeEntries are fetched. -func (h *Harvest) GetEntries(from time.Time, to time.Time, u User) *TimeEntries { +func (h *Harvest) GetEntries(from time.Time, to time.Time, u *User) *TimeEntries { // Let's build the URL with parameters. params := GetTimeEntriesParams{ UserID: int64(u.ID), diff --git a/user.go b/user.go index 852e277..ca2fb5e 100644 --- a/user.go +++ b/user.go @@ -54,13 +54,13 @@ func (h *Harvest) GetUser() (*User, error) { } // GetUserByEmail is ... -func (h *Harvest) GetUserByEmail(email string) (User, error) { +func (h *Harvest) GetUserByEmail(email string) (*User, error) { var user User body, err := h.getURL("GET", usersURL) if err != nil { log.Fatalf("[ERROR] Could not get users.") - return user, err + return &user, err } var usersList Users @@ -69,10 +69,10 @@ func (h *Harvest) GetUserByEmail(email string) (User, error) { for _, v := range usersList.Users { if v.Email == email { // user = v - return v, nil + return &v, nil } } - return user, fmt.Errorf("could not found user") + return &user, fmt.Errorf("could not found user") }