Skip to content

Commit

Permalink
feat: adding dashboard field to user data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Dec 5, 2023
1 parent 4a867fb commit b819668
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
11 changes: 10 additions & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/base64"
"errors"
"fmt"
"regexp"

"github.com/go-vela/types/constants"
Expand Down Expand Up @@ -49,6 +50,7 @@ type User struct {
Favorites pq.StringArray `sql:"favorites" gorm:"type:varchar(5000)"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
Dashboards pq.StringArray `sql:"dashboards"`
}

// Decrypt will manipulate the existing user tokens by
Expand Down Expand Up @@ -199,7 +201,7 @@ func (u *User) Nullify() *User {

// ToLibrary converts the User type
// to a library User type.
func (u *User) ToLibrary() *library.User {
func (u *User) ToLibrary(dashboards []library.Dashboard) *library.User {
user := new(library.User)

user.SetID(u.ID.Int64)
Expand All @@ -210,6 +212,7 @@ func (u *User) ToLibrary() *library.User {
user.SetActive(u.Active.Bool)
user.SetAdmin(u.Admin.Bool)
user.SetFavorites(u.Favorites)
user.SetDashboards(dashboards)

return user
}
Expand Down Expand Up @@ -262,6 +265,11 @@ func (u *User) Validate() error {
// UserFromLibrary converts the library User type
// to a database User type.
func UserFromLibrary(u *library.User) *User {
dahsboardIDs := []string{}
for _, dashboard := range u.GetDashboards() {
dahsboardIDs = append(dahsboardIDs, fmt.Sprint(dashboard.GetID()))
}

user := &User{
ID: sql.NullInt64{Int64: u.GetID(), Valid: true},
Name: sql.NullString{String: u.GetName(), Valid: true},
Expand All @@ -271,6 +279,7 @@ func UserFromLibrary(u *library.User) *User {
Active: sql.NullBool{Bool: u.GetActive(), Valid: true},
Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true},
Favorites: pq.StringArray(u.GetFavorites()),
Dashboards: dahsboardIDs,
}

return user.Nullify()
Expand Down
6 changes: 5 additions & 1 deletion database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestDatabase_User_Nullify(t *testing.T) {
Hash: sql.NullString{String: "", Valid: false},
Active: sql.NullBool{Bool: false, Valid: false},
Admin: sql.NullBool{Bool: false, Valid: false},
Dashboards: nil,
}

// setup tests
Expand Down Expand Up @@ -157,9 +158,10 @@ func TestDatabase_User_ToLibrary(t *testing.T) {
want.SetFavorites([]string{"github/octocat"})
want.SetActive(true)
want.SetAdmin(false)
want.SetDashboards(nil)

// run test
got := testUser().ToLibrary()
got := testUser().ToLibrary(nil)

if !reflect.DeepEqual(got, want) {
t.Errorf("ToLibrary is %v, want %v", got, want)
Expand Down Expand Up @@ -244,6 +246,7 @@ func TestDatabase_UserFromLibrary(t *testing.T) {
u.SetFavorites([]string{"github/octocat"})
u.SetActive(true)
u.SetAdmin(false)
u.SetDashboards(nil)

want := testUser()

Expand All @@ -267,6 +270,7 @@ func testUser() *User {
Favorites: []string{"github/octocat"},
Active: sql.NullBool{Bool: true, Valid: true},
Admin: sql.NullBool{Bool: false, Valid: true},
Dashboards: nil,
}
}

Expand Down
65 changes: 57 additions & 8 deletions library/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
//
// swagger:model User
type User struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RefreshToken *string `json:"-"`
Token *string `json:"-"`
Hash *string `json:"-"` // deprecated
Favorites *[]string `json:"favorites,omitempty"`
Active *bool `json:"active,omitempty"`
Admin *bool `json:"admin,omitempty"`
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RefreshToken *string `json:"-"`
Token *string `json:"-"`
Hash *string `json:"-"` // deprecated
Favorites *[]string `json:"favorites,omitempty"`
Active *bool `json:"active,omitempty"`
Admin *bool `json:"admin,omitempty"`
Dashboards *[]Dashboard `json:"dashboards,omitempty"`
}

// Sanitize creates a duplicate of the User without the token values.
Expand All @@ -38,6 +39,7 @@ func (u *User) Sanitize() *User {
Favorites: u.Favorites,
Active: u.Active,
Admin: u.Admin,
Dashboards: u.Dashboards,
}
}

Expand Down Expand Up @@ -156,6 +158,19 @@ func (u *User) GetFavorites() []string {
return *u.Favorites
}

// GetDashboards returns the Dashboards field.
//
// When the provided User type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (u *User) GetDashboards() []Dashboard {
// return zero value if User type or Favorites field is nil
if u == nil || u.Dashboards == nil {
return []Dashboard{}
}

return *u.Dashboards
}

// SetID sets the ID field.
//
// When the provided User type is nil, it
Expand Down Expand Up @@ -260,18 +275,52 @@ func (u *User) SetFavorites(v []string) {
u.Favorites = &v
}

// SetDashboard sets the Dashboard field.
//
// When the provided User type is nil, it
// will set nothing and immediately return.
func (u *User) SetDashboards(v []Dashboard) {
// return if User type is nil
if u == nil {
return
}

u.Dashboards = &v
}

// SetDefaultDashboard sets the default Dashboard.
//
// When the provided User type is nil, it
// will set nothing and immediately return.
func (u *User) SetDefaultDashboard(d Dashboard) {
dashboards := *u.Dashboards
dID := d.GetID()

for a, dashboard := range u.GetDashboards() {
if dashboard.GetID() == dID {
hold := dashboards[0]
dashboards[0] = dashboard
dashboards[a] = hold
}
}

u.Dashboards = &dashboards
}

// String implements the Stringer interface for the User type.
func (u *User) String() string {
return fmt.Sprintf(`{
Active: %t,
Admin: %t,
Dashboards: %d,
Favorites: %s,
ID: %d,
Name: %s,
Token: %s,
}`,
u.GetActive(),
u.GetAdmin(),
len(u.GetDashboards()),
u.GetFavorites(),
u.GetID(),
u.GetName(),
Expand Down

0 comments on commit b819668

Please sign in to comment.