Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Structs #6

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mapache

import "time"

type User struct {
ID string `json:"id" gorm:"primaryKey"`
FirstName string `json:"first_name" gorm:"index"`
LastName string `json:"last_name"`
Email string `json:"email" gorm:"index,unique"`
Password string `json:"password"`
Subteam string `json:"subteam"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;precision:6"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;precision:6"`
Roles []string `json:"roles" gorm:"-"`
}

func (User) TableName() string {
return "user"
}

func (u User) HasRole(role string) bool {
for _, r := range u.Roles {
if r == role {
return true
}
}
return false
}

type UserRole struct {
UserID string `json:"user_id" gorm:"primaryKey"`
Role string `json:"role" gorm:"primaryKey"`
CreatedAt time.Time `json:"time" gorm:"autoCreateTime;precision:6"`
}

func (UserRole) TableName() string {
return "user_role"
}
34 changes: 34 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mapache

import "testing"

func TestUser_TableName(t *testing.T) {
u := User{}
if u.TableName() != "user" {
t.Error("Expected user, got", u.TableName())
}
}

func TestUser_HasRole(t *testing.T) {
t.Run("Test HasRole", func(t *testing.T) {
u := User{
Roles: []string{"admin", "user"},
}
if !u.HasRole("admin") {
t.Error("Expected true, got", u.HasRole("admin"))
}
if !u.HasRole("user") {
t.Error("Expected true, got", u.HasRole("user"))
}
if u.HasRole("guest") {
t.Error("Expected false, got", u.HasRole("guest"))
}
})
}

func TestUserRole_TableName(t *testing.T) {
ur := UserRole{}
if ur.TableName() != "user_role" {
t.Error("Expected user_role, got", ur.TableName())
}
}
Loading