Skip to content

Commit

Permalink
Changing config
Browse files Browse the repository at this point in the history
  • Loading branch information
ransoni committed Mar 5, 2020
1 parent 3bbf82a commit fa9686c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
9 changes: 6 additions & 3 deletions hours.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand Down
67 changes: 57 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion time_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

}

0 comments on commit fa9686c

Please sign in to comment.