Implemented Get one team method
This commit is contained in:
parent
8a06db5351
commit
7dc4cd2173
3 changed files with 30 additions and 3 deletions
|
@ -7,8 +7,8 @@ type Team struct {
|
|||
Description string `xorm:"varchar(250)" json:"description"`
|
||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||
|
||||
CreatedBy User `xorm:"-" json:"created_by"`
|
||||
Members []*User `xorm:"-" json:"members"`
|
||||
CreatedBy User `xorm:"-" json:"created_by"`
|
||||
Members []*TeamUser `xorm:"-" json:"members"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
@ -26,6 +26,13 @@ func (Team) TableName() string {
|
|||
func (t *Team) AfterLoad() {
|
||||
// Get the owner
|
||||
t.CreatedBy, _, _ = GetUserByID(t.CreatedByID)
|
||||
|
||||
// Get all members
|
||||
x.Select("*").
|
||||
Table("users").
|
||||
Join("INNER", "team_members", "team_members.user_id = users.id").
|
||||
Where("team_id = ?", t.ID).
|
||||
Find(&t.Members)
|
||||
}
|
||||
|
||||
// TeamMember defines the relationship between a user and a team
|
||||
|
@ -47,6 +54,11 @@ func (TeamMember) TableName() string {
|
|||
return "team_members"
|
||||
}
|
||||
|
||||
type TeamUser struct {
|
||||
User `xorm:"extends"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
|
||||
// TeamNamespace defines the relationship between a Team and a Namespace
|
||||
type TeamNamespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
|
@ -101,3 +113,9 @@ func GetTeamByID(id int64) (team Team, err error) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
// ReadOne implements the CRUD method to get one team
|
||||
func (t *Team) ReadOne(id int64) (err error) {
|
||||
*t, err = GetTeamByID(id)
|
||||
return
|
||||
}
|
||||
|
|
1
models/teams_read_one.go
Normal file
1
models/teams_read_one.go
Normal file
|
@ -0,0 +1 @@
|
|||
package models
|
|
@ -31,4 +31,12 @@ func (t *Team) IsAdmin(user *User) bool {
|
|||
And("is_admin = ?", true).
|
||||
Get(&TeamMember{})
|
||||
return exists
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Team) CanRead(user *User) bool {
|
||||
// Check if the user is in the team
|
||||
exists, _ := x.Where("team_id = ?", t.ID).
|
||||
And("user_id = ?", user.ID).
|
||||
Get(&TeamMember{})
|
||||
return exists
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue