github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/teams.go (about) 1 package database 2 3 import ( 4 "time" 5 6 . "github.com/drone/drone/pkg/model" 7 "github.com/russross/meddler" 8 ) 9 10 // Name of the Team table in the database 11 const teamTable = "teams" 12 13 // SQL Queries to retrieve a list of all teams belonging to a user. 14 const teamStmt = ` 15 SELECT id, slug, name, email, gravatar, created, updated 16 FROM teams 17 WHERE id IN (select team_id from members where user_id = ?) 18 ` 19 20 // SQL Queries to retrieve a team by id and user. 21 const teamFindStmt = ` 22 SELECT id, slug, name, email, gravatar, created, updated 23 FROM teams 24 WHERE id = ? 25 ` 26 27 // SQL Queries to retrieve a team by slug. 28 const teamFindSlugStmt = ` 29 SELECT id, slug, name, email, gravatar, created, updated 30 FROM teams 31 WHERE slug = ? 32 ` 33 34 // Returns the Team with the given ID. 35 func GetTeam(id int64) (*Team, error) { 36 team := Team{} 37 err := meddler.QueryRow(db, &team, teamFindStmt, id) 38 return &team, err 39 } 40 41 // Returns the Team with the given slug. 42 func GetTeamSlug(slug string) (*Team, error) { 43 team := Team{} 44 err := meddler.QueryRow(db, &team, teamFindSlugStmt, slug) 45 return &team, err 46 } 47 48 // Saves a Team. 49 func SaveTeam(team *Team) error { 50 if team.ID == 0 { 51 team.Created = time.Now().UTC() 52 } 53 team.Updated = time.Now().UTC() 54 return meddler.Save(db, teamTable, team) 55 } 56 57 // Deletes an existing Team account. 58 func DeleteTeam(id int64) error { 59 // disassociate all repos with this team 60 db.Exec("UPDATE repos SET team_id = 0 WHERE team_id = ?", id) 61 // delete the team memberships and the team itself 62 db.Exec("DELETE FROM members WHERE team_id = ?", id) 63 db.Exec("DELETE FROM teams WHERE id = ?", id) 64 return nil 65 } 66 67 // Returns a list of all Teams associated 68 // with the specified User ID. 69 func ListTeams(id int64) ([]*Team, error) { 70 var teams []*Team 71 err := meddler.QueryAll(db, &teams, teamStmt, id) 72 return teams, err 73 }