You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
package models
|
|
|
|
import "context"
|
|
|
|
// A Group is a collection of lights that relates to one another. The API should
|
|
// allow all lights within it.
|
|
type Group struct {
|
|
ID int `json:"id" db:"id"`
|
|
Name string `json:"name" db:"name"`
|
|
Permissions []GroupPermission `json:"permissions" db:"-"`
|
|
}
|
|
|
|
// Permission gets the permissions for the user with id `userID` or returns
|
|
// a blank permission if none is found.
|
|
func (group *Group) Permission(userID int) GroupPermission {
|
|
for _, permission := range group.Permissions {
|
|
if permission.UserID == userID {
|
|
return permission
|
|
}
|
|
}
|
|
|
|
return GroupPermission{
|
|
GroupID: group.ID,
|
|
UserID: userID,
|
|
}
|
|
}
|
|
|
|
// A GroupPermission is a permission for a user in a group.
|
|
type GroupPermission struct {
|
|
GroupID int `json:"-" db:"group_id"`
|
|
UserID int `json:"userId" db:"user_id"`
|
|
Read bool `json:"read" db:"read"`
|
|
Write bool `json:"write" db:"write"`
|
|
Create bool `json:"create" db:"create"`
|
|
Delete bool `json:"delete" db:"delete"`
|
|
Manage bool `json:"manage" db:"manage"`
|
|
}
|
|
|
|
// GroupRepository is an interface for all database operations
|
|
// the Group model makes.
|
|
type GroupRepository interface {
|
|
FindByID(ctx context.Context, id int) (Group, error)
|
|
FindByLight(ctx context.Context, light Light) (Group, error)
|
|
List(ctx context.Context) ([]Group, error)
|
|
ListByUser(ctx context.Context, user User) ([]Group, error)
|
|
UpdatePermissions(ctx context.Context, permission GroupPermission) error
|
|
Insert(ctx context.Context, group Group) (Group, error)
|
|
Update(ctx context.Context, group Group) error
|
|
Remove(ctx context.Context, group Group) error
|
|
}
|