Implemented Get one team method

This commit is contained in:
konrad 2018-07-16 08:43:47 +02:00 committed by kolaente
parent 8a06db5351
commit 7dc4cd2173
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 30 additions and 3 deletions

View file

@ -7,8 +7,8 @@ type Team struct {
Description string `xorm:"varchar(250)" json:"description"` Description string `xorm:"varchar(250)" json:"description"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"` CreatedByID int64 `xorm:"int(11) not null" json:"-"`
CreatedBy User `xorm:"-" json:"created_by"` CreatedBy User `xorm:"-" json:"created_by"`
Members []*User `xorm:"-" json:"members"` Members []*TeamUser `xorm:"-" json:"members"`
Created int64 `xorm:"created" json:"created"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
@ -26,6 +26,13 @@ func (Team) TableName() string {
func (t *Team) AfterLoad() { func (t *Team) AfterLoad() {
// Get the owner // Get the owner
t.CreatedBy, _, _ = GetUserByID(t.CreatedByID) 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 // TeamMember defines the relationship between a user and a team
@ -47,6 +54,11 @@ func (TeamMember) TableName() string {
return "team_members" return "team_members"
} }
type TeamUser struct {
User `xorm:"extends"`
IsAdmin bool `json:"is_admin"`
}
// TeamNamespace defines the relationship between a Team and a Namespace // TeamNamespace defines the relationship between a Team and a Namespace
type TeamNamespace struct { type TeamNamespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` 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 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
View file

@ -0,0 +1 @@
package models

View file

@ -32,3 +32,11 @@ func (t *Team) IsAdmin(user *User) bool {
Get(&TeamMember{}) Get(&TeamMember{})
return exists 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
}