Skip to content

Commit

Permalink
Adding UserByEmail feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ransoni committed Mar 3, 2020
1 parent d5d791e commit 3bbf82a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
type Harvest struct {
API *structs.API
User *User
Users *Users
Project string
TimeEntries *TimeEntries
}
Expand All @@ -41,6 +42,16 @@ type Entries []structs.Entries
// User ...
type User structs.User

// type Users structs.UserList
// Users ...
type Users struct {
Users []User `json:"users"`
}

// type Users []User

// type Users []structs.User

// Links ...
type Links structs.Links

Expand All @@ -56,6 +67,7 @@ type GetTimeEntriesParams struct {
PerPage int8 `url:"per_page"`
}

// API is something...
type API structs.API

// Init methot initializes the data structure needed for Harvest
Expand All @@ -66,10 +78,12 @@ func Init(conf *config.Config) *Harvest {
}

e := &TimeEntries{}
// u := &Users{}

H := &Harvest{
API: a,
User: &User{},
API: a,
User: &User{},
// Users: u,
Project: "",
TimeEntries: e,
}
Expand Down
5 changes: 5 additions & 0 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type API struct {
BaseURL string
}

// UserList is list of all users
type UserList struct {
Users []User `json:"users"`
}

// User is a object of a user fetched from REST API
type User struct {
ID int64 `json:"id"`
Expand Down
24 changes: 24 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,27 @@ func (h *Harvest) GetUser() (*User, error) {

return &user, nil
}

// GetUserByEmail is ...
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
}

var usersList Users
json.Unmarshal(body, &usersList)

for _, v := range usersList.Users {
if v.Email == email {
// user = v
return v, nil
}
}

return user, fmt.Errorf("could not found user")

}

0 comments on commit 3bbf82a

Please sign in to comment.