Skip to content

Commit

Permalink
Changed func arg list in users module
Browse files Browse the repository at this point in the history
  • Loading branch information
borisershov committed Jul 22, 2021
1 parent 84d75e7 commit a89a2e3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
87 changes: 54 additions & 33 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,30 @@ import (
"strconv"
)

// UserStatus defines user status type
type UserStatus int

// UserNotification defines user notification type
type UserNotification string

// UserStatus const
const (
UserStatusActive = 1
UserStatusRegistered = 2
UserStatusLocked = 3
UserStatusAnonymous UserStatus = 0
UserStatusActive UserStatus = 1
UserStatusRegistered UserStatus = 2
UserStatusLocked UserStatus = 3
)

// UserNotification const
const (
UserNotificationAll = 1
UserNotificationSelected = 2
UserNotificationOnlyMyEvents = 3
UserNotificationOnlyAssigned = 4
UserNotificationOnlyOwner = 5
UserNotificationOnlyNone = 6
UserNotificationAll UserNotification = "all"
UserNotificationSelected UserNotification = "selected"
UserNotificationOnlyMyEvents UserNotification = "only_my_events"
UserNotificationOnlyAssigned UserNotification = "only_assigned"
UserNotificationOnlyOwner UserNotification = "only_owner"
UserNotificationOnlyNone UserNotification = "none"
)

// UserStatus names in Redmine
var UserStatus = map[int]string{
UserStatusActive: "active",
UserStatusRegistered: "registered",
UserStatusLocked: "locked",
}

// UserNotification names in Redmine
var UserNotification = map[int]string{
UserNotificationAll: "all",
UserNotificationSelected: "selected",
UserNotificationOnlyMyEvents: "only_my_events",
UserNotificationOnlyAssigned: "only_assigned",
UserNotificationOnlyOwner: "only_owner",
UserNotificationOnlyNone: "none",
}

/* Get */

// UserObject struct used for users get operations
Expand All @@ -52,7 +42,7 @@ type UserObject struct {
CreatedOn string `json:"created_on"`
LastLoginOn string `json:"last_login_on"`
APIKey string `json:"api_key"` // used only: get single user
Status int `json:"status"` // used only: get single user
Status UserStatus `json:"status"` // used only: get single user
CustomFields []CustomFieldGetObject `json:"custom_fields"`
Groups []IDName `json:"groups"` // used only: get single user
Memberships []UserMembershipObject `json:"memberships"` // used only: get single user
Expand Down Expand Up @@ -113,9 +103,19 @@ type UserMultiGetRequest struct {
Limit int
}

// UserSingleGetRequest contains data for making request to get specified user
type UserSingleGetRequest struct {
Includes []string
}

// UserCurrentGetRequest contains data for making request to get current user
type UserCurrentGetRequest struct {
Includes []string
}

// UserGetRequestFilters contains data for making users get request
type UserGetRequestFilters struct {
Status int
Status UserStatus
Name string
GroupID int
}
Expand Down Expand Up @@ -144,6 +144,27 @@ type userUpdate struct {
User UserUpdateObject `json:"user"`
}

func (u UserStatus) String() string {

status := map[UserStatus]string{
UserStatusAnonymous: "anonymous",
UserStatusActive: "active",
UserStatusRegistered: "registered",
UserStatusLocked: "locked",
}

s, b := status[u]
if b == false {
return "unknown"
}

return s
}

func (u UserNotification) String() string {
return string(u)
}

// UserAllGet gets info for all users satisfying specified filters
//
// see: http://www.redmine.org/projects/redmine/wiki/Rest_Users#GET
Expand Down Expand Up @@ -222,14 +243,14 @@ func (r *Context) UserMultiGet(request UserMultiGetRequest) (UserResult, int, er
// Available includes:
// * groups
// * memberships
func (r *Context) UserSingleGet(id int, includes []string) (UserObject, int, error) {
func (r *Context) UserSingleGet(id int, request UserSingleGetRequest) (UserObject, int, error) {

var u userSingleResult

urlParams := url.Values{}

// Preparing includes
urlIncludes(&urlParams, includes)
urlIncludes(&urlParams, request.Includes)

ur := url.URL{
Path: "/users/" + strconv.Itoa(id) + ".json",
Expand All @@ -248,14 +269,14 @@ func (r *Context) UserSingleGet(id int, includes []string) (UserObject, int, err
// Available includes:
// * groups
// * memberships
func (r *Context) UserCurrentGet(includes []string) (UserObject, int, error) {
func (r *Context) UserCurrentGet(request UserCurrentGetRequest) (UserObject, int, error) {

var u userSingleResult

urlParams := url.Values{}

// Preparing includes
urlIncludes(&urlParams, includes)
urlIncludes(&urlParams, request.Includes)

ur := url.URL{
Path: "/users/current.json",
Expand Down Expand Up @@ -317,7 +338,7 @@ func userURLFilters(urlParams *url.Values, filters UserGetRequestFilters) {
filters.Status = 1
}

urlParams.Add("status", strconv.Itoa(filters.Status))
urlParams.Add("status", strconv.Itoa(int(filters.Status)))

if len(filters.Name) > 0 {
urlParams.Add("name", filters.Name)
Expand Down
12 changes: 8 additions & 4 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func testUserCreate(t *testing.T, r Context) UserObject {
FirstName: testUserFirstName,
LastName: testUserLastName,
Mail: testUserMail,
MailNotification: UserNotification[UserNotificationOnlyAssigned],
MailNotification: UserNotificationOnlyAssigned.String(),
MustChangePasswd: true,
GeneratePassword: true,
})
Expand All @@ -60,7 +60,7 @@ func testUserUpdate(t *testing.T, r Context, id int) {
_, err := r.UserUpdate(id, UserUpdateObject{
FirstName: testUserFirstName2,
LastName: testUserLastName2,
MailNotification: UserNotification[UserNotificationOnlyNone],
MailNotification: UserNotificationOnlyNone.String(),
})
if err != nil {
t.Fatal("User update error:", err)
Expand Down Expand Up @@ -104,7 +104,9 @@ func testUserAllGet(t *testing.T, r Context) {

func testUserSingleGet(t *testing.T, r Context, id int) {

_, _, err := r.UserSingleGet(id, []string{"groups", "memberships"})
_, _, err := r.UserSingleGet(id, UserSingleGetRequest{
Includes: []string{"groups", "memberships"},
})
if err != nil {
t.Fatal("User get error:", err)
}
Expand All @@ -114,7 +116,9 @@ func testUserSingleGet(t *testing.T, r Context, id int) {

func testUserCurrentGet(t *testing.T, r Context) {

_, _, err := r.UserCurrentGet([]string{"groups", "memberships"})
_, _, err := r.UserCurrentGet(UserCurrentGetRequest{
Includes: []string{"groups", "memberships"},
})
if err != nil {
t.Fatal("Current user get error:", err)
}
Expand Down

0 comments on commit a89a2e3

Please sign in to comment.